All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redhat.lightblue.rest.authz.RolesCache Maven / Gradle / Ivy

There is a newer version: 2.32.0
Show newest version
package com.redhat.lightblue.rest.authz;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.ParametersAreNonnullByDefault;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;

/**
 * RolesCache contains 2 caches: rolesCache and fallbackRolesCache. Roles put in
 * the former are evicted after few minutes, so that role membership changes in
 * ldap are reflected reasonably quickly. Roles put in the latter are evicted
 * after few hours. FallbackRolesCache is read only when ldap failure is
 * identified (circuit breaker pattern).
 *
 * @author mpatercz
 *
 */
public class RolesCache {

    private static final Logger LOGGER = LoggerFactory.getLogger(RolesCache.class);
    private static Cache> rolesCache; // non-persisted cache
    private static Cache> fallbackRolesCache; // non-persisted cache

    static {
        rolesCache = CacheBuilder.newBuilder()
                .concurrencyLevel(10) // handle 10 concurrent request without a problem
                .maximumSize(500) // Hold 500 sessions before remove them
                .expireAfterWrite(5, TimeUnit.MINUTES) // If the session is inactive for more than 5 minutes, remove it
                .removalListener(
                        new RemovalListener>() {
                    {
                        LOGGER.debug("Removal Listener created");
                    }

                    @Override
                    public void onRemoval(@ParametersAreNonnullByDefault RemovalNotification notification) {
                        LOGGER.debug("This data from " + notification.getKey() + " evacuated due:" + notification.getCause());
                    }
                }
                ).build();
        LOGGER.debug("RolesCache: rolesCache was created on a static block");

        fallbackRolesCache = CacheBuilder.newBuilder()
                .concurrencyLevel(10) // handle 10 concurrent request without a problem
                .maximumSize(500) // Hold 500 sessions before remove them
                .build();
        LOGGER.debug("RolesCache: fallbackRolesCache was created on a static block");
    }

    public static void put(String login, List roles) {
        LOGGER.debug("RolesCache#put was invoked");
        rolesCache.put(login, roles);
        fallbackRolesCache.put(login, roles);
    }

    public static List get(String login) {
        LOGGER.debug("RolesCache#get was invoked");
        return rolesCache.getIfPresent(login);
    }

    public static List getFromFallback(String login) {
        LOGGER.debug("RolesCache#getFromFallback was invoked");
        return fallbackRolesCache.getIfPresent(login);
    }

    public static void invalidate(String login) {
        LOGGER.debug("RolesCache#invalidate was invoked");
        rolesCache.invalidate(login);
        fallbackRolesCache.invalidate(login);
    }

    public static Cache> getRolesCache() {
        return rolesCache;
    }

    public static Cache> getFallbackRolesCache() {
        return fallbackRolesCache;
    }

    public static void invalidateAll() {
        rolesCache.invalidateAll();
        fallbackRolesCache.invalidateAll();
    }

    public static void setRolesCache(Cache> rolesCache) {
        RolesCache.rolesCache = rolesCache;
    }

    public static void setFallbackRolesCache(Cache> fallbackRolesCache) {
        RolesCache.fallbackRolesCache = fallbackRolesCache;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy