Skip to content

Commit

Permalink
fix(perf) don't create session if it is not required (#30322)
Browse files Browse the repository at this point in the history
This pull request includes changes to improve session handling in the
`CurrentVariantWebInterceptor` and `VariantWebAPIImpl` classes. The main
focus is on ensuring sessions are only created when necessary, which can
help optimize resource usage and performance.

Session handling improvements:

*
[`dotCMS/src/main/java/com/dotcms/variant/business/web/CurrentVariantWebInterceptor.java`](diffhunk://#diff-65fdb30fd885a08035709f661b6cd0e1e4f4354bde74a4f3e1f6464e765d0731L59-R59):
Modified `getSession` to avoid creating a new session if one does not
already exist by passing `false` to `getSession`.
*
[`dotCMS/src/main/java/com/dotcms/variant/business/web/VariantWebAPIImpl.java`](diffhunk://#diff-c67db85ad5a8303546f73462e9074d4cf3f669295e99d9ca050c4bd8ce25e7f2L69-R78):
Renamed `setSessionAttribute` to `setSessionAttributeIfNeeded` and
updated the method to conditionally create a session based on the
`currentVariantName`.
  • Loading branch information
wezell authored Oct 15, 2024
1 parent d5fde27 commit 10c59ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Result intercept(final HttpServletRequest request, final HttpServletRespo
}

if (!UtilMethods.isSet(currentVariantName)) {
final HttpSession session = request.getSession();
final HttpSession session = request.getSession(false);

if (session != null) {
final Object attribute = session.getAttribute(VariantAPI.VARIANT_KEY);
Expand Down Expand Up @@ -98,4 +98,4 @@ private static Optional<String> getVariantValueFromReferer(final String refererV

return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,21 @@ public String currentVariantId() {
}
}

setSessionAttribute(request, currentVariantName);
setSessionAttributeIfNeeded(request, currentVariantName);
return currentVariantName;
}

private static void setSessionAttribute(final HttpServletRequest request,
private static void setSessionAttributeIfNeeded(final HttpServletRequest request,
final String currentVariantName) {

final HttpSession session = request.getSession(true);
boolean buildSessionIfNeeded = !"DEFAULT".equals(currentVariantName);

final HttpSession session = request.getSession(buildSessionIfNeeded);

if (!UtilMethods.isSet(session)) {
return;
}

final Object attribute = session.getAttribute(VariantAPI.VARIANT_KEY);

if (mustOverwrite(attribute, currentVariantName)) {
Expand Down

0 comments on commit 10c59ee

Please sign in to comment.