Skip to content
Snippets Groups Projects
Commit 2acf3bfc authored by thorben.betten's avatar thorben.betten
Browse files

Prefer a local cache for too frequently queried user data - /appsuite/platform/core#215

(cherry picked from commit 16b2c51f)
parent 41073633
No related branches found
No related tags found
No related merge requests found
...@@ -215,7 +215,7 @@ public class LocalCachingContextStorage extends ContextStorage { ...@@ -215,7 +215,7 @@ public class LocalCachingContextStorage extends ContextStorage {
Integer contextId = contextIdCache.getIfPresent(loginInfo); Integer contextId = contextIdCache.getIfPresent(loginInfo);
if (contextId == null) { if (contextId == null) {
try { try {
contextId = contextIdCache.get(loginInfo, this::loadContextByLoginInfo); contextId = contextIdCache.get(loginInfo, l0ginInfo -> loadContextByLoginInfo(l0ginInfo, false));
} catch (OXRuntimeException e) { // NOSONARLINT } catch (OXRuntimeException e) { // NOSONARLINT
throw e.getException(); throw e.getException();
} }
...@@ -227,10 +227,11 @@ public class LocalCachingContextStorage extends ContextStorage { ...@@ -227,10 +227,11 @@ public class LocalCachingContextStorage extends ContextStorage {
* Loads the context by given login info. * Loads the context by given login info.
* *
* @param loginInfo The login info to load by * @param loginInfo The login info to load by
* @param populateContextCache <code>true</code> to populate context cache; otherwise <code>false</code>
* @return The identifier of the associated context or {@link ContextStorage#NOT_FOUND} if there is none * @return The identifier of the associated context or {@link ContextStorage#NOT_FOUND} if there is none
* @throws OXRuntimeException If retrieval fails * @throws OXRuntimeException If retrieval fails
*/ */
private Integer loadContextByLoginInfo(String loginInfo) { private Integer loadContextByLoginInfo(String loginInfo, boolean populateContextCache) {
try { try {
ContextExtended context = persistentImpl.getContext(loginInfo); ContextExtended context = persistentImpl.getContext(loginInfo);
if (null == context) { if (null == context) {
...@@ -238,7 +239,9 @@ public class LocalCachingContextStorage extends ContextStorage { ...@@ -238,7 +239,9 @@ public class LocalCachingContextStorage extends ContextStorage {
} }
triggerUpdate(context, UpdateBehavior.NONE); triggerUpdate(context, UpdateBehavior.NONE);
Integer contextId = I(context.getContextId()); Integer contextId = I(context.getContextId());
contextCache.put(contextId, context); if (populateContextCache) {
contextCache.put(contextId, context);
}
return contextId; return contextId;
} catch (OXException e) { } catch (OXException e) {
throw new OXRuntimeException(e); throw new OXRuntimeException(e);
...@@ -250,7 +253,7 @@ public class LocalCachingContextStorage extends ContextStorage { ...@@ -250,7 +253,7 @@ public class LocalCachingContextStorage extends ContextStorage {
ContextExtended context = contextCache.getIfPresent(I(contextId)); ContextExtended context = contextCache.getIfPresent(I(contextId));
if (context == null) { if (context == null) {
try { try {
context = contextCache.get(I(contextId), cid -> loadContextById(cid.intValue(), updateBehavior)); context = contextCache.get(I(contextId), cid -> loadContextById(cid.intValue(), updateBehavior, false));
} catch (OXRuntimeException e) { // NOSONARLINT } catch (OXRuntimeException e) { // NOSONARLINT
throw e.getException(); throw e.getException();
} }
...@@ -262,15 +265,16 @@ public class LocalCachingContextStorage extends ContextStorage { ...@@ -262,15 +265,16 @@ public class LocalCachingContextStorage extends ContextStorage {
* Loads the context by given identifier. * Loads the context by given identifier.
* *
* @param contextId The context identifier * @param contextId The context identifier
* @param updateBehavior The update behavior to obey * @param updateBehavior The update behavior to obey contextIdCache
* @param populateContextIdCache <code>true</code> to populate login-info-2-context-identifier cache; otherwise <code>false</code>
* @return The context * @return The context
* @throws OXRuntimeException If retrieval fails * @throws OXRuntimeException If retrieval fails
*/ */
private ContextExtended loadContextById(int contextId, UpdateBehavior updateBehavior) { private ContextExtended loadContextById(int contextId, UpdateBehavior updateBehavior, boolean populateContextIdCache) {
try { try {
ContextExtended context = persistentImpl.loadContext(contextId); ContextExtended context = persistentImpl.loadContext(contextId);
triggerUpdate(context, updateBehavior); triggerUpdate(context, updateBehavior);
String[] loginInfo = context.getLoginInfo(); String[] loginInfo = populateContextIdCache ? context.getLoginInfo() : null;
if (loginInfo != null) { if (loginInfo != null) {
Integer iContextId = I(contextId); Integer iContextId = I(contextId);
for (String li : loginInfo) { for (String li : loginInfo) {
......
...@@ -212,7 +212,7 @@ public class LocalCachingUserStorage extends UserStorage { ...@@ -212,7 +212,7 @@ public class LocalCachingUserStorage extends UserStorage {
CachedUser user = userCache.getIfPresent(key); CachedUser user = userCache.getIfPresent(key);
if (user == null) { if (user == null) {
try { try {
user = userCache.get(key, this::loadUserById); user = userCache.get(key, k -> loadUserById(key, false));
} catch (OXRuntimeException e) { // NOSONARLINT } catch (OXRuntimeException e) { // NOSONARLINT
throw e.getException(); throw e.getException();
} }
...@@ -224,13 +224,14 @@ public class LocalCachingUserStorage extends UserStorage { ...@@ -224,13 +224,14 @@ public class LocalCachingUserStorage extends UserStorage {
* Loads the user by given identifier. * Loads the user by given identifier.
* *
* @param uac The user and context identifier * @param uac The user and context identifier
* @param populateLoginCache <code>true</code> to populate login-2-user cache; otherwise <code>false</code>
* @return The loaded user * @return The loaded user
* @throws OXRuntimeException If retrieval fails * @throws OXRuntimeException If retrieval fails
*/ */
private CachedUser loadUserById(UserAndContext uac) { private CachedUser loadUserById(UserAndContext uac, boolean populateLoginCache) {
try { try {
User user = delegate.getUser(uac.getUserId(), uac.getContextId()); User user = delegate.getUser(uac.getUserId(), uac.getContextId());
String loginInfo = user.getLoginInfo(); String loginInfo = populateLoginCache ? user.getLoginInfo() : null;
if (loginInfo != null) { if (loginInfo != null) {
logins2UserCache.put(new ContextIdAndString(uac.getContextId(), loginInfo), I(user.getId())); logins2UserCache.put(new ContextIdAndString(uac.getContextId(), loginInfo), I(user.getId()));
} }
...@@ -246,7 +247,7 @@ public class LocalCachingUserStorage extends UserStorage { ...@@ -246,7 +247,7 @@ public class LocalCachingUserStorage extends UserStorage {
CachedUser user = userCache.getIfPresent(key); CachedUser user = userCache.getIfPresent(key);
if (user == null) { if (user == null) {
try { try {
user = userCache.get(key, uac -> loadUserById(uac, context, con)); user = userCache.get(key, uac -> loadUserById(uac, context, false, con));
} catch (OXRuntimeException e) { // NOSONARLINT } catch (OXRuntimeException e) { // NOSONARLINT
throw e.getException(); throw e.getException();
} }
...@@ -259,14 +260,15 @@ public class LocalCachingUserStorage extends UserStorage { ...@@ -259,14 +260,15 @@ public class LocalCachingUserStorage extends UserStorage {
* *
* @param uac The user and context identifier * @param uac The user and context identifier
* @param context The given context * @param context The given context
* @param populateLoginCache <code>true</code> to populate login-2-user cache; otherwise <code>false</code>
* @param con The connection to use * @param con The connection to use
* @return The loaded user * @return The loaded user
* @throws OXRuntimeException If retrieval fails * @throws OXRuntimeException If retrieval fails
*/ */
private CachedUser loadUserById(UserAndContext uac, Context context, Connection con) { private CachedUser loadUserById(UserAndContext uac, Context context, boolean populateLoginCache, Connection con) {
try { try {
User user = delegate.loadIfAbsent(uac.getUserId(), getEffectiveContext(uac.getContextId(), context), con); User user = delegate.loadIfAbsent(uac.getUserId(), getEffectiveContext(uac.getContextId(), context), con);
String loginInfo = user.getLoginInfo(); String loginInfo = populateLoginCache ? user.getLoginInfo() : null;
if (loginInfo != null) { if (loginInfo != null) {
logins2UserCache.put(new ContextIdAndString(uac.getContextId(), loginInfo), I(user.getId())); logins2UserCache.put(new ContextIdAndString(uac.getContextId(), loginInfo), I(user.getId()));
} }
...@@ -303,7 +305,7 @@ public class LocalCachingUserStorage extends UserStorage { ...@@ -303,7 +305,7 @@ public class LocalCachingUserStorage extends UserStorage {
CachedUser user = userCache.getIfPresent(key); CachedUser user = userCache.getIfPresent(key);
if (user == null) { if (user == null) {
try { try {
user = userCache.get(key, this::loadUserById); user = userCache.get(key, k -> loadUserById(k, false));
} catch (OXRuntimeException e) { // NOSONARLINT } catch (OXRuntimeException e) { // NOSONARLINT
throw e.getException(); throw e.getException();
} }
...@@ -317,7 +319,7 @@ public class LocalCachingUserStorage extends UserStorage { ...@@ -317,7 +319,7 @@ public class LocalCachingUserStorage extends UserStorage {
CachedUser user = userCache.getIfPresent(key); CachedUser user = userCache.getIfPresent(key);
if (user == null) { if (user == null) {
try { try {
user = userCache.get(key, uac -> loadUserById2(uac, ctx, con)); user = userCache.get(key, uac -> loadUserById2(uac, ctx, false, con));
} catch (OXRuntimeException e) { // NOSONARLINT } catch (OXRuntimeException e) { // NOSONARLINT
throw e.getException(); throw e.getException();
} }
...@@ -330,14 +332,15 @@ public class LocalCachingUserStorage extends UserStorage { ...@@ -330,14 +332,15 @@ public class LocalCachingUserStorage extends UserStorage {
* *
* @param uac The user and context identifier * @param uac The user and context identifier
* @param context The given context * @param context The given context
* @param populateLoginCache <code>true</code> to populate login-2-user cache; otherwise <code>false</code>
* @param con The connection to use * @param con The connection to use
* @return The loaded user * @return The loaded user
* @throws OXRuntimeException If retrieval fails * @throws OXRuntimeException If retrieval fails
*/ */
private CachedUser loadUserById2(UserAndContext uac, Context context, Connection con) { private CachedUser loadUserById2(UserAndContext uac, Context context, boolean populateLoginCache, Connection con) {
try { try {
User user = delegate.getUser(getEffectiveContext(uac.getContextId(), context), uac.getUserId(), con); User user = delegate.getUser(getEffectiveContext(uac.getContextId(), context), uac.getUserId(), con);
String loginInfo = user.getLoginInfo(); String loginInfo = populateLoginCache ? user.getLoginInfo() : null;
if (loginInfo != null) { if (loginInfo != null) {
logins2UserCache.put(new ContextIdAndString(uac.getContextId(), loginInfo), I(user.getId())); logins2UserCache.put(new ContextIdAndString(uac.getContextId(), loginInfo), I(user.getId()));
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment