When I use spring-data-redis, it across two problems.
The First problem is when I use cache annation to load data to redis, but I can not get the value by redisTemplate, the code as flows :
@Bean
RedisTemplate<Object, Object> redisTemplate() {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
return redisTemplate;
}
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName("localhost");
factory.setPort(6379);
factory.setUsePool(true);
return factory;
}
@Bean
CacheManager cacheManager() {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
cacheManager.setDefaultExpiration(86400);
return cacheManager;
}
UserService as flows :
@Cacheable("allUsers")
public List<UserModel> getAllUsers() {
logger.info("execute getAllUsers!");
return Arrays.asList(new UserModel("jason"), new UserModel("david"));
}
my test code is :
@Test
public void testCache() {
redisTemplate.delete("allUsers");
userService.getAllUsers(); // load user data
List<Object> users = redisTemplate.opsForList().range("allUsers", 0, -1); // get allUsers from redis
logger.info("get from template");
for(Object user : users) {
logger.info(((UserModel)user).getUsername());
}
logger.info("get from service");
for(UserModel user : userService.getAllUsers()) {
logger.info(user.getUsername());
}
and , I found the cache "allUsers" in redis is allUsers~keys, but I also can not get value by this key, how can I get the value by redisTemplate and why the key is allUsers~keys in redis.
The second problem is I set a key call "mykey" by commond , and set value "mykey", and I also cant get null value by java code , I can get value through commond. How is that? If the two problems are the same one? Is anyone know these? Thank you advance ^.^ .
update my code version is :
spring 4.1.6,
spring-data-redis 1.7.0,
Jedis 2.8.1
redis 3.2.3