stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis里存入數據和設置緩存時間
stringRedisTemplate.boundValueOps("test").increment(-1);//val做-1操作
stringRedisTemplate.opsForValue().get("test")//根據key獲取緩存中的val
stringRedisTemplate.boundValueOps("test").increment(1);//val +1
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根據key獲取過期時間並換算成指定單位
因為微信小程序中用到了這個模板,所以我需要檢查session是否過期,之前想的是先獲取指定單位的時間日期,然后再用當前日期減去指定日期,只要大於0就是沒過期,但是這樣做沒成功。最后找遍了百度,找到了一個
可以通過Key獲取有效時間:
- Long expire = redisTemplate.boundHashOps("red_123").getExpire();
- System.out.println("redis有效時間:"+expire+"S");
通過判斷有效時間是否大於0 ,就可以判斷session是否過期了。