com.yammer.dropwizard.auth.CachingAuthenticator Maven / Gradle / Ivy
package com.yammer.dropwizard.auth;
import com.google.common.base.Optional;
import com.google.common.cache.*;
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Meter;
import com.yammer.metrics.core.Timer;
import com.yammer.metrics.core.TimerContext;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* An {@link Authenticator} decorator which uses a Guava cache to temporarily cache credentials and
* their corresponding principals.
*
* @param the type of credentials the authenticator can authenticate
* @param the type of principals the authenticator returns
*/
public class CachingAuthenticator implements Authenticator {
/**
* Wraps an underlying authenticator with a cache.
*
* @param authenticator the underlying authenticator
* @param cacheSpec a {@link CacheBuilderSpec}
* @param the type of credentials the authenticator can authenticate
* @param the type of principals the authenticator returns
* @return a cached version of {@code authenticator}
*/
public static CachingAuthenticator wrap(Authenticator authenticator,
CacheBuilderSpec cacheSpec) {
return new CachingAuthenticator(authenticator, CacheBuilder.from(cacheSpec));
}
private final Authenticator underlying;
private final LoadingCache> cache;
private final Meter cacheMisses;
private final Timer gets;
private CachingAuthenticator(Authenticator authenticator,
CacheBuilder