Home > OS >  What does requestedSessionCached do, and under what circumstances does it become true
What does requestedSessionCached do, and under what circumstances does it become true

Time:03-20

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;
    }

CodePudding user response:

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.

  • Related