Redis的键值默认应用JDK序列化,为不便排查问题,需自定义Json列化,并退出客户端操作工具类,不便各服务应用。
1、定义序列化FastJsonRedisSerializer
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> { public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; private Class<T> clazz; public FastJsonRedisSerializer(Class<T> 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); }}
2、自定义redisTemplate
@Configurationpublic class RedisConfiguration { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); RedisSerializer stringRedisSerializer = new StringRedisSerializer(); FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); ParserConfig.getGlobalInstance().addAccept("com.test."); // 设置值(value)的序列化采纳FastJsonRedisSerializer redisTemplate.setValueSerializer(fastJsonRedisSerializer); redisTemplate.setHashValueSerializer(fastJsonRedisSerializer); // 设置键(key)的序列化采纳StringRedisSerializer redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; }}
3、操作工具类
3.1、抽取AbstractDataManager
public abstract class AbstractDataManager { private static final String REDIS_TEMPLATE = "redisTemplate"; /** * redis操作 */ @Resource(name = REDIS_TEMPLATE) protected RedisOperations redisOps; /** * 简略字符串类型操作 */ @Resource(name = REDIS_TEMPLATE) protected ValueOperations valOps; /** * Hash类型操作 */ @Resource(name = REDIS_TEMPLATE) protected HashOperations hashOps; /** * Set类型操作 */ @Resource(name = REDIS_TEMPLATE) protected SetOperations setOps; /** * ZSet类型操作 */ @Resource(name = REDIS_TEMPLATE) protected ZSetOperations zSetOps; /** * List类型操作 */ @Resource(name = REDIS_TEMPLATE) protected ListOperations listOps;}
3.2、工具类
public class RedisUtils extends AbstractDataManager { /** * 设置 String 类型 value * * @param key * @param value */ public void set(String key, Object value) { valOps.set(key, value); } /** * 设置 String 类型 value 并增加过期工夫 (毫秒单位) * * @param key * @param value * @param timeout 过期工夫 * @param timeUnit 工夫单位 */ public void set(String key, Object value, long timeout, TimeUnit timeUnit) { valOps.set(key, value, timeout, timeUnit); } /** * 当key不存在时新增 * * @param key * @param value * @return */ public Boolean setNx(String key, Object value) { return valOps.setIfAbsent(key, value); } /** * 当key不存在时新增 ,减少无效工夫 * * @param key * @param value * @param timeout 过期工夫 * @param timeUnit 工夫单位 * @return */ public Boolean setNx(String key, Object value, long timeout, TimeUnit timeUnit) { return valOps.setIfAbsent(key, value, timeout, timeUnit); } /** * 获取 String 类型 value * * @param key * @return */ public Object get(String key) { return valOps.get(key); } /** * 如果 key 存在则笼罩,并返回旧值. * 如果不存在,返回null 并增加 * * @param key * @param value * @return */ public String getAndSet(String key, Object value) { return (String) valOps.getAndSet(key, value); } /** * 批量增加 key-value (反复的键会笼罩) * * @param map */ public void multiSet(Map map) { valOps.multiSet(map); } /** * 批量增加 key-value 只有在键不存在时,才增加 * map 中只有有一个key存在,则全副不增加 * * @param map */ public void batchSetIfAbsent(Map map) { valOps.multiSetIfAbsent(map); } /** * 对一个 key-value 的值进行加减操作, * 如果该 key 不存在 将创立一个key 并赋值该 number * 如果 key 存在,但 value 不是长整型 ,将报错 * * @param key * @param number */ public Long increment(String key, long number) { return valOps.increment(key, number); } /** * 对一个 key-value 的值进行加减操作, 并第一次操作减少过期工夫 * 如果该 key 不存在 将创立一个key 并赋值该 number * 如果 key 存在,但 value 不是长整型 ,将报错 * * @param key * @param number */ public Long increment(String key, long number, long timeout, TimeUnit timeUnit) { Long increment = valOps.increment(key, number); if (increment == 1) { redisOps.expire(key, timeout, timeUnit); } return increment; } /** * 对一个 key-value 的值进行加减操作, * 如果该 key 不存在 将创立一个key 并赋值该 number * 如果 key 存在,但 value 不是 纯数字 ,将报错 * * @param key * @param number */ public Double increment(String key, double number) { return valOps.increment(key, number); } /** * 给一个指定的 key 值附加过期工夫 * * @param key * @param time * @param type * @return */ public boolean expire(String key, long time, TimeUnit type) { return redisOps.boundValueOps(key).expire(time, type); } /** * 移除指定key 的过期工夫 * * @param key * @return */ public boolean persist(String key) { return redisOps.boundValueOps(key).persist(); } /** * 获取指定key 的过期工夫 * * @param key * @return */ public Long getExpire(String key) { return redisOps.boundValueOps(key).getExpire(); } /** * 批改 key * * @param key * @return */ public void rename(String key, String newKey) { redisOps.boundValueOps(key).rename(newKey); } /** * 删除 key-value * * @param key * @return */ public boolean delete(String key) { return redisOps.delete(key); } /** ----------------------------------- hash 操作 start ----------------------------*/ /** * 增加 Hash 键值对 * * @param key * @param hashKey * @param value */ public void put(String key, String hashKey, Object value) { hashOps.put(key, hashKey, value); } /** * 批量增加 hash 的 键值对 * 有则笼罩,没有则增加 * * @param key * @param map */ public void putAll(String key, Map map) { hashOps.putAll(key, map); } /** * 增加 hash 键值对. 不存在的时候才增加 * * @param key * @param hashKey * @param value * @return */ public boolean putIfAbsent(String key, String hashKey, Object value) { return hashOps.putIfAbsent(key, hashKey, value); } /** * 删除指定 hash 的 HashKey * * @param key * @param hashKeys * @return 删除胜利的 数量 */ public Long delete(String key, String... hashKeys) { return hashOps.delete(key, hashKeys); } /** * 给指定 hash 的 hashkey 做增减操作 * * @param key * @param hashKey * @param number * @return */ public Long increment(String key, String hashKey, long number) { return hashOps.increment(key, hashKey, number); } /** * 给指定 hash 的 hashkey 做增减操作 * * @param key * @param hashKey * @param number * @return */ public Double increment(String key, String hashKey, Double number) { return hashOps.increment(key, hashKey, number); } /** * 获取指定 key 下的 hashkey * * @param key * @param hashKey * @return */ public Object getHashKey(String key, String hashKey) { return hashOps.get(key, hashKey); } /** * 获取 key 下的 所有 hashkey 和 value * * @param key * @return */ public Map getHashEntries(String key) { return hashOps.entries(key); } /** * 验证指定 key 下 有没有指定的 hashkey * * @param key * @param hashKey * @return */ public boolean hashKey(String key, String hashKey) { return hashOps.hasKey(key, hashKey); } /** * 获取 key 下的 所有 hashkey 字段名 * * @param key * @return */ public Set hashKeys(String key) { return hashOps.keys(key); } /** * 获取指定 hash 上面的 键值对 数量 * * @param key * @return */ public Long hashSize(String key) { return hashOps.size(key); } /** ------------------------------------ hash 操作 end -----------------------------*/ /** ----------------------------------- List 操作 start ----------------------------*/ /** * 指定 list 从左入栈 * * @param key * @return 以后队列的长度 */ public Long leftPush(String key, Object value) { return listOps.leftPush(key, value); } /** * 指定 list 从左出栈 * 如果列表没有元素,会梗塞到列表始终有元素或者超时为止 * * @param key * @return 出栈的值 */ public Object leftPop(String key) { return listOps.leftPop(key); } /** * 从右边顺次入栈 * 导入程序依照 Collection 程序 * 如: a b c => c b a * * @param key * @param values * @return */ public Long leftPushAll(String key, Collection<Object> values) { return listOps.leftPushAll(key, values); } /** * 指定 list 从右入栈 * * @param key * @return 以后队列的长度 */ public Long rightPush(String key, Object value) { return listOps.rightPush(key, value); } /** * 指定 list 从右出栈 * 如果列表没有元素,会梗塞到列表始终有元素或者超时为止 * * @param key * @return 出栈的值 */ public Object rightPop(String key) { return listOps.rightPop(key); } /** * 从左边顺次入栈 * 导入程序依照 Collection 程序 * 如: a b c => a b c * * @param key * @param values * @return */ public Long rightPushAll(String key, Collection<Object> values) { return listOps.rightPushAll(key, values); } /** * 依据下标获取值 * * @param key * @param index * @return */ public Object popIndex(String key, long index) { return listOps.index(key, index); } /** * 获取列表指定长度 * * @param key * @param index * @return */ public Long listSize(String key, long index) { return listOps.size(key); } /** * 获取列表 指定范畴内的所有值 * * @param key * @param start * @param end * @return */ public List listRange(String key, long start, long end) { return listOps.range(key, start, end); } /** * 删除 key 中 值为 value 的 count 个数. * * @param key * @param count * @param value * @return 胜利删除的个数 */ public Long listRemove(String key, long count, Object value) { return listOps.remove(key, count, value); } /** * 删除 列表 [start,end] 以外的所有元素 * * @param key * @param start * @param end */ public void listTrim(String key, long start, long end) { listOps.trim(key, start, end); } /** * 将 key 右出栈,并左入栈到 key2 * * @param key 右出栈的列表 * @param key2 左入栈的列表 * @return 操作的值 */ public Object rightPopAndLeftPush(String key, String key2) { return listOps.rightPopAndLeftPush(key, key2); } /** ----------------------------------- List 操作 end ----------------------------*/ /** ----------------------------------- Set 操作 start --------------------------*/ /** * 增加 set 元素 * * @param key * @param values * @return */ public Long add(String key, String... values) { return setOps.add(key, values); } /** * 获取两个汇合的差集 * * @param key * @param otherkey * @return */ public Set difference(String key, String otherkey) { return setOps.difference(key, otherkey); } /** * 获取 key 和 汇合 collections 中的 key 汇合的差集 * * @param key * @param otherKeys * @return */ public Set difference(String key, Collection<Object> otherKeys) { return setOps.difference(key, otherKeys); } /** * 将 key 与 otherkey 的差集 ,增加到新的 newKey 汇合中 * * @param key * @param otherkey * @param newKey * @return 返回差集的数量 */ public Long differenceAndStore(String key, String otherkey, String newKey) { return setOps.differenceAndStore(key, otherkey, newKey); } /** * 将 key 和 汇合 collections 中的 key 汇合的差集 增加到 newkey 汇合中 * * @param key * @param otherKeys * @param newKey * @return 返回差集的数量 */ public Long differenceAndStore(String key, Collection<Object> otherKeys, String newKey) { return setOps.differenceAndStore(newKey, otherKeys, newKey); } /** * 删除一个或多个汇合中的指定值 * * @param key * @param values * @return 胜利删除数量 */ public Long remove(String key, Object... values) { return setOps.remove(key, values); } /** * 随机移除一个元素,并返回进去 * * @param key * @return */ public Object randomSetPop(String key) { return setOps.pop(key); } /** * 随机获取一个元素 * * @param key * @return */ public Object randomSet(String key) { return setOps.randomMember(key); } /** * 随机获取指定数量的元素,同一个元素可能会选中两次 * * @param key * @param count * @return */ public List randomSet(String key, long count) { return setOps.randomMembers(key, count); } /** * 随机获取指定数量的元素,去重(同一个元素只能抉择两一次) * * @param key * @param count * @return */ public Set randomSetDistinct(String key, long count) { return setOps.distinctRandomMembers(key, count); } /** * 将 key 中的 value 转入到 destKey 中 * * @param key * @param value * @param destKey * @return 返回胜利与否 */ public boolean moveSet(String key, Object value, String destKey) { return setOps.move(key, value, destKey); } /** * 无序汇合的大小 * * @param key * @return */ public Long setSize(String key) { return setOps.size(key); } /** * 判断 set 汇合中 是否有 value * * @param key * @param value * @return */ public boolean isMember(String key, Object value) { return setOps.isMember(key, value); } /** * 返回 key 和 othere 的并集 * * @param key * @param otherKey * @return */ public Set unionSet(String key, String otherKey) { return setOps.union(key, otherKey); } /** * 返回 key 和 otherKeys 的并集 * * @param key * @param otherKeys key 的汇合 * @return */ public Set unionSet(String key, Collection<Object> otherKeys) { return setOps.union(key, otherKeys); } /** * 将 key 与 otherKey 的并集,保留到 destKey 中 * * @param key * @param otherKey * @param destKey * @return destKey 数量 */ public Long unionAndStoreSet(String key, String otherKey, String destKey) { return setOps.unionAndStore(key, otherKey, destKey); } /** * 将 key 与 otherKey 的并集,保留到 destKey 中 * * @param key * @param otherKeys * @param destKey * @return destKey 数量 */ public Long unionAndStoreSet(String key, Collection<Object> otherKeys, String destKey) { return setOps.unionAndStore(key, otherKeys, destKey); } /** * 返回汇合中所有元素 * * @param key * @return */ public Set members(String key) { return setOps.members(key); } /** ----------------------------------- Set 操作 end --------------------------*/ /** ----------------------------------- ZSet 操作 start -------------------------- * Zset 依据 socre 排序 不反复 每个元素附加一个 socre double类型的属性(double 能够反复) */ /** * 增加 ZSet 元素 * * @param key * @param value * @param score */ public boolean add(String key, Object value, double score) { return zSetOps.add(key, value, score); } /** * 批量增加 Zset <br> * Set<TypedTuple<Object>> tuples = new HashSet<>();<br> * TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<Object>("zset-5",9.6);<br> * tuples.add(objectTypedTuple1); * * @param key * @param tuples * @return */ public Long batchAddZset(String key, Set<ZSetOperations.TypedTuple<Object>> tuples) { return zSetOps.add(key, tuples); } /** * Zset 删除一个或多个元素 * * @param key * @param values * @return */ public Long removeZset(String key, String... values) { return zSetOps.remove(key, values); } /** * 对指定的 zset 的 value 值 , socre 属性做增减操作 * * @param key * @param value * @param score * @return */ public Double incrementScore(String key, Object value, double score) { return zSetOps.incrementScore(key, value, score); } /** * 获取 key 中指定 value 的排名(从0开始,从小到大排序) * * @param key * @param value * @return */ public Long rank(String key, Object value) { return zSetOps.rank(key, value); } /** * 获取 key 中指定 value 的排名(从0开始,从大到小排序) * * @param key * @param value * @return */ public Long reverseRank(String key, Object value) { return zSetOps.reverseRank(key, value); } /** * 获取索引区间内的排序后果汇合(从0开始,从小到大,带上分数) * * @param key * @param start * @param end * @return */ public Set<ZSetOperations.TypedTuple<Object>> rangeWithScores(String key, long start, long end) { return zSetOps.rangeWithScores(key, start, end); } /** * 获取索引区间内的排序后果汇合(从0开始,从小到大,只有列名) * * @param key * @param start * @param end * @return */ public Set range(String key, long start, long end) { return zSetOps.range(key, start, end); } /** * 获取分数范畴内的 [min,max] 的排序后果汇合 (从小到大,只有列名) * * @param key * @param min * @param max * @return */ public Set rangeByScore(String key, double min, double max) { return zSetOps.rangeByScore(key, min, max); } /** * 获取分数范畴内的 [min,max] 的排序后果汇合 (从小到大,汇合带分数) * * @param key * @param min * @param max * @return */ public Set<ZSetOperations.TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max) { return zSetOps.rangeByScoreWithScores(key, min, max); } /** * 返回 分数范畴内 指定 count 数量的元素汇合, 并且从 offset 下标开始(从小到大,不带分数的汇合) * * @param key * @param min * @param max * @param offset 从指定下标开始 * @param count 输入指定元素数量 * @return */ public Set rangeByScore(String key, double min, double max, long offset, long count) { return zSetOps.rangeByScore(key, min, max, offset, count); } /** * 返回 分数范畴内 指定 count 数量的元素汇合, 并且从 offset 下标开始(从小到大,带分数的汇合) * * @param key * @param min * @param max * @param offset 从指定下标开始 * @param count 输入指定元素数量 * @return */ public Set<ZSetOperations.TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max, long offset, long count) { return zSetOps.rangeByScoreWithScores(key, min, max, offset, count); } /** * 获取索引区间内的排序后果汇合(从0开始,从大到小,只有列名) * * @param key * @param start * @param end * @return */ public Set reverseRange(String key, long start, long end) { return zSetOps.reverseRange(key, start, end); } /** * 获取索引区间内的排序后果汇合(从0开始,从大到小,带上分数) * * @param key * @param start * @param end * @return */ public Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String key, long start, long end) { return zSetOps.reverseRangeWithScores(key, start, end); } /** * 获取分数范畴内的 [min,max] 的排序后果汇合 (从大到小,汇合不带分数) * * @param key * @param min * @param max * @return */ public Set reverseRangeByScore(String key, double min, double max) { return zSetOps.reverseRangeByScore(key, min, max); } /** * 获取分数范畴内的 [min,max] 的排序后果汇合 (从大到小,汇合带分数) * * @param key * @param min * @param max * @return */ public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max) { return zSetOps.reverseRangeByScoreWithScores(key, min, max); } /** * 返回 分数范畴内 指定 count 数量的元素汇合, 并且从 offset 下标开始(从大到小,不带分数的汇合) * * @param key * @param min * @param max * @param offset 从指定下标开始 * @param count 输入指定元素数量 * @return */ public Set reverseRangeByScore(String key, double min, double max, long offset, long count) { return zSetOps.reverseRangeByScore(key, min, max, offset, count); } /** * 返回 分数范畴内 指定 count 数量的元素汇合, 并且从 offset 下标开始(从大到小,带分数的汇合) * * @param key * @param min * @param max * @param offset 从指定下标开始 * @param count 输入指定元素数量 * @return */ public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max, long offset, long count) { return zSetOps.reverseRangeByScoreWithScores(key, min, max, offset, count); } /** * 返回指定分数区间 [min,max] 的元素个数 * * @param key * @param min * @param max * @return */ public long countZSet(String key, double min, double max) { return zSetOps.count(key, min, max); } /** * 返回 zset 汇合数量 * * @param key * @return */ public long sizeZset(String key) { return zSetOps.size(key); } /** * 获取指定成员的 score 值 * * @param key * @param value * @return */ public Double score(String key, Object value) { return zSetOps.score(key, value); } /** * 删除指定索引地位的成员,其中成员分数按( 从小到大 ) * * @param key * @param start * @param end * @return */ public Long removeRange(String key, long start, long end) { return zSetOps.removeRange(key, start, end); } /** * 删除指定 分数范畴 内的成员 [main,max],其中成员分数按( 从小到大 ) * * @param key * @param min * @param max * @return */ public Long removeRangeByScore(String key, double min, double max) { return zSetOps.removeRangeByScore(key, min, max); } /** * key 和 other 两个汇合的并集,保留在 destKey 汇合中, 列名雷同的 score 相加 * * @param key * @param otherKey * @param destKey * @return */ public Long unionAndStoreZset(String key, String otherKey, String destKey) { return zSetOps.unionAndStore(key, otherKey, destKey); } /** * key 和 otherKeys 多个汇合的并集,保留在 destKey 汇合中, 列名雷同的 score 相加 * * @param key * @param otherKeys * @param destKey * @return */ public Long unionAndStoreZset(String key, Collection<String> otherKeys, String destKey) { return zSetOps.unionAndStore(key, otherKeys, destKey); } /** * key 和 otherKey 两个汇合的交加,保留在 destKey 汇合中 * * @param key * @param otherKey * @param destKey * @return */ public Long intersectAndStore(String key, String otherKey, String destKey) { return zSetOps.intersectAndStore(key, otherKey, destKey); } /** * key 和 otherKeys 多个汇合的交加,保留在 destKey 汇合中 * * @param key * @param otherKeys * @param destKey * @return */ public Long intersectAndStore(String key, Collection<String> otherKeys, String destKey) { return zSetOps.intersectAndStore(key, otherKeys, destKey); } /** ----------------------------------- ZSet 操作 end --------------------------*/}
3.3、主动注入到spring中
resources中创立META-INF文件夹,创立spring.factories
增加内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.mtsbw.sca.redis.config.RedisConfiguration,\com.mtsbw.sca.redis.utils.RedisUtils