背景说明
redis在spring中可以使用自己实现的reditUtils工具也可以使用开源的jar包
这里分别以springboot和spring需要的jar为背景,以maven的方式引入
springboot配置需要的jar包
spring-boot-starter-redis.jar
spring中配置需要的jar包
spring-data-redis-1.6.2.RELEASE.jar
jedis-2.7.2.jar(依赖 commons-pool2-2.3.jar)
commons-pool2-2.3.jar
redis在spring中的配置
请参考我的另一篇博文,在spring中的配置基本大同小异
redis在springboot中的配置
spring中的配置参考另一位写的的
redis在spring中的配置
常遇到的坑
redistempalate的超时设置
当用户每次调用接口时如果客户端提供的token验证成功则自动把该token的过期时间重置一次
但是这里直接重置延期时间会遇到坑,根本不起作用。
redistempalate的超时设置时,一定要每次用set写入时,更新超时,不然默认会失效的。
例如:
int tempTime = this.redisTemplate.getExpire("max").intValue();
tempCount = this.redisTemplate.opsForValue().get("max")-1;
this.redisTemplate.opsForValue().set("max", tempCount);
this.redisTemplate.expire("max",tempTime,TimeUnit.SECONDS);
获取到max的值,把该值减一后重新设置max的新值,然后设置max的延期时间