项目中redisTemplate设置的key,redis客户端上查询不到的问题
再项目使用了redis储存key,测试需要在客户端删除对应的key,发现查询不到对应的key
redis客户端:
发现redisTemplate实际存进去的key会多了几个字符
原因:程序中对key没做序列化存储
如果你用redis客户端查询你想要的key,redisTemplate下面添加这两个属性
<property?name="keySerializer">?? ???????? <bean?class="org.springframework.data.redis.serializer.StringRedisSerializer"?/>?? ????</property>??? ????<property?name="hashKeySerializer">?? ????????<bean?class="org.springframework.data.redis.serializer.StringRedisSerializer"?/>?? ?</property>?
或者
@Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; }
注意如果使用springboot会默认创建JdkSerializationRedisSerializer序列化类,我们需要自己定义redisTemplate对应的bean,且bean的名称必须为redisTemplate,否则springboot会默认创建
相关推荐
jackbon 2020-10-19
anglehearts 2020-09-09
天空一样的蔚蓝 2020-08-24
chenhualong0 2020-11-16
MRFENGG 2020-11-11
fackyou00 2020-11-10
Quietboy 2020-10-30
王道革 2020-11-25
wangdonghello 2020-11-03
Langeldep 2020-11-16
聚合室 2020-11-16
koushr 2020-11-12
guoyanga 2020-11-10
Orangesss 2020-11-03
dongCSDN 2020-10-31
rainandtear 2020-10-30
liuyulong 2020-10-29
fansili 2020-10-29
温攀峰 2020-10-23