redis在spring和springboot中的使用方式以及遇到的坑
2017-10-13 17:53
736 查看
背景说明
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的延期时间
相关文章推荐
- redis中的基本数据类型,以及在Spring-Boot对Redis的基本使用
- springboot读取自己定义的配置文件的方式以及使用joda_time来处理时间日期
- spring boot 项目打包jar 启动方式,以及 可能遇到的坑。
- springboot+redis(注解方式)使用(java代码部分)
- Redis Scan的使用方式以及Spring redis的坑
- 关于Spring boot 与 redis 的集成,以及Spring中redis的使用
- Redis Scan的使用方式以及Spring redis的坑
- Redis Scan的使用方式以及Spring redis的坑
- redis中的基本数据类型,以及在Spring-Boot对Redis的基本使用
- REDIS学习(3.2)spring boot 使用redis作为缓存
- Spring Boot使用redis做数据缓存
- 使用gradle来构建Spring boot时遇到的问题
- Spring Boot使用redis做数据缓存
- Redis 一二事 - 在spring中使用jedis 连接调试单机redis以及集群redis
- Spring Boot 1.2.5使用redis做数据缓存
- 使用spring redisTemplate.getValueSerializer()遇到的问题
- spring boot使用redis
- DTO的理解以及spring遇到的一个问题注解方式对象为空
- spring 整合 redis,以及spring的RedisTemplate如何使用
- 深入理解Spring Redis的使用 (九)、通过Redis 实现 分布式锁 的 BUG,以及和数据库加锁的性能测试