realm cache event
This commit is contained in:
parent
9c7e9da9fa
commit
36c2422fa4
5 changed files with 105 additions and 6 deletions
|
@ -36,6 +36,7 @@ import org.keycloak.models.RequiredCredentialModel;
|
||||||
import org.keycloak.models.RoleModel;
|
import org.keycloak.models.RoleModel;
|
||||||
import org.keycloak.models.UserFederationMapperModel;
|
import org.keycloak.models.UserFederationMapperModel;
|
||||||
import org.keycloak.models.UserFederationProviderModel;
|
import org.keycloak.models.UserFederationProviderModel;
|
||||||
|
import org.keycloak.models.cache.CachedRealmModel;
|
||||||
import org.keycloak.models.cache.infinispan.entities.CachedRealm;
|
import org.keycloak.models.cache.infinispan.entities.CachedRealm;
|
||||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||||
|
|
||||||
|
@ -50,12 +51,13 @@ import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||||
* @version $Revision: 1 $
|
* @version $Revision: 1 $
|
||||||
*/
|
*/
|
||||||
public class RealmAdapter implements RealmModel {
|
public class RealmAdapter implements CachedRealmModel {
|
||||||
protected CachedRealm cached;
|
protected CachedRealm cached;
|
||||||
protected RealmCacheSession cacheSession;
|
protected RealmCacheSession cacheSession;
|
||||||
protected RealmModel updated;
|
protected RealmModel updated;
|
||||||
|
@ -66,17 +68,32 @@ public class RealmAdapter implements RealmModel {
|
||||||
this.cacheSession = cacheSession;
|
this.cacheSession = cacheSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void getDelegateForUpdate() {
|
@Override
|
||||||
|
public RealmModel getDelegateForUpdate() {
|
||||||
if (updated == null) {
|
if (updated == null) {
|
||||||
cacheSession.registerRealmInvalidation(cached.getId());
|
cacheSession.registerRealmInvalidation(cached.getId());
|
||||||
updated = cacheSession.getDelegate().getRealm(cached.getId());
|
updated = cacheSession.getDelegate().getRealm(cached.getId());
|
||||||
if (updated == null) throw new IllegalStateException("Not found in database");
|
if (updated == null) throw new IllegalStateException("Not found in database");
|
||||||
}
|
}
|
||||||
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean invalidated;
|
protected boolean invalidated;
|
||||||
|
|
||||||
|
@Override
|
||||||
public void invalidate() {
|
public void invalidate() {
|
||||||
invalidated = true;
|
invalidated = true;
|
||||||
|
getDelegateForUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getCacheTimestamp() {
|
||||||
|
return cached.getCacheTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConcurrentHashMap getCachedWith() {
|
||||||
|
return cached.getCachedWith();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isUpdated() {
|
protected boolean isUpdated() {
|
||||||
|
|
|
@ -29,6 +29,7 @@ import org.keycloak.models.RealmModel;
|
||||||
import org.keycloak.models.RealmProvider;
|
import org.keycloak.models.RealmProvider;
|
||||||
import org.keycloak.models.RoleModel;
|
import org.keycloak.models.RoleModel;
|
||||||
import org.keycloak.models.cache.CacheRealmProvider;
|
import org.keycloak.models.cache.CacheRealmProvider;
|
||||||
|
import org.keycloak.models.cache.CachedRealmModel;
|
||||||
import org.keycloak.models.cache.infinispan.entities.CachedClient;
|
import org.keycloak.models.cache.infinispan.entities.CachedClient;
|
||||||
import org.keycloak.models.cache.infinispan.entities.CachedClientRole;
|
import org.keycloak.models.cache.infinispan.entities.CachedClientRole;
|
||||||
import org.keycloak.models.cache.infinispan.entities.CachedClientTemplate;
|
import org.keycloak.models.cache.infinispan.entities.CachedClientTemplate;
|
||||||
|
@ -374,6 +375,7 @@ public class RealmCacheSession implements CacheRealmProvider {
|
||||||
if (cached != null) {
|
if (cached != null) {
|
||||||
logger.tracev("by id cache hit: {0}", cached.getName());
|
logger.tracev("by id cache hit: {0}", cached.getName());
|
||||||
}
|
}
|
||||||
|
boolean wasCached = false;
|
||||||
if (cached == null) {
|
if (cached == null) {
|
||||||
Long loaded = cache.getCurrentRevision(id);
|
Long loaded = cache.getCurrentRevision(id);
|
||||||
RealmModel model = getDelegate().getRealm(id);
|
RealmModel model = getDelegate().getRealm(id);
|
||||||
|
@ -381,12 +383,27 @@ public class RealmCacheSession implements CacheRealmProvider {
|
||||||
if (invalidations.contains(id)) return model;
|
if (invalidations.contains(id)) return model;
|
||||||
cached = new CachedRealm(loaded, model);
|
cached = new CachedRealm(loaded, model);
|
||||||
cache.addRevisioned(cached, startupRevision);
|
cache.addRevisioned(cached, startupRevision);
|
||||||
|
wasCached =true;
|
||||||
} else if (invalidations.contains(id)) {
|
} else if (invalidations.contains(id)) {
|
||||||
return getDelegate().getRealm(id);
|
return getDelegate().getRealm(id);
|
||||||
} else if (managedRealms.containsKey(id)) {
|
} else if (managedRealms.containsKey(id)) {
|
||||||
return managedRealms.get(id);
|
return managedRealms.get(id);
|
||||||
}
|
}
|
||||||
RealmAdapter adapter = new RealmAdapter(cached, this);
|
RealmAdapter adapter = new RealmAdapter(cached, this);
|
||||||
|
if (wasCached) {
|
||||||
|
CachedRealmModel.RealmCachedEvent event = new CachedRealmModel.RealmCachedEvent() {
|
||||||
|
@Override
|
||||||
|
public CachedRealmModel getRealm() {
|
||||||
|
return adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public KeycloakSession getKeycloakSession() {
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
session.getKeycloakSessionFactory().publish(event);
|
||||||
|
}
|
||||||
managedRealms.put(id, adapter);
|
managedRealms.put(id, adapter);
|
||||||
return adapter;
|
return adapter;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ import java.util.Set;
|
||||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||||
* @version $Revision: 1 $
|
* @version $Revision: 1 $
|
||||||
*/
|
*/
|
||||||
public class CachedRealm extends AbstractRevisioned {
|
public class CachedRealm extends AbstractExtendableRevisioned {
|
||||||
|
|
||||||
protected String name;
|
protected String name;
|
||||||
protected String displayName;
|
protected String displayName;
|
||||||
|
|
65
server-spi/src/main/java/org/keycloak/models/cache/CachedRealmModel.java
vendored
Normal file
65
server-spi/src/main/java/org/keycloak/models/cache/CachedRealmModel.java
vendored
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
||||||
|
* and other contributors as indicated by the @author tags.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.keycloak.models.cache;
|
||||||
|
|
||||||
|
import org.keycloak.models.KeycloakSession;
|
||||||
|
import org.keycloak.models.RealmModel;
|
||||||
|
import org.keycloak.models.UserModel;
|
||||||
|
import org.keycloak.provider.ProviderEvent;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cached realms will implement this interface
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||||
|
* @version $Revision: 1 $
|
||||||
|
*/
|
||||||
|
public interface CachedRealmModel extends RealmModel {
|
||||||
|
|
||||||
|
interface RealmCachedEvent extends ProviderEvent {
|
||||||
|
CachedRealmModel getRealm();
|
||||||
|
KeycloakSession getKeycloakSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidates the cache for this model and returns a delegate that represents the actual data provider
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
RealmModel getDelegateForUpdate();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidate the cache for this model
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void invalidate();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When was the model was loaded from database.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
long getCacheTimestamp();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a map that contains custom things that are cached along with this model. You can write to this map.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
ConcurrentHashMap getCachedWith();
|
||||||
|
}
|
|
@ -36,20 +36,20 @@ public interface CachedUserModel extends UserModel {
|
||||||
UserModel getDelegateForUpdate();
|
UserModel getDelegateForUpdate();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invalidate the cache for this user
|
* Invalidate the cache for this model
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void invalidate();
|
void invalidate();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When was the user loaded from database.
|
* When was the model was loaded from database.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
long getCacheTimestamp();
|
long getCacheTimestamp();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a map that contains custom things that are cached along with the user. You can write to this map.
|
* Returns a map that contains custom things that are cached along with this model. You can write to this map.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue