SpringSession整合Redis问题记录(一)

一、背景描述

    1、最近在SpringSession整合Redis的过程中,当session.setAttribute(LOGIN_USER,entity),存入一个实体类对象的时候,这个时候另外一个服务调用request.getSession().getAttribute(key),此时的key和set进去的LOGIN_USERkey不一致。调用的时发现取值的时候报序列化entity这个实体类不存在,当前调用的服务确实没有这个实体类,但是取值的这个key对应的json对象也不是对应这个实体类,这个时候想到是不是sesionn在getAttribute前把所有的值已经取到了。通过查询源码发现

private RedisSession getSession(String id, boolean allowExpired) {
   Map<Object, Object> entries = getSessionBoundHashOperations(id).entries();
   if (entries.isEmpty()) {
      return null;
   }
   MapSession loaded = loadSession(id, entries);
   if (!allowExpired && loaded.isExpired()) {
      return null;
   }
   RedisSession result = new RedisSession(loaded, false);
   result.originalLastAccessTime = loaded.getLastAccessedTime();
   return result;
}

在getsession的时候已经把当前HASH对应键下的所有值取到存在mapsession里面,

查看getAttribute方法

@Override
public <T> T getAttribute(String attributeName) {
   T attributeValue = this.cached.getAttribute(attributeName);
   if (attributeValue != null
         && RedisIndexedSessionRepository.this.saveMode.equals(SaveMode.ON_GET_ATTRIBUTE)) {
      this.delta.put(getSessionAttrNameKey(attributeName), attributeValue);
   }
   return attributeValue;
}

发现取值是存mapsession取值,这个时候就明白了。先通过redis把对应键下的所有值取到放在map里面,然后取值的时候根据key去匹配。减少对redis的调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值