public void realmRemoval(String id, String name, Set<String> invalidations) { realmUpdated(id, name, invalidations); addInvalidations(InRealmPredicate.create().realm(id), invalidations); }
@Override public void commit() { try { if (clearAll) { cache.clear(); } runInvalidations(); transactionActive = false; } finally { cache.endRevisionBatch(); } }
@Override public void addInvalidations(RealmCacheManager realmCache, Set<String> invalidations) { realmCache.clientAdded(realmId, clientUuid, clientId, invalidations); }
@Override public void addInvalidations(RealmCacheManager realmCache, Set<String> invalidations) { realmCache.clientRemoval(realmId, clientUuid, clientId, invalidations); // Separate iteration for all client roles to invalidate records dependent on them for (Map.Entry<String, String> clientRole : clientRoles.entrySet()) { String roleId = clientRole.getKey(); String roleName = clientRole.getValue(); realmCache.roleRemoval(roleId, roleName, clientUuid, invalidations); } }
@Override public ClientScopeModel getClientScopeById(String id, RealmModel realm) { CachedClientScope cached = cache.get(id, CachedClientScope.class); if (cached != null && !cached.getRealm().equals(realm.getId())) { cached = null; } if (cached == null) { Long loaded = cache.getCurrentRevision(id); ClientScopeModel model = getRealmDelegate().getClientScopeById(id, realm); if (model == null) return null; if (invalidations.contains(id)) return model; cached = new CachedClientScope(loaded, realm, model); cache.addRevisioned(cached, startupRevision); } else if (invalidations.contains(id)) { return getRealmDelegate().getClientScopeById(id, realm); } else if (managedClientScopes.containsKey(id)) { return managedClientScopes.get(id); } ClientScopeAdapter adapter = new ClientScopeAdapter(realm, cached, this); managedClientScopes.put(id, adapter); return adapter; }
private void lazyInit(KeycloakSession session) { if (realmCache == null) { synchronized (this) { if (realmCache == null) { Cache<String, Revisioned> cache = session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.REALM_CACHE_NAME); Cache<String, Long> revisions = session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME); realmCache = new RealmCacheManager(cache, revisions); ClusterProvider cluster = session.getProvider(ClusterProvider.class); cluster.registerListener(REALM_INVALIDATION_EVENTS, (ClusterEvent event) -> { InvalidationEvent invalidationEvent = (InvalidationEvent) event; realmCache.invalidationEventReceived(invalidationEvent); }); cluster.registerListener(REALM_CLEAR_CACHE_EVENTS, (ClusterEvent event) -> { realmCache.clear(); }); log.debug("Registered cluster listeners"); } } } }
@Override public ClientModel getClientById(String id, RealmModel realm) { CachedClient cached = cache.get(id, CachedClient.class); if (cached != null && !cached.getRealm().equals(realm.getId())) { cached = null; } if (cached != null) { logger.tracev("client by id cache hit: {0}", cached.getClientId()); } if (cached == null) { Long loaded = cache.getCurrentRevision(id); ClientModel model = getClientDelegate().getClientById(id, realm); if (model == null) return null; ClientModel adapter = cacheClient(realm, model, loaded); managedApplications.put(id, adapter); return adapter; } else if (invalidations.contains(id)) { return getRealmDelegate().getClientById(id, realm); } else if (managedApplications.containsKey(id)) { return managedApplications.get(id); } ClientModel adapter = validateCache(realm, cached); managedApplications.put(id, adapter); return adapter; }
public void groupQueriesInvalidations(String realmId, Set<String> invalidations) { invalidations.add(RealmCacheSession.getGroupsQueryCacheKey(realmId)); invalidations.add(RealmCacheSession.getTopGroupsQueryCacheKey(realmId)); addInvalidations(GroupListPredicate.create().realm(realmId), invalidations); }
@Override public void addInvalidations(RealmCacheManager realmCache, Set<String> invalidations) { realmCache.clientUpdated(realmId, clientUuid, clientId, invalidations); }
@Override public void rollback() { try { setRollbackOnly = true; runInvalidations(); transactionActive = false; } finally { cache.endRevisionBatch(); } }
public RealmCacheSession(RealmCacheManager cache, KeycloakSession session) { this.cache = cache; this.session = session; this.startupRevision = cache.getCurrentCounter(); session.getTransactionManager().enlistPrepare(getPrepareTransaction()); session.getTransactionManager().enlistAfterCompletion(getAfterTransaction()); }
protected ClientModel cacheClient(RealmModel realm, ClientModel delegate, Long revision) { if (invalidations.contains(delegate.getId())) return delegate; StorageId storageId = new StorageId(delegate.getId()); CachedClient cached = null; ClientAdapter adapter = null; if (!storageId.isLocal()) { ComponentModel component = realm.getComponent(storageId.getProviderId()); ClientStorageProviderModel model = new ClientStorageProviderModel(component); if (!model.isEnabled()) { return delegate; } ClientStorageProviderModel.CachePolicy policy = model.getCachePolicy(); if (policy != null && policy == ClientStorageProviderModel.CachePolicy.NO_CACHE) { return delegate; } cached = new CachedClient(revision, realm, delegate); adapter = new ClientAdapter(realm, cached, this); long lifespan = model.getLifespan(); if (lifespan > 0) { cache.addRevisioned(cached, startupRevision, lifespan); } else { cache.addRevisioned(cached, startupRevision); } } else { cached = new CachedClient(revision, realm, delegate); adapter = new ClientAdapter(realm, cached, this); cache.addRevisioned(cached, startupRevision); } return adapter; }
@Override public boolean removeClient(String id, RealmModel realm) { ClientModel client = getClientById(id, realm); if (client == null) return false; invalidateClient(client.getId()); // this is needed so that a client that hasn't been committed isn't cached in a query listInvalidations.add(realm.getId()); invalidationEvents.add(ClientRemovedEvent.create(client)); cache.clientRemoval(realm.getId(), id, client.getClientId(), invalidations); for (RoleModel role : client.getRoles()) { roleRemovalInvalidations(role.getId(), role.getName(), client.getId()); } return getRealmDelegate().removeClient(id, realm); }
@Override public GroupModel getGroupById(String id, RealmModel realm) { CachedGroup cached = cache.get(id, CachedGroup.class); if (cached != null && !cached.getRealm().equals(realm.getId())) { cached = null; } if (cached == null) { Long loaded = cache.getCurrentRevision(id); GroupModel model = getRealmDelegate().getGroupById(id, realm); if (model == null) return null; if (invalidations.contains(id)) return model; cached = new CachedGroup(loaded, realm, model); cache.addRevisioned(cached, startupRevision); } else if (invalidations.contains(id)) { return getRealmDelegate().getGroupById(id, realm); } else if (managedGroups.containsKey(id)) { return managedGroups.get(id); } GroupAdapter adapter = new GroupAdapter(cached, this, session, realm); managedGroups.put(id, adapter); return adapter; }
public void clientRemoval(String realmId, String clientUUID, String clientId, Set<String> invalidations) { invalidations.add(RealmCacheSession.getRealmClientsQueryCacheKey(realmId)); invalidations.add(RealmCacheSession.getClientByClientIdCacheKey(clientId, realmId)); addInvalidations(InClientPredicate.create().client(clientUUID), invalidations); }
@Override public void registerClientInvalidation(String id, String clientId, String realmId) { invalidateClient(id); invalidationEvents.add(ClientUpdatedEvent.create(id, clientId, realmId)); cache.clientUpdated(realmId, id, clientId, invalidations); }
@Override public RoleModel getRoleById(String id, RealmModel realm) { CachedRole cached = cache.get(id, CachedRole.class); if (cached != null && !cached.getRealm().equals(realm.getId())) { cached = null; } if (cached == null) { Long loaded = cache.getCurrentRevision(id); RoleModel model = getRealmDelegate().getRoleById(id, realm); if (model == null) return null; if (invalidations.contains(id)) return model; if (model.isClientRole()) { cached = new CachedClientRole(loaded, model.getContainerId(), model, realm); } else { cached = new CachedRealmRole(loaded, model, realm); } cache.addRevisioned(cached, startupRevision); } else if (invalidations.contains(id)) { return getRealmDelegate().getRoleById(id, realm); } else if (managedRoles.containsKey(id)) { return managedRoles.get(id); } RoleAdapter adapter = new RoleAdapter(cached,this, realm); managedRoles.put(id, adapter); return adapter; }
public void roleRemoval(String id, String roleName, String roleContainerId, Set<String> invalidations) { invalidations.add(RealmCacheSession.getRolesCacheKey(roleContainerId)); invalidations.add(RealmCacheSession.getRoleByNameCacheKey(roleContainerId, roleName)); addInvalidations(HasRolePredicate.create().role(id), invalidations); }
private ClientModel addedClient(RealmModel realm, ClientModel client) { logger.trace("added Client....."); invalidateClient(client.getId()); // this is needed so that a client that hasn't been committed isn't cached in a query listInvalidations.add(realm.getId()); invalidationEvents.add(ClientAddedEvent.create(client.getId(), client.getClientId(), realm.getId())); cache.clientAdded(realm.getId(), client.getId(), client.getClientId(), invalidations); return client; }
@Override public RealmModel getRealm(String id) { CachedRealm cached = cache.get(id, CachedRealm.class); if (cached != null) { logger.tracev("by id cache hit: {0}", cached.getName()); Long loaded = cache.getCurrentRevision(id); RealmModel model = getRealmDelegate().getRealm(id); if (model == null) return null; if (invalidations.contains(id)) return model; cached = new CachedRealm(loaded, model); cache.addRevisioned(cached, startupRevision); wasCached =true; } else if (invalidations.contains(id)) {