@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; }
@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; }
@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; }
query = new GroupListQuery(loaded, cacheKey, realm, ids); logger.tracev("adding realm getTopLevelGroups cache miss: realm {0} key {1}", realm.getName(), cacheKey); cache.addRevisioned(query, startupRevision); return model;
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; }
query = new GroupListQuery(loaded, cacheKey, realm, ids); logger.tracev("adding realm getTopLevelGroups cache miss: realm {0} key {1}", realm.getName(), cacheKey); cache.addRevisioned(query, startupRevision); return model;
if (invalidations.contains(id)) return model; cached = new CachedRealm(loaded, model); cache.addRevisioned(cached, startupRevision); wasCached =true; } else if (invalidations.contains(id)) {
@Override public RealmModel getRealmByName(String name) { String cacheKey = getRealmByNameCacheKey(name); RealmListQuery query = cache.get(cacheKey, RealmListQuery.class); if (query != null) { logger.tracev("realm by name cache hit: {0}", name); } if (query == null) { Long loaded = cache.getCurrentRevision(cacheKey); RealmModel model = getRealmDelegate().getRealmByName(name); if (model == null) return null; if (invalidations.contains(model.getId())) return model; query = new RealmListQuery(loaded, cacheKey, model.getId()); cache.addRevisioned(query, startupRevision); return model; } else if (invalidations.contains(cacheKey)) { return getRealmDelegate().getRealmByName(name); } else { String realmId = query.getRealms().iterator().next(); if (invalidations.contains(realmId)) { return getRealmDelegate().getRealmByName(name); } return getRealm(realmId); } }
query = new GroupListQuery(loaded, cacheKey, realm, ids); logger.tracev("adding realm getGroups cache miss: realm {0} key {1}", realm.getName(), cacheKey); cache.addRevisioned(query, startupRevision); return model;
query = new RoleListQuery(loaded, cacheKey, realm, ids); logger.tracev("adding realm roles cache miss: realm {0} key {1}", realm.getName(), cacheKey); cache.addRevisioned(query, startupRevision); return model;
@Override public ClientModel getClientByClientId(String clientId, RealmModel realm) { String cacheKey = getClientByClientIdCacheKey(clientId, realm.getId()); ClientListQuery query = cache.get(cacheKey, ClientListQuery.class); String id = null; if (query != null) { logger.tracev("client by name cache hit: {0}", clientId); } if (query == null) { Long loaded = cache.getCurrentRevision(cacheKey); ClientModel model = getClientDelegate().getClientByClientId(clientId, realm); if (model == null) return null; if (invalidations.contains(model.getId())) return model; id = model.getId(); query = new ClientListQuery(loaded, cacheKey, realm, id); logger.tracev("adding client by name cache miss: {0}", clientId); cache.addRevisioned(query, startupRevision); } else if (invalidations.contains(cacheKey)) { return getClientDelegate().getClientByClientId(clientId, realm); } else { id = query.getClients().iterator().next(); if (invalidations.contains(id)) { return getClientDelegate().getClientByClientId(clientId, realm); } } return getClientById(id, realm); }
query = new ClientListQuery(loaded, cacheKey, realm, ids); logger.tracev("adding realm clients cache miss: realm {0} key {1}", realm.getName(), cacheKey); cache.addRevisioned(query, startupRevision); return model;
query = new RoleListQuery(loaded, cacheKey, realm, ids, client.getClientId()); logger.tracev("adding client roles cache miss: client {0} key {1}", client.getClientId(), cacheKey); cache.addRevisioned(query, startupRevision); return model;
@Override public RoleModel getRealmRole(RealmModel realm, String name) { String cacheKey = getRoleByNameCacheKey(realm.getId(), name); boolean queryDB = invalidations.contains(cacheKey) || listInvalidations.contains(realm.getId()); if (queryDB) { return getRealmDelegate().getRealmRole(realm, name); } RoleListQuery query = cache.get(cacheKey, RoleListQuery.class); if (query != null) { logger.tracev("getRealmRole cache hit: {0}.{1}", realm.getName(), name); } if (query == null) { Long loaded = cache.getCurrentRevision(cacheKey); RoleModel model = getRealmDelegate().getRealmRole(realm, name); if (model == null) return null; query = new RoleListQuery(loaded, cacheKey, realm, model.getId()); logger.tracev("adding realm role cache miss: client {0} key {1}", realm.getName(), cacheKey); cache.addRevisioned(query, startupRevision); return model; } RoleModel role = getRoleById(query.getRoles().iterator().next(), realm); if (role == null) { invalidations.add(cacheKey); return getRealmDelegate().getRealmRole(realm, name); } return role; }
@Override public RoleModel getClientRole(RealmModel realm, ClientModel client, String name) { String cacheKey = getRoleByNameCacheKey(client.getId(), name); boolean queryDB = invalidations.contains(cacheKey) || listInvalidations.contains(client.getId()); if (queryDB) { return getRealmDelegate().getClientRole(realm, client, name); } RoleListQuery query = cache.get(cacheKey, RoleListQuery.class); if (query != null) { logger.tracev("getClientRole cache hit: {0}.{1}", client.getClientId(), name); } if (query == null) { Long loaded = cache.getCurrentRevision(cacheKey); RoleModel model = getRealmDelegate().getClientRole(realm, client, name); if (model == null) return null; query = new RoleListQuery(loaded, cacheKey, realm, model.getId(), client.getClientId()); logger.tracev("adding client role cache miss: client {0} key {1}", client.getClientId(), cacheKey); cache.addRevisioned(query, startupRevision); return model; } RoleModel role = getRoleById(query.getRoles().iterator().next(), realm); if (role == null) { invalidations.add(cacheKey); return getRealmDelegate().getClientRole(realm, client, name); } return role; }