fastjson 我用FastJson实现序列化,然后通过springcache进行缓存 缓存的对象还是没有序列化,并且返回的缓存对象id还是null,

jfewjypa  于 2021-11-27  发布在  Java
关注(0)|答案(0)|浏览(174)

package com.xiaobu.base.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.charset.Charset;

/**

  • @author xiaobu
  • @Version JDK1.8.0_171
  • @Date on 2019/9/4 17:07
  • @description 序列化器

*/
public class FastJsonRedisSerializer implements RedisSerializer {

public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

private Class clazz;

public FastJsonRedisSerializer(Class clazz) {
super();
this.clazz = clazz;
}

@OverRide
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}

@OverRide
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}

}

package com.xiaobu.config;

import com.alibaba.fastjson.parser.ParserConfig;
import com.xiaobu.base.entity.FastJsonRedisSerializer;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**

  • @author xiaobu
  • @Version JDK1.8.0_171
  • @Date on 2019/9/4 15:36
  • @description 自定义序列化

*/
@configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

@bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
// 全局开启AutoType,不建议使用
// ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
// 建议使用这种方式,小范围指定白名单
ParserConfig.getGlobalInstance().addAccept("com.xiaobu.entity.");
// 设置值(value)的序列化采用FastJsonRedisSerializer。
redisTemplate.setValueSerializer(fastJsonRedisSerializer);
redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
// 设置键(key)的序列化采用StringRedisSerializer。
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

//设置缓存
@Cacheable(value = "cacheCity",key ="#id", condition ="#id<2")
@GetMapping("getCityByCache/{id}")
public City getCityByCache(@PathVariable Integer id) {
return cityService.getCityByCache(id);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题