Springboot鼓励零配置的方式,帮你做好大部分重复劳动的事,好到不能再好;具体的Redis安装方法和Springboot集成Redis方法,可以去搜索相关文章或参考该文章http://www.cnblogs.com/mengmeng89012/p/5519698.html。

当做用户权限管理时,一般都设置一个session过期时间,以确保用户长时间不操作时自动退出系统。

Spring seesions的原理可以参考该文章:http://blog.csdn.net/wojiaolinaaa/article/details/62424642

在springboot中设置該值的方法如下:

  1.  
    @Configuration
  2.  
    @EnableRedisHttpSession(maxInactiveIntervalInSeconds= 3600,redisNamespace = "tl")
  3.  
    public class RedisSessionConfig {
  4.  
     
  5.  
    }

即开启Redis共享sessions的配置类的注解设置maxInactiveIntervalInSeconds的值,单位为秒,默认值为1800秒;问题来了,当设置其为600秒时,在redis的客户端查看sessions的key的过期时间时(redis命令:ttl key,查看其有效时间),发现該值为1100秒;有问题有疑惑找源码,集成redis的代码在spring-sessions包中,找到

org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession注解,该注解的解析类为:
org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;可以看到
maxInactiveIntervalInSeconds默认值为1800秒,找到setImportMetadata函数:

  1.  
    public void setImportMetadata(AnnotationMetadata importMetadata) {
  2.  
     
  3.  
    Map<String, Object> enableAttrMap = importMetadata
  4.  
    .getAnnotationAttributes(EnableRedisHttpSession.class.getName());
  5.  
    AnnotationAttributes enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
  6.  
    /**
  7.  
    *解析注解,获取maxInactiveIntervalInSeconds赋值给配置类的maxInactiveIntervalInSeconds对象
  8.  
    */
  9.  
    this.maxInactiveIntervalInSeconds = enableAttrs
  10.  
    .getNumber("maxInactiveIntervalInSeconds");
  11.  
    String redisNamespaceValue = enableAttrs.getString("redisNamespace");
  12.  
    /**
  13.  
    *设置命名空间
  14.  
    */
  15.  
    if (StringUtils.hasText(redisNamespaceValue)) {
  16.  
    this.redisNamespace = this.embeddedValueResolver.resolveStringValue(redisNamespaceValue);
  17.  
    }
  18.  
    this.redisFlushMode = enableAttrs.getEnum("redisFlushMode");
  19.  
    }
	利用Redis作为sessions共享仓实现类为org.springframework.session.data.redis.RedisOperationsSessionRepository
