【spring boot】2.0 配置@cacheable 自定义序列化方式 缓存数据到redis

转自 https://blog.csdn.net/b376924098/article/details/79820642

一·背景描述

  spring 的  @cacheable 已成为我们比较常用的缓存数据的方式,但是把非String对象缓存到redis后在使用RedisDesktopManager等软件查看缓存的数据的时候 展示的是HEX 数据,观察起来比较不方便,所以我们这里自定义了FastJsonRedisSerializer 序列化对象后缓存到redis,可以更 方便的观察缓存数据。

二·引入jar包

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastJson</artifactId>
   <version>1.2.38</version>
</dependency>

ps:导入fastjson 某些版本运行时会报com.alibaba.fastjson.JSONException: autoType is not support.错,在FastJsonRedisSerializer类中加入

 static {
        ParserConfig.getGlobalInstance().addAccept("com.xxxx.blog"); //com.xxxx.blog换成自己的包名..........
    }
即可解决

三·代码

1·自定义序列化方式

  1. import com.alibaba.fastjson.JSON;
  2. import com.alibaba.fastjson.serializer.SerializerFeature;
  3. import org.springframework.data.redis.serializer.RedisSerializer;
  4. import org.springframework.data.redis.serializer.SerializationException;
  5. import java.nio.charset.Charset;
  6. /**
  7. * 说明:自定义redis序列化方式
  8. *
  9. * @author WangBin
  10. * @version V1.0
  11. * @since 2018.03.22
  12. */
  13. public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
  14. public static final Charset DEFAULT_CHARSET = Charset.forName( "UTF-8");
  15. private Class<T> clazz;
  16. public FastJsonRedisSerializer(Class<T> clazz) {
  17. super();
  18. this.clazz = clazz;
  19. }
  20. @Override
  21. public byte[] serialize(T t) throws SerializationException {
  22. if (t == null) {
  23. return new byte[ 0];
  24. }
  25. return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
  26. }
  27. @Override
  28. public T deserialize(byte[] bytes) throws SerializationException {
  29. if (bytes == null || bytes.length <= 0) {
  30. return null;
  31. }
  32. String str = new String(bytes, DEFAULT_CHARSET);
  33. return JSON.parseObject(str, clazz);
  34. }
  35. }

2·配置

  1. import org.springframework.cache.annotation.CachingConfigurerSupport;
  2. import org.springframework.cache.annotation.EnableCaching;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.data.redis.cache.RedisCacheConfiguration;
  6. import org.springframework.data.redis.serializer.RedisSerializationContext;
  7. import java.time.Duration;
  8. /**
  9. *
  10. * @author WangBin
  11. * @version V1.0
  12. * @date 2017.12.05
  13. */
  14. @Configuration
  15. @EnableCaching
  16. public class RedisConfig extends CachingConfigurerSupport {
  17. /**
  18. * 设置 redis 数据默认过期时间
  19. * 设置@cacheable 序列化方式
  20. * @return
  21. */
  22. @Bean
  23. public RedisCacheConfiguration redisCacheConfiguration(){
  24. FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
  25. RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
  26. configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofDays( 30));
  27. return configuration;
  28. }
  29. }

猜你喜欢

转载自blog.csdn.net/qq_24084605/article/details/81006908