# RedisCachedSession **Repository Path**: Esmool/RedisCachedSession ## Basic Information - **Project Name**: RedisCachedSession - **Description**: JFinal RedisCachedSession 支持组件 - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-04-12 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # JFinal RedisCachedSession 支持组件 RedisCachedSession组件用于支持JFinal集群化作业,负责将tomcat原生Session转移到Redis服务器中,以实现JFinal的无状态化改造. ## 使用环境 ### 运行环境 - JDK 1.8 - tomcat 8.0 ### 依赖清单 - commons-pool2-2.3 - fastjson-1.2.6 - fst-2.29 - jackson-core-2.5.3 - javassist-3.19.0-GA - jedis-2.7.2 - jfinal-2.2 - objenesis-2.1 ## 内容 - RedisCachedSession 在Redis中缓存的Session对象 - RedisCachedSessionController 支持使用RedisCachedSession的控制器对象 - RedisCachedSessionInterceptor 支持使用RedisCachedSession的全局拦截器 ## 用法 1. 在项目中引用RedisCachedSession包; 2. 在JFinal的配置文件 ? extends JFinalConfig类中添加RedisCachedSession的常量配置,例如: ``` @Override public void configConstant(Constants me) { RedisCachedSession.REDIS_SESSION_DB_NAME = "testSessionCache"; // Redis中的会话缓存数据库 RedisCachedSession.SESSION_TIMEOUT = 120000; // 会话超时时间,单位ms } ``` 3. 在JFinal的配置文件 ? extends JFinalConfig 类中添加RedisPlugin的配置,例如: ``` @Override public void configPlugin(Plugins me) { RedisPlugin sessionRedis = new RedisPlugin(RedisCachedSession.REDIS_SESSION_DB_NAME,"localhost"); me.add(sessionRedis); } ``` 4. 在JFinal的配置文件 ? extends JFinalConfig 类中添加全局拦截器RedisCachedSessionInterceptor的配置,例如: ``` @Override public void configInterceptor(Interceptors me) { me.add(new RedisCachedSessionInterceptor()); } ``` 5. 将需要设置为无状态化的控制器声明为派生自RedisCachedSessionController,例如: ``` public class IndexController extends RedisCachedSessionController { ``` 6. 如同往常使用getSession(),getSessionAttr(),setSessionAttr(),removeSessionAttr()方法. ## 注意 1. 若要使用tomcat原有session,请使用RedisCachedSession.getLocalSession()方法获取; 2. RedisCachedSession.getServletContext()和RedisCachedSession.getSessionContext()方法直接传递tomcat原有session的对应结果,因此以下的调用完全等价于注释中的调用 ``` class SomeController extends RedisCachedSessionController { // ... public void some_method() { // ... // 等价于 ServletContext servletContext = this.getSession().getLocalSession().getServletContext(); ServletContext servletContet = this.getSession().getServletContext(); // 等价于 HttpSessionContext sessionContext = this.getSession().getLocalSession().getSessionContext(); HttpSessionContext sessionContext = this.getSession().getSessionContext(); // ... } // ... } ``` 3. 若JFinalConfig的常量配置中的setDevMode()设置为true,在运行时命令行中将打印Session同Redis的交换情况. ## 工作过程 RedisCachedSession基本工作过程为: 1. 由RedisCachedSessionInterceptor拦截进入的客户端请求。检查是否存在RedisCachedSession.REDIS_SESSION_DB_NAME所指定的cookie项目。若没有,则新建一个uuid存入cookie中,以此作为全局session的id,若有则以该cookie项的值作为全局的session的id; 2. 之后实例化RedisCachedSession对象,使之连接到RedisPlugin所指定的Redis服务器上,使用 RedisCachedSession.REDIS_SESSION_DB_NAME所指定的数据库作为主库; 3. 使用之前确定的全局session的id作为key,从Redis中加载session对象的内容,装入之前实例化的RedisCachedSession对象中. 若Redis中不存在该id作为key的缓存,则将新建一个空白的并存入redis后再装入。装入数据后,将会刷新redis中对应key的时间戳,在RedisCachedSession.SESSION_TIMEOUT时间(单位:毫秒)后过期; 4. RedisCachedSessionController重写了Controller中对Session的读写动作,将其转调到RedisCachedSession之上,同时将原本tomcat内置session对象的ServletContext和SessionContext保持原样传递给调用者; 5. RedisCachedSessionController响应动作完成后,由RedisCachedSessionInterceptor调用RedisCachedSession.save()方法将更新后的session重新放入缓存,并刷新时间戳。