restx.security.GuavaEntryCacheManager Maven / Gradle / Ivy
package restx.security;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import restx.security.RestxSession.Definition.CachedEntry;
import restx.security.RestxSession.Definition.Entry;
import restx.security.RestxSession.Definition.EntryCacheManager;
import java.util.concurrent.ExecutionException;
/**
* A restx session entry cache manager based on guava cache.
*
* You can override the cache settings by overriding the getCacheBuilder() method.
*
* Note that Guava Cache is not distributed, so be very careful with cache invalidation
* when using this cache.
*
* This is the default EntryCacheManager, see SecurityModule which provides one.
*/
public class GuavaEntryCacheManager implements EntryCacheManager {
@Override
public CachedEntry getCachedEntry(Entry entry) {
return new GuavaCacheSessionDefinitionEntry(entry.getKey(), getLoadingCacheFor(entry));
}
protected LoadingCache getLoadingCacheFor(final Entry entry) {
return getCacheBuilder(entry).build(getCacheLoaderFor(entry));
}
protected CacheLoader getCacheLoaderFor(final Entry entry) {
return new CacheLoader() {
@Override
public T load(String key) throws Exception {
return entry.getValueForId(key).orNull();
}
};
}
protected CacheBuilder