Skip to content

Commit

Permalink
Don't fail on ttl value when not using it
Browse files Browse the repository at this point in the history
  • Loading branch information
OliviaYtterbrink committed Nov 1, 2017
1 parent 87b3834 commit 5fde2c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
Expand Up @@ -216,9 +216,9 @@ private static AuthenticationStrategy createAuthenticationStrategy( Config confi
private static CacheManager createCacheManager( Config config ) private static CacheManager createCacheManager( Config config )
{ {
long ttl = config.get( SecuritySettings.auth_cache_ttl ).toMillis(); long ttl = config.get( SecuritySettings.auth_cache_ttl ).toMillis();
boolean use_ttl = config.get( SecuritySettings.auth_cache_use_ttl ); boolean useTTL = config.get( SecuritySettings.auth_cache_use_ttl );
int maxCapacity = config.get( SecuritySettings.auth_cache_max_capacity ); int maxCapacity = config.get( SecuritySettings.auth_cache_max_capacity );
return new ShiroCaffeineCache.Manager( Ticker.systemTicker(), ttl, maxCapacity, use_ttl ); return new ShiroCaffeineCache.Manager( Ticker.systemTicker(), ttl, maxCapacity, useTTL );
} }


private static List<PluginRealm> createPluginRealms( private static List<PluginRealm> createPluginRealms(
Expand Down
Expand Up @@ -38,25 +38,23 @@ class ShiroCaffeineCache<K, V> implements Cache<K,V>
{ {
private final com.github.benmanes.caffeine.cache.Cache<K,V> caffCache; private final com.github.benmanes.caffeine.cache.Cache<K,V> caffCache;


ShiroCaffeineCache( Ticker ticker, long ttl, int maxCapacity, boolean use_ttl ) ShiroCaffeineCache( Ticker ticker, long ttl, int maxCapacity, boolean useTTL )
{ {
this( ticker, ForkJoinPool.commonPool(), ttl, maxCapacity, use_ttl ); this( ticker, ForkJoinPool.commonPool(), ttl, maxCapacity, useTTL );
} }


ShiroCaffeineCache( Ticker ticker, Executor maintenanceExecutor, long ttl, int maxCapacity, boolean use_ttl ) ShiroCaffeineCache( Ticker ticker, Executor maintenanceExecutor, long ttl, int maxCapacity, boolean useTTL )
{ {
if ( ttl <= 0 )
{
throw new IllegalArgumentException( "TTL must be larger than zero." );
}

Caffeine<Object,Object> builder = Caffeine.newBuilder() Caffeine<Object,Object> builder = Caffeine.newBuilder()
.maximumSize( maxCapacity ) .maximumSize( maxCapacity )
.executor( maintenanceExecutor ) .executor( maintenanceExecutor );
.ticker( ticker ); if ( useTTL )
if ( use_ttl )
{ {
builder.expireAfterWrite( ttl, TimeUnit.MILLISECONDS ); if ( ttl <= 0 )
{
throw new IllegalArgumentException( "TTL must be larger than zero." );
}
builder.ticker( ticker ).expireAfterWrite( ttl, TimeUnit.MILLISECONDS );
} }
caffCache = builder.build(); caffCache = builder.build();
} }
Expand Down Expand Up @@ -109,24 +107,23 @@ static class Manager implements CacheManager
private final Ticker ticker; private final Ticker ticker;
private final long ttl; private final long ttl;
private final int maxCapacity; private final int maxCapacity;
private boolean use_ttl; private boolean useTTL;


Manager( Ticker ticker, long ttl, int maxCapacity, boolean use_ttl ) Manager( Ticker ticker, long ttl, int maxCapacity, boolean useTTL )
{ {
this.ticker = ticker; this.ticker = ticker;
this.ttl = ttl; this.ttl = ttl;
this.maxCapacity = maxCapacity; this.maxCapacity = maxCapacity;
this.use_ttl = use_ttl; this.useTTL = useTTL;
caches = new HashMap<>(); caches = new HashMap<>();
} }


@Override @Override
public <K, V> Cache<K,V> getCache( String s ) throws CacheException public <K, V> Cache<K,V> getCache( String s ) throws CacheException
{ {
//noinspection unchecked //noinspection unchecked
return (Cache<K,V>) caches.computeIfAbsent( s, ignored -> ttl <= 0 ? return (Cache<K,V>) caches.computeIfAbsent( s,
new NullCache() : ignored -> useTTL && ttl <= 0 ? new NullCache() : new ShiroCaffeineCache<K,V>( ticker, ttl, maxCapacity, useTTL ) );
new ShiroCaffeineCache<K,V>( ticker, ttl, maxCapacity, use_ttl ) );
} }
} }


Expand Down
Expand Up @@ -45,8 +45,9 @@ public void setUp()
} }


@Test @Test
public void shouldFailToCreateAuthCacheForTTLZero() public void shouldFailToCreateAuthCacheForTTLZeroIfUsingTLL()
{ {
new ShiroCaffeineCache<>( fakeTicker::read, Runnable::run, 0, 5, false );
try try
{ {
new ShiroCaffeineCache<>( fakeTicker::read, Runnable::run, 0, 5, true ); new ShiroCaffeineCache<>( fakeTicker::read, Runnable::run, 0, 5, true );
Expand Down

0 comments on commit 5fde2c3

Please sign in to comment.