From ee6753d31b31abbba469ef4ba913f046ddd1f1e8 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Wed, 2 Dec 2015 11:23:21 +0100 Subject: [PATCH] KEYCLOAK-1813 KEYCLOAK-2182 Fixes enable/disable cache --- .../admin/messages/admin-messages_en.properties | 4 ++-- .../InfinispanCacheRealmProviderFactory.java | 17 +++++++++++++++-- .../InfinispanCacheUserProviderFactory.java | 2 +- .../cache/infinispan/InfinispanRealmCache.java | 5 +++-- .../cache/infinispan/InfinispanUserCache.java | 5 +++-- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/forms/common-themes/src/main/resources/theme/base/admin/messages/admin-messages_en.properties b/forms/common-themes/src/main/resources/theme/base/admin/messages/admin-messages_en.properties index b92f65bf1f..eca236fc13 100755 --- a/forms/common-themes/src/main/resources/theme/base/admin/messages/admin-messages_en.properties +++ b/forms/common-themes/src/main/resources/theme/base/admin/messages/admin-messages_en.properties @@ -63,9 +63,9 @@ supported-locales=Supported Locales supported-locales.placeholder=Type a locale and enter default-locale=Default Locale realm-cache-enabled=Realm Cache Enabled -realm-cache-enabled.tooltip=Enable/disable cache for realm, client and role data. +realm-cache-enabled.tooltip=Enable/disable cache for realms, clients and roles. user-cache-enabled=User Cache Enabled -user-cache-enabled.tooltip=Enable/disable user and user role mapping cache. +user-cache-enabled.tooltip=Enable/disable cache for users and user role mappings. revoke-refresh-token=Revoke Refresh Token revoke-refresh-token.tooltip=If enabled refresh tokens can only be used once. Otherwise refresh tokens are not revoked when used and can be used multiple times. sso-session-idle=SSO Session Idle diff --git a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheRealmProviderFactory.java b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheRealmProviderFactory.java index 4b96241fbe..38fddbd4a8 100755 --- a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheRealmProviderFactory.java +++ b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheRealmProviderFactory.java @@ -8,6 +8,7 @@ import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.cache.CacheRealmProvider; import org.keycloak.models.cache.CacheRealmProviderFactory; import org.keycloak.models.cache.RealmCache; +import org.keycloak.models.cache.entities.CachedUser; import java.util.concurrent.ConcurrentHashMap; @@ -19,13 +20,25 @@ public class InfinispanCacheRealmProviderFactory implements CacheRealmProviderFa protected final ConcurrentHashMap realmLookup = new ConcurrentHashMap(); + protected volatile InfinispanRealmCache realmCache; + @Override public CacheRealmProvider create(KeycloakSession session) { - Cache cache = session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.REALM_CACHE_NAME); - RealmCache realmCache = new InfinispanRealmCache(cache, realmLookup); + lazyInit(session); return new DefaultCacheRealmProvider(realmCache, session); } + private void lazyInit(KeycloakSession session) { + if (realmCache == null) { + synchronized (this) { + if (realmCache == null) { + Cache cache = session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.REALM_CACHE_NAME); + realmCache = new InfinispanRealmCache(cache, realmLookup); + } + } + } + } + @Override public void init(Config.Scope config) { } diff --git a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheUserProviderFactory.java b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheUserProviderFactory.java index 4eb0bdf8ee..ec699127c4 100755 --- a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheUserProviderFactory.java +++ b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanCacheUserProviderFactory.java @@ -26,7 +26,7 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact private static final Logger log = Logger.getLogger(InfinispanCacheUserProviderFactory.class); - protected InfinispanUserCache userCache; + protected volatile InfinispanUserCache userCache; protected final RealmLookup usernameLookup = new RealmLookup(); diff --git a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanRealmCache.java b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanRealmCache.java index 3bb9e5ae64..845836965d 100755 --- a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanRealmCache.java +++ b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanRealmCache.java @@ -38,9 +38,10 @@ public class InfinispanRealmCache implements RealmCache { @Override public void setEnabled(boolean enabled) { - clear(); + if (this.enabled && !enabled) { + clear(); + } this.enabled = enabled; - clear(); } @Override diff --git a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanUserCache.java b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanUserCache.java index 8e4e84b3e1..c5e960c0e1 100755 --- a/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanUserCache.java +++ b/model/invalidation-cache/infinispan/src/main/java/org/keycloak/models/cache/infinispan/InfinispanUserCache.java @@ -35,9 +35,10 @@ public class InfinispanUserCache implements UserCache { @Override public void setEnabled(boolean enabled) { - clear(); + if (this.enabled && !enabled) { + clear(); + } this.enabled = enabled; - clear(); } @Override