redis失效时间问题总结

1、 在没有设置Redis的Value的时候,设置key的失效时间没有用
 
  
String redisKey = "test:string";
BoundValueOperations<String, String> redisOper = redisTemplate.boundValueOps(redisKey);
redisOper.expire(10, TimeUnit.DAYS);
2、对于 Redis 字符串(String),设置了失效日期之后再设置值,那么将会丢掉失效日期
 
  
String redisKey = "test:string";
BoundValueOperations<String, String> redisOper = redisTemplate.boundValueOps(redisKey);
redisOper.expire(10, TimeUnit.DAYS);
System.out.println(redisOper.getExpire());
redisOper.set("----------");
redisOper.expire(10, TimeUnit.DAYS);
System.out.println(redisOper.get());
System.out.println(redisOper.getExpire());
redisOper.set("-----+-----");
System.out.println(redisOper.get());
System.out.println(redisOper.getExpire());
3、 当Redis hash中的键值对清空的时候,这个redisKey也被Redis删除了,也就没有失效日期了
String redisKey = "test:hash";
BoundHashOperations redisOper = redisTemplate.boundHashOps(redisKey);
System.out.println(redisOper.getExpire());
redisOper.put("name","xiaoming");
redisOper.put("sex","man");
redisOper.expire(10, TimeUnit.DAYS);
System.out.println(redisOper.size());
System.out.println(redisOper.getExpire());
redisOper.delete("name");
System.out.println(redisOper.size());
System.out.println(redisOper.getExpire());
redisOper.delete("sex");
System.out.println(redisOper.size());
System.out.println(redisOper.getExpire());

你可能感兴趣的:(redis)