在平时项目中,可能会有某个条件的查询,会多次进到db里面去查,这样就会重复的查询相同的数据,但是我们的数据又不是需要更改及显示的,这时候就可以用到

  方法的缓存了。例如在我们调用微信小程序时,需要获取access_token,并且其有效时间为7200秒,过期后再次获取,我们就可以把获取access_token的方法作为

  缓存。以下为我实现的过程记录。

 

1、重写 RedisSerializer 中的 serialize 和 deserialize

 1 public class GenericFastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
 2     public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
 3     public GenericFastJson2JsonRedisSerializer() {
 4         super();
 5     }
 6    @Override
 7     public byte[] serialize(T t) throws SerializationException {
 8         if (t == null) {
 9             return new byte[0];
10         }
11        FastJsonWraper<T> wraperSet =new FastJsonWraper<>(t);
12        return JSON.toJSONString(wraperSet, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
13    }
14     @Override
15     public T deserialize(byte[] bytes) throws SerializationException {
16         if (bytes == null || bytes.length <= 0) {
17             return null;
18         }
19         String deserializeStr = new String(bytes, DEFAULT_CHARSET);
20         FastJsonWraper<T> wraperGet=JSON.parseObject(deserializeStr,FastJsonWraper.class);
21         return wraperGet.getValue();
22     }
23 }
重写RedisSerializer

相关文章:

  • 2022-02-06
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2022-12-23
  • 2021-04-22
  • 2021-11-22
猜你喜欢
  • 2022-12-23
  • 2021-10-16
  • 2021-09-26
  • 2021-09-19
  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
相关资源
相似解决方案