@Test public void testActiveCacheLoadsWhenTimedOut() throws Exception { long timeout = 100; @SuppressWarnings( "unchecked" ) ActiveCacheLoader<String, String> mockLoader = mock( ActiveCacheLoader.class ); ActiveCache<String, String> cache = new ActiveCache<String, String>( mockLoader, timeout ); String testKey = "TEST-KEY"; String testResult = "TEST-RESULT"; String testResult2 = "TEST-RESULT-2"; when( mockLoader.load( testKey ) ).thenReturn( testResult ).thenReturn( testResult2 ); assertEquals( testResult, cache.get( testKey ) ); Thread.sleep( timeout + 10 ); assertEquals( testResult2, cache.get( testKey ) ); verify( mockLoader, times( 2 ) ).load( testKey ); }
public Value get( Key key ) throws Exception { ActiveCacheResult<Value> result = null; Future<ActiveCacheResult<Value>> futureResult = null; synchronized ( this ) { result = valueMap.get( key ); boolean shouldReload = false; long time = System.currentTimeMillis(); if ( result == null || result.getTimeLoaded() + timeout < time ) { result = null; shouldReload = true; } else if ( result.getTimeLoaded() + ( timeout / 2.0 ) < time ) { shouldReload = true; } if ( shouldReload ) { futureResult = loadingMap.get( key ); if ( futureResult == null ) { futureResult = executorServiceGetter.getExecutor().submit( new ActiveCacheCallable<Key, Value>( this, valueMap, loadingMap, key, loader ) ); loadingMap.put( key, futureResult ); } } } if ( result == null ) { result = futureResult.get(); } Exception exception = result.getException(); if ( exception != null ) { throw exception; } return result.getValue(); }
ActiveCache { public Value get( Key key ) throws Exception { ActiveCacheResult<Value> result = null; Future<ActiveCacheResult<Value>> futureResult = null; synchronized ( this ) { result = valueMap.get( key ); boolean shouldReload = false; long time = System.currentTimeMillis(); if ( result == null || result.getTimeLoaded() + timeout < time ) { result = null; shouldReload = true; } else if ( result.getTimeLoaded() + ( timeout / 2.0 ) < time ) { shouldReload = true; } if ( shouldReload ) { futureResult = loadingMap.get( key ); if ( futureResult == null ) { futureResult = executorServiceGetter.getExecutor().submit( new ActiveCacheCallable<Key, Value>( this, valueMap, loadingMap, key, loader ) ); loadingMap.put( key, futureResult ); } } } if ( result == null ) { result = futureResult.get(); } Exception exception = result.getException(); if ( exception != null ) { throw exception; } return result.getValue(); } }
ActiveCache { public Value get( Key key ) throws Exception { ActiveCacheResult<Value> result = null; Future<ActiveCacheResult<Value>> futureResult = null; synchronized ( this ) { result = valueMap.get( key ); boolean shouldReload = false; long time = System.currentTimeMillis(); if ( result == null || result.getTimeLoaded() + timeout < time ) { result = null; shouldReload = true; } else if ( result.getTimeLoaded() + ( timeout / 2.0 ) < time ) { shouldReload = true; } if ( shouldReload ) { futureResult = loadingMap.get( key ); if ( futureResult == null ) { futureResult = executorServiceGetter.getExecutor().submit( new ActiveCacheCallable<Key, Value>( this, valueMap, loadingMap, key, loader ) ); loadingMap.put( key, futureResult ); } } } if ( result == null ) { result = futureResult.get(); } Exception exception = result.getException(); if ( exception != null ) { throw exception; } return result.getValue(); } ActiveCache( ActiveCacheLoader<Key, Value> loader, long timeout );  ActiveCache( ActiveCacheLoader<Key, Value> loader, long timeout, ExecutorServiceGetter executorServiceGetter );  ActiveCache( ActiveCacheLoader<Key, Value> loader, Map<Key, ActiveCacheResult<Value>> valueMap,
      Map<Key, Future<ActiveCacheResult<Value>>> loadingMap, long timeout, ExecutorServiceGetter executorServiceGetter ); }
ActiveCache { public Value get( Key key ) throws Exception { ActiveCacheResult<Value> result = null; Future<ActiveCacheResult<Value>> futureResult = null; synchronized ( this ) { result = valueMap.get( key ); boolean shouldReload = false; long time = System.currentTimeMillis(); if ( result == null || result.getTimeLoaded() + timeout < time ) { result = null; shouldReload = true; } else if ( result.getTimeLoaded() + ( timeout / 2.0 ) < time ) { shouldReload = true; } if ( shouldReload ) { futureResult = loadingMap.get( key ); if ( futureResult == null ) { futureResult = executorServiceGetter.getExecutor().submit( new ActiveCacheCallable<Key, Value>( this, valueMap, loadingMap, key, loader ) ); loadingMap.put( key, futureResult ); } } } if ( result == null ) { result = futureResult.get(); } Exception exception = result.getException(); if ( exception != null ) { throw exception; } return result.getValue(); } ActiveCache( ActiveCacheLoader<Key, Value> loader, long timeout );  ActiveCache( ActiveCacheLoader<Key, Value> loader, long timeout, ExecutorServiceGetter executorServiceGetter );  ActiveCache( ActiveCacheLoader<Key, Value> loader, Map<Key, ActiveCacheResult<Value>> valueMap,
      Map<Key, Future<ActiveCacheResult<Value>>> loadingMap, long timeout, ExecutorServiceGetter executorServiceGetter ); Value get( Key key ); }
ActiveCache { public Value get( Key key ) throws Exception { ActiveCacheResult<Value> result = null; Future<ActiveCacheResult<Value>> futureResult = null; synchronized ( this ) { result = valueMap.get( key ); boolean shouldReload = false; long time = System.currentTimeMillis(); if ( result == null || result.getTimeLoaded() + timeout < time ) { result = null; shouldReload = true; } else if ( result.getTimeLoaded() + ( timeout / 2.0 ) < time ) { shouldReload = true; } if ( shouldReload ) { futureResult = loadingMap.get( key ); if ( futureResult == null ) { futureResult = executorServiceGetter.getExecutor().submit( new ActiveCacheCallable<Key, Value>( this, valueMap, loadingMap, key, loader ) ); loadingMap.put( key, futureResult ); } } } if ( result == null ) { result = futureResult.get(); } Exception exception = result.getException(); if ( exception != null ) { throw exception; } return result.getValue(); } ActiveCache( ActiveCacheLoader<Key, Value> loader, long timeout );  ActiveCache( ActiveCacheLoader<Key, Value> loader, long timeout, ExecutorServiceGetter executorServiceGetter );  ActiveCache( ActiveCacheLoader<Key, Value> loader, Map<Key, ActiveCacheResult<Value>> valueMap,
      Map<Key, Future<ActiveCacheResult<Value>>> loadingMap, long timeout, ExecutorServiceGetter executorServiceGetter ); Value get( Key key );  }