protected void removeUserSessions(RealmModel realm, boolean offline) { Cache<String, SessionEntity> cache = getCache(offline); Iterator<String> itr = cache.entrySet().stream().filter(SessionPredicate.create(realm.getId())).map(Mappers.sessionId()).iterator(); while (itr.hasNext()) { cache.remove(itr.next()); } }
private void removeLocalUserSessions(String realmId, boolean offline) { FuturesHelper futures = new FuturesHelper(); Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline); Cache<String, SessionEntityWrapper<UserSessionEntity>> localCache = CacheDecorators.localCache(cache); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> clientSessionCache = getClientSessionCache(offline); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> localClientSessionCache = CacheDecorators.localCache(clientSessionCache); Cache<String, SessionEntityWrapper<UserSessionEntity>> localCacheStoreIgnore = CacheDecorators.skipCacheLoaders(localCache); final AtomicInteger userSessionsSize = new AtomicInteger(); localCacheStoreIgnore .entrySet() .stream() .filter(SessionPredicate.create(realmId)) .map(Mappers.userSessionEntity()) .forEach(new Consumer<UserSessionEntity>() { @Override public void accept(UserSessionEntity userSessionEntity) { userSessionsSize.incrementAndGet(); // Remove session from remoteCache too. Use removeAsync for better perf Future future = localCache.removeAsync(userSessionEntity.getId()); futures.addTask(future); userSessionEntity.getAuthenticatedClientSessions().forEach((clientUUID, clientSessionId) -> { Future f = localClientSessionCache.removeAsync(clientSessionId); futures.addTask(f); }); } }); futures.waitForAllToFinish(); log.debugf("Removed %d sessions in realm %s. Offline: %b", (Object) userSessionsSize.get(), realmId, offline); }