添加 maven 依赖
de.ruedigermoeller
fst
2.57
实现 RedisSerializer 接口
public class FSTSerializer implements RedisSerializer {
private FSTConfiguration fstConfiguration ;
public FSTSerializer() {
fstConfiguration = FSTConfiguration.getDefaultConfiguration();
fstConfiguration.setClassLoader(Thread.currentThread().getContextClassLoader());
}
@Override
public byte[] serialize(Object obj) {
if (ObjectUtil.isEmpty(obj)) {
return null;
}
return fstConfiguration.asByteArray(obj);
}
@Override
public Object deserialize(byte[] bytes) {
if (ObjectUtil.isEmpty(bytes)) {
return null;
}
return fstConfiguration.asObject(bytes);
}
}
定义一个 bean
@Bean
public RedisTemplate customRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new FSTSerializer());
return redisTemplate;
}
然后就可以使用了
@Autowired
private RedisTemplate myRedisTemplate;
myRedisTemplate.opsForValue().get("hello");