其执行session过期策略的实现类为RedisSessionExpirationPolicy,其过期的具体函数如下:
  1.  
    public void onExpirationUpdated(Long originalExpirationTimeInMilli,
  2.  
    ExpiringSession session) {
  3.  
    String keyToExpire = "expires:" + session.getId();
  4.  
    long toExpire = roundUpToNextMinute(expiresInMillis(session));
  5.  
     
  6.  
    if (originalExpirationTimeInMilli != null) {
  7.  
    long originalRoundedUp = roundUpToNextMinute(originalExpirationTimeInMilli);
  8.  
    if (toExpire != originalRoundedUp) {
  9.  
    String expireKey = getExpirationKey(originalRoundedUp);
  10.  
    this.redis.boundSetOps(expireKey).remove(keyToExpire);
  11.  
    }
  12.  
    }
  13.  
     
  14.  
    long sessionExpireInSeconds = session.getMaxInactiveIntervalInSeconds();
  15.  
    String sessionKey = getSessionKey(keyToExpire);
  16.  
     
  17.  
    if (sessionExpireInSeconds < 0) {
  18.  
    this.redis.boundValueOps(sessionKey).append("");
  19.  
    this.redis.boundValueOps(sessionKey).persist();
  20.  
    this.redis.boundHashOps(getSessionKey(session.getId())).persist();
  21.  
    return;
  22.  
    }
  23.  
     
  24.  
    String expireKey = getExpirationKey(toExpire);
  25.  
    BoundSetOperations<Object, Object> expireOperations = this.redis
  26.  
    .boundSetOps(expireKey);
  27.  
    expireOperations.add(keyToExpire);
  28.  
    /**
  29.  
    *关键在此处,在设置的基础上增加了5分钟
  30.  
    /
  31.  
    long fiveMinutesAfterExpires = sessionExpireInSeconds
  32.  
    + TimeUnit.MINUTES.toSeconds(5);
  33.  
     
  34.  
    expireOperations.expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
  35.  
    if (sessionExpireInSeconds == 0) {
  36.  
    this.redis.delete(sessionKey);
  37.  
    }
  38.  
    else {
  39.  
    this.redis.boundValueOps(sessionKey).append("");
  40.  
    this.redis.boundValueOps(sessionKey).expire(sessionExpireInSeconds,
  41.  
    TimeUnit.SECONDS);
  42.  
    }
  43.  
    this.redis.boundHashOps(getSessionKey(session.getId()))
  44.  
    .expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
  45.  
    }
  46.  

Spring boot集成Redis实现sessions共享时,sessions过期时间问题分析的更多相关文章

  1. spring boot集成redis实现session共享

    1.pom文件依赖 <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springframe ...

  2. Spring Boot 2.X(六):Spring Boot 集成Redis

    Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcac ...

  3. SpringBoot(十一): Spring Boot集成Redis

    1.在 pom.xml 中配置相关的 jar 依赖: <!-- 加载 spring boot redis 包 --> <dependency> <groupId>o ...

  4. spring boot集成redis基础入门

    redis 支持持久化数据,不仅支持key-value类型的数据,还拥有list,set,zset,hash等数据结构的存储. 可以进行master-slave模式的数据备份 更多redis相关文档请 ...

  5. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

  6. 【spring boot】【redis】spring boot 集成redis的发布订阅机制

    一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...

  7. spring boot 集成 redis lettuce

    一.简介 spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端,两种客户端的区别如下 # Jedis和L ...

  8. spring boot 集成 redis lettuce(jedis)

    spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端 引入依赖 <!-- spring boot ...

  9. Spring Boot集成Redis集群(Cluster模式)

    目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...

随机推荐

  1. CSP2020游记

    初赛 这次考试完全没准备好啊-- Day0 (10.10) 本来打算看看初赛的内容 然后因为各种原因咕了-- 就做了一下洛谷的模拟卷 结果 \(40 \text{min}\) 得 \(80 \text ...

  2. Automation Framework Design 自动化框架设计思想

    从2007年到2017年,十年内自动化测试工具层出不穷,各种工具在运用一段时间之后,各个公司都会有测试架构师对于目前的自动化测试工具进行框架定制设计. 从惠普2007年GDCC推出的的WebDrivi ...

  3. 建立第一个SCRAPY的具体过程

    1.安装SCRAPY2.进入CMD:执行:SCRAPY显示: Scrapy 1.8.0 - no active project Usage: scrapy <command> [optio ...

  4. Java基础00-形参和返回值22

    1. 形参和返回值 1.1 类名作为形参和返回值 1.2 抽象类名作为形参和返回值 代码示例: 方法的形参是抽象类名 抽象动物类:定义了一个抽象的eat方法 动物的操作类:创建一个useAnimal方 ...

  5. Git初始化本地已有项目

    1.初始化仓库 git init 2.remote git remote add origin 仓库地址 3.从远程分支拉取master分支并与本地master分支合并 git pull origin ...

  6. 如何热更新长缓存的 HTTP 资源

    前言 HTTP 缓存时间一直让开发者头疼.时间太短,性能不够好:时间太长,更新不及时.当遇到严重问题需紧急修复时,尽管后端文件可快速替换,但前端文件仍从本地缓存加载,导致更新长时间无法生效. 对于这个 ...

  7. facade层,service 层,domain层,dao 层设计

    转自http://fei-6666.iteye.com/blog/446247,记录下来 一,Service->DAO,只能在Service中注入DAO. 二,DAO只能操作但表数据,跨表操作放 ...

  8. [考试总结]noip模拟20

    第五场,再挂分就没了.. 然后就没了.. 考场上一直想方法. 似乎想到了 \(T1\) 正解. 然而几个 \(k\) 的调试信息都让我迷失了自我. 然后有几句啥都没用的语句加在了上面.. 挂分... ...

  9. js中==和===的区别以及总结

    js中==和===的区别以及总结 学习js时我们会遇到 == 和 === 两种符号,现做总结如下 两种符号的定义 "==" 叫做相等运算符 "===" 叫做严格 ...

  10. AAAI 2021 最佳论文公布

    ​ 作者:Synced 翻译:仿佛若有光 第三十五届 AAAI 人工智能会议 (AAAI-21) 以虚拟会议的形式拉开帷幕.组委会在开幕式上公布了最佳论文奖和亚军.三篇论文获得了最佳论文奖,三篇被评为 ...