spning-session-data-redis解决session共享的问题

系统要做到用户友好,需要对用户的session进行存储,存储的方式有以下几种: 本地缓存 数据库 文件 缓存服务器 可以看一些不同方案的优缺点 1.本地机器或者本地缓存。优点:速度快 缺点:服务宕机后重启用户信息丢失,用户不优好 2.数据库。优点:技术栈简单 缺点:速度慢 3.文件。优点:技术栈简单,速度适中 缺点:无灾备或者灾备方案成本高 4.缓存服务器。一般是内存服务器,优点:速度快 可以和原有技术栈契合,有现成的解决方案。缺点:不明显 如果使用java语言,并且缓存服务器为redis,可以使用开源的spring session项目来解决。 spring session项目现有三个自项目,分别是 spring-session-data-redis 使用redis方式 spring-session-hazelcast 使用hazelcast方式 spring-session-jdbc 使用jdbc方式 在这里我建议大家使用redis方式,它提供了注解式和编程式不同的方法。具体如何使用,网上有很多实例,我就不赘述。我想和大家一起深入内部看一下,spring-session项目的github地址为:https://github.com/spring-projects/spring-session.git 我们只看spring-session-data-redis,实现非常简单。它总共只有12个类 核心类只有一个 RedisOperationsSessionRepository 这个类内部定义了session的实现 RedisSession /** * A custom implementation of {@link Session} that uses a {@link MapSession} as the * basis for its mapping. It keeps track of any attributes that have changed. When * {@link org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession#saveDelta()} * is invoked all the attributes that have been changed will be persisted. * * @author Rob Winch * @since 1.0 */ final class RedisSession implements Session { private final MapSession cached; private Instant originalLastAccessTime; private Map delta = new HashMap<>(); private boolean isNew; private String originalPrincipalName; private String originalSessionId; 注意: delta 是增量 cached是存量 我们来看这个RedisSession的创建 销毁及修改 RedisSession内部存储如下(示例) *
* HMSET spring:session:sessions:33fdd1b6-b496-4b33-9f7d-df96679d32fe creationTime 1404360000000 maxInactiveInterval 1800 lastAccessedTime 1404360000000 sessionAttr:attrName someAttrValue sessionAttr2:attrName someAttrValue2
* EXPIRE spring:session:sessions:33fdd1b6-b496-4b33-9f7d-df96679d32fe 2100
* APPEND spring:session:sessions:expires:33fdd1b6-b496-4b33-9f7d-df96679d32fe ""
* EXPIRE spring:session:sessions:expires:33fdd1b6-b496-4b33-9f7d-df96679d32fe 1800
* SADD spring:session:expirations:1439245080000 expires:33fdd1b6-b496-4b33-9f7d-df96679d32fe
* EXPIRE spring:session:expirations1439245080000 2100
* 
RedisSession的数据结构是Hash *

* Each session is stored in Redis as a * Hash. Each session is set and * updated using the HMSET command. An * example of how each session is stored can be seen below. *

RedisSession的失效 *

Expiration

* *

* An expiration is associated to each session using the * EXPIRE command based upon the * {@link org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession#getMaxInactiveInterval()} * . For example: *

RedisSession的更新有一个比较重要的方法: /** * Saves any attributes that have been changed and updates the expiration of this * session. */ private void saveDelta() { String sessionId = getId(); saveChangeSessionId(sessionId); if (this.delta.isEmpty()) { return; } getSessionBoundHashOperations(sessionId).putAll(this.delta); String principalSessionKey = getSessionAttrNameKey( FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME); String securityPrincipalSessionKey = getSessionAttrNameKey( SPRING_SECURITY_CONTEXT); if (this.delta.containsKey(principalSessionKey) || this.delta.containsKey(securityPrincipalSessionKey)) { if (this.originalPrincipalName != null) { String originalPrincipalRedisKey = getPrincipalKey( this.originalPrincipalName); RedisOperationsSessionRepository.this.sessionRedisOperations .boundSetOps(originalPrincipalRedisKey).remove(sessionId); } String principal = PRINCIPAL_NAME_RESOLVER.resolvePrincipal(this); this.originalPrincipalName = principal; if (principal != null) { String principalRedisKey = getPrincipalKey(principal); RedisOperationsSessionRepository.this.sessionRedisOperations .boundSetOps(principalRedisKey).add(sessionId); } } this.delta = new HashMap<>(this.delta.size()); Long originalExpiration = (this.originalLastAccessTime != null) ? this.originalLastAccessTime.plus(getMaxInactiveInterval()) .toEpochMilli() : null; RedisOperationsSessionRepository.this.expirationPolicy .onExpirationUpdated(originalExpiration, this); } 小结: 1.session是键值对形式的,对应redis的数据结构hash 2.session的存储形式使用redis非常方便 分类: spring及其源码分析专辑https://www.cnblogs.com/davidwang456/p/10172841.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信