StringRedisTemplate常用操作,以及如何判断session过期

[java]  view plain  copy
  1. stringRedisTemplate.opsForValue().set("test""100",60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间  
[java]  view plain  copy
  1. stringRedisTemplate.boundValueOps("test").increment(-1);//val做-1操作  
[java]  view plain  copy
  1. stringRedisTemplate.opsForValue().get("test")//根据key获取缓存中的val  
[java]  view plain  copy
  1. stringRedisTemplate.boundValueOps("test").increment(1);//val +1  
[java]  view plain  copy
  1. stringRedisTemplate.getExpire("test")//根据key获取过期时间  
[java]  view plain  copy
  1. stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根据key获取过期时间并换算成指定单位  
[java]  view plain  copy
  1. stringRedisTemplate.delete("test");//根据key删除缓存  
[java]  view plain  copy
  1. stringRedisTemplate.hasKey("546545");//检查key是否存在,返回boolean值  
[java]  view plain  copy
  1. stringRedisTemplate.opsForSet().add("red_123""1","2","3");//向指定key中存放set集合  
[java]  view plain  copy
  1. stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//设置过期时间  
[java]  view plain  copy
  1. stringRedisTemplate.opsForSet().isMember("red_123""1")//根据key查看集合中是否存在指定数据  
[java]  view plain  copy
  1. stringRedisTemplate.opsForSet().members("red_123");//根据key获取set集合  



因为微信小程序中用到了这个模板,所以我需要检查session是否过期,之前想的是先获取指定单位的时间日期,然后再用当前日期减去指定日期,只要大于0就是没过期,但是这样做没成功。最后找遍了百度,找到了一个

可以通过Key获取有效时间:
Long expire = redisTemplate.boundHashOps("red_123").getExpire();
System.out.println("redis有效时间:"+expire+"S");

通过判断有效时间是否大于0 ,就可以判断session是否过期了。

猜你喜欢

转载自blog.csdn.net/qq_35357001/article/details/77966861