springDataRedis存, 客户端却取不到数据

[b]如果key没做序列化存储,实际存进去的key前面会多几个字符,如果你用redis客户端查询你想要的key,最好在程序里对key进行序列化,这样最终的key值才是你想要的key,redisTemplate下面有这两个属性[/b]

<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>


springBoot做法:

@Configuration
public class RedisConfig {

@Bean
JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}

@Bean
public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, T> template = new RedisTemplate<String, T>();
template.setConnectionFactory(factory);
//1.序列化key
template.setKeySerializer(new StringRedisSerializer());//spring自带
//2.序列划value
template.setValueSerializer(new RedisObjectSerializer());//自定义
return template;
}

}
展开阅读全文

没有更多推荐了,返回首页