我使用spring 1.4.1,所以我的spring-boot-starter-data-redis在我的
这样的pom.xml文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>这是主要的课程:
@SpringBootApplication
public class App {
public static void main(String[] args) {
new SpringApplicationBuilder(App.class).web(true).run(args);
}
}这是用于测试的控制器类:
@RestController
public class CommonTestCtrl {
@Autowired
private RedisTemplate<Object, Object> template;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping(value = "/redisGet", method = RequestMethod.GET)
public String getRedisValue(@RequestParam(value = "key", required = false) String key) {
// not OK
this.template.opsForValue().set(888888, 188);
// OK
this.stringRedisTemplate.opsForValue().set(key + "String", "stringRedisTemplate");
return "";
}
}我发现this.template.opsForValue().set(888888,188);不会将任何东西保存给redis。为什么?
下面是代码URL:https://github.com/eacdy/test2。
你能帮我一下吗?经常这样。
发布于 2018-02-20 16:53:46
我从spring 1.3.7.RELEASE升级到1.5.10 upgraded。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>至:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>发布于 2018-03-17 09:04:05
我使用了来自弹簧-引导-数据-红-启动官方样本的代码,有一个不同之处:
@Autowired
private StringRedisTemplate template;
@Autowired
private RedisTemplate redisTemplate;
@Override
public void run(String... args) throws Exception {
{
ValueOperations<String, Integer> stringIntegerValueOperations = this.redisTemplate.opsForValue();
String key = "hello";
int v = 100;
if (!this.redisTemplate.hasKey(key)) {
System.out.println("write redisTemplate"); // stored in redis.
stringIntegerValueOperations.set(key, v);
}
Set<String> keys = this.redisTemplate.keys("*");
System.out.println(String.join(",", keys)); // 0 result.
}
{
ValueOperations<String, String> ops = this.template.opsForValue();
String key = "spring.boot.redis.test";
if (!this.template.hasKey(key)) {
ops.set(key, "foo"); // stored in redis server.
System.out.println("write stringTemplate");
}
Set<String> keys = this.template.keys("*"); // all data even ones stored by redisTemplate
System.out.println(String.join(",", keys));
}
}同时,我启动了redis以查看keys *提供了什么。
结果
RedisTemplate以二进制形式存储K(偶数字符串)(大部分不能打印)RedisTemplate hasKey(K)确实给出了正确的结果,尽管RedisTemplate keys("*")给出了0的结果,而不管键的类型(字符串还是二进制)。结论结论:RedisTemplate,无论是StringRedisTemplate还是原始RedisTemplate,其K均保存在redis服务器上。虽然原始的RedisTemplate键给出了0的结果,而且Redis keys *在二进制K开始时停止,但这两种情况都使它看起来像是RedisTemplate无法保存。
解决方案: RedisTemplate以不需要的二进制形式存储字符串K。像StringRedisTemplate一样,实现一个StringObjectRedisTemplate,而不是注入一个原始RedisTemplate,而是使用StringObjectRedisTemplate。
https://stackoverflow.com/questions/40282057
复制相似问题