KEYCLOAK-1240 Fix stale cache issue

This commit is contained in:
mposolda 2015-04-28 19:24:05 +02:00
parent 4d288f1ffc
commit 553f8a6f27
2 changed files with 25 additions and 3 deletions

View file

@ -15,6 +15,7 @@ import org.keycloak.models.cache.entities.CachedRole;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -196,8 +197,16 @@ public class DefaultCacheRealmProvider implements CacheRealmProvider {
@Override
public List<RealmModel> getRealms() {
// we don't cache this for now
return getDelegate().getRealms();
// Retrieve realms from backend
List<RealmModel> backendRealms = getDelegate().getRealms();
// Return cache delegates to ensure cache invalidated during write operations
List<RealmModel> cachedRealms = new LinkedList<RealmModel>();
for (RealmModel realm : backendRealms) {
RealmModel cached = session.realms().getRealm(realm.getId());
cachedRealms.add(cached);
}
return cachedRealms;
}
@Override

View file

@ -1,5 +1,7 @@
package org.keycloak.testsuite.model;
import java.util.List;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
@ -32,7 +34,17 @@ public class CacheTest {
{
// update realm, then get an AppModel and change it. The AppModel would not be a cache adapter
KeycloakSession session = kc.startSession();
RealmModel realm = session.realms().getRealmByName("test");
// KEYCLOAK-1240 - obtain the realm via session.realms().getRealms()
RealmModel realm = null;
List<RealmModel> realms = session.realms().getRealms();
for (RealmModel current : realms) {
if ("test".equals(current.getName())) {
realm = current;
break;
}
}
Assert.assertTrue(realm instanceof org.keycloak.models.cache.RealmAdapter);
realm.setAccessCodeLifespanLogin(200);
ClientModel testApp = realm.getClientByClientId("test-app");
@ -44,6 +56,7 @@ public class CacheTest {
{
KeycloakSession session = kc.startSession();
RealmModel realm = session.realms().getRealmByName("test");
Assert.assertEquals(200, realm.getAccessCodeLifespanLogin());
ClientModel testApp = session.realms().getClientById(appId, realm);
Assert.assertFalse(testApp.isEnabled());
kc.stopSession(session, true);