requestSessionCached 是做什么的,什么情况下会变成 true
我一直在学习SpringSession源代码,我发现在SessionRepositoryRequestWrapper类中包含一个布尔值:RequestedSessionCached,它用于在getRequestedSession方法中判断会话是否被缓存。但是每次发送请求调试源码总是为false,那么requestedSessionCached到底是做什么的,什么情况下会变成true呢?
private final class SessionRepositoryRequestWrapper extends HttpServletRequestWrapper {
private final HttpServletResponse response;
private S requestedSession;
private boolean requestedSessionCached;
private String requestedSessionId;
private Boolean requestedSessionIdValid;
private boolean requestedSessionInvalidated;
}
private S getRequestedSession() {
if (!this.requestedSessionCached) {
List<String> sessionIds = SessionRepositoryFilter.this.httpSessionIdResolver.resolveSessionIds(this);
for (String sessionId : sessionIds) {
if (this.requestedSessionId == null) {
this.requestedSessionId = sessionId;
}
S session = SessionRepositoryFilter.this.sessionRepository.findById(sessionId);
if (session != null) {
this.requestedSession = session;
this.requestedSessionId = sessionId;
break;
}
}
this.requestedSessionCached = true;
}
return this.requestedSession;
}
I've been learning on SpringSession source code, I found that in SessionRepositoryRequestWrapper class contains a Boolean value: RequestedSessionCached, which is used in the getRequestedSession method to determine whether the session is cached. However, it is always false every time I send a request to debug the source code, so what does requestedSessionCached really do, and under what circumstances does it become true?
private final class SessionRepositoryRequestWrapper extends HttpServletRequestWrapper {
private final HttpServletResponse response;
private S requestedSession;
private boolean requestedSessionCached;
private String requestedSessionId;
private Boolean requestedSessionIdValid;
private boolean requestedSessionInvalidated;
}
private S getRequestedSession() {
if (!this.requestedSessionCached) {
List<String> sessionIds = SessionRepositoryFilter.this.httpSessionIdResolver.resolveSessionIds(this);
for (String sessionId : sessionIds) {
if (this.requestedSessionId == null) {
this.requestedSessionId = sessionId;
}
S session = SessionRepositoryFilter.this.sessionRepository.findById(sessionId);
if (session != null) {
this.requestedSession = session;
this.requestedSessionId = sessionId;
break;
}
}
this.requestedSessionCached = true;
}
return this.requestedSession;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是针对在处理请求期间多次调用
getRequestedSession()
的情况进行的优化。它将立即返回找到的会话,而不是在会话存储库中再次查找它,以节省时间。
也许您当前的设置仅调用它一次,但如果您将自定义过滤器添加到处理链或从端点访问会话,情况可能会发生变化。
This is an optimization for the case that
getRequestedSession()
gets called more than one time during processing the request.It will return the found session immediately instead of looking it up again in the session repository in order to save time.
Maybe your current setup calls it only once but this may change if you add custom filters to the processing chain or access the session from an endpoint.