我使用了spring-data-redis并将模板设置如下:
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(jedisConnectionFactory());
//template.setDefaultSerializer(new StringRedisSerializer());
return template;
}
@Autowired
private RedisTemplate<String, Object> redisTemplate;
private ValueOperations<String, Object> valueOps;
public static final String CATEGORY="QUOTE";
public void save(String key, String value) {
valueOps.set(key, value);
logger.info("redis get key={}, value={}", key, get(key));
}
当我运行代码时,它可以将值显示为String,
Aug 02, 2017 5:00:52 下午 com.web.service.QuoteRepo save
INFO: redis get key=QUOTE:932:324371:rem, value=4
当我去redis客户端
$ ./redis-cli
127.0.0.1:6379>get QUOTE:932:324371:rem
(nil)
答案 0 :(得分:0)
请注意以下代码行:
private ValueOperations<String, Object> valueOps;
您应该将模板注入ValueOperations。你忘记了这样的事情:
@resource(name="redisTemplate")
private ValueOperations<String, Object> valueOps;
请参阅此内容以获取更多信息:https://docs.spring.io/spring-data/redis/docs/1.8.6.RELEASE/reference/html/ 第5.5节对你有帮助。