实现一个RedisSerializer<T>用在spring-boot-starter-data-redis中


声明:本文转载自https://my.oschina.net/Kxvz/blog/1604427,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

需求: 因为会使用到Spring-redis作为缓存框架缓存数据,不让程序每次查询都进到数据库中,因此正对不同的数据格式需要进行序列化和反序列化,于是就自己实现了一个简单的序列化类

首先 配置 Springboot 的 redis 配置

# REDIS (RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0   # Redis服务器地址 spring.redis.host=192.168.0.11 # Redis服务器连接端口 spring.redis.port=6379   # Redis服务器连接密码(默认为空) spring.redis.password=ThisIsPass # 连接池最大连接数(使用负值表示没有限制) spring.redis.pool.max-active=-1   # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.pool.max-wait=-1   # 连接池中的最大空闲连接 spring.redis.pool.max-idle=8   # 连接池中的最小空闲连接 spring.redis.pool.min-idle=2   # 连接超时时间(毫秒) spring.redis.timeout=50

接下来实现 序列化类 (这里使用到了GSON):

import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException;   public class RedisValueSerializer<T> implements RedisSerializer<T> {      private static final byte[] EMPTY_ARRAY = new byte[0];      @Override     public byte[] serialize(T t) throws SerializationException {         if (t == null) {             return EMPTY_ARRAY;         }         String tmp = String.format("%s_%s", t.getClass().getName(), JsonUtils.obj2Json(t));         return tmp.getBytes(Encode.UTF8);     }      @Override     public T deserialize(byte[] bytes) throws SerializationException {         try {             if (bytes == null || bytes.length <= 0) {                 return null;             }             String tmp = new String(bytes, Encode.UTF8);             String className = tmp.substring(0, tmp.indexOf("_{"));             String json = tmp.substring(tmp.indexOf("_{") + 1);             return (T) JsonUtils.json2Obj(json, Class.forName(className));         } catch (ClassNotFoundException ex) {             LogUtils.error(TAG.DEF, this.getClass(), "deserialize", ex);         }         return null;     } }

 

然后RedisConfig:

import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;   @Primary @Configuration public class RedisConfig extends CachingConfigurerSupport {      @Primary     @Bean     public CacheManager cacheManager(RedisTemplate redisTemplate) {         RedisCacheManager rcm = new RedisCacheManager(redisTemplate);         return rcm;     }      @Primary     @Bean     public StringRedisTemplate redisTemplate(RedisConnectionFactory factory) {         StringRedisTemplate template = new StringRedisTemplate(factory);         template.setValueSerializer(new RedisValueSerializer<Object>());         template.afterPropertiesSet();         return template;     } }

然后就可以使用缓存注解了:

@Cacheable(key ="#p0")   @CachePut(key = "#p0")  @CacheEvict(key ="#p0",allEntries=true)  ...

 

注解使用参考:http://blog.csdn.net/sanjay_f/article/details/47372967

 

本文发表于2018年01月09日 16:32
(c)注:本文转载自https://my.oschina.net/Kxvz/blog/1604427,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 3177 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1