Package com.aoindustries.cache
Class BackgroundCache<K,V,E extends Exception>
- java.lang.Object
-
- com.aoindustries.cache.BackgroundCache<K,V,E>
-
public class BackgroundCache<K,V,E extends Exception> extends Object
A cache that is refreshed in the background, implementing only get, put, and size. There is no remove; background cleaning is performed on the underlying map.
When the system is sitting idle, all cache entries are expired and there is zero overhead. Background refreshes only happen on recently used keys.
- Author:
- AO Industries, Inc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceBackgroundCache.Refresher<K,V,E extends Exception>A callable used to refresh the cache.static classBackgroundCache.Result<V,E extends Exception>The result of a refresh.
-
Constructor Summary
Constructors Constructor Description BackgroundCache(String name, Class<E> exceptionClass, long refreshInterval, long expirationAge)Uses the default logger.BackgroundCache(String name, Class<E> exceptionClass, long refreshInterval, long expirationAge, Logger logger)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BackgroundCache.Result<V,E>get(K key)Gets a cached result for the given key, null if not cached.BackgroundCache.Result<V,E>get(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher)Gets the value if currently in the cache.BackgroundCache.Result<V,E>put(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher)Runs the refresher immediately to obtain the result, then places an entry into the cache, replacing any existing entry under this key.voidput(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher, E exception)Places a result into the cache, replacing any existing entry under this key.voidput(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher, V value)Places a result into the cache, replacing any existing entry under this key.intsize()Gets the current size of the cache.voidstop()Stops this cache.StringtoString()
-
-
-
Constructor Detail
-
BackgroundCache
public BackgroundCache(String name, Class<E> exceptionClass, long refreshInterval, long expirationAge, Logger logger)
- Parameters:
name- The name resources are based on, such as background thread names.exceptionClass- The class object used to verify exception types are runtimerefreshInterval- The time between cache entry refreshesexpirationAge- The time the a cache entry will expire if it has not been accessed, the actual expiration may happen after this time as it is only checked during refreshes.
-
-
Method Detail
-
stop
public void stop()
Stops this cache. This cache should not be used after this is called. Repeated calls to stop are allowed.
-
get
public BackgroundCache.Result<V,E> get(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher)
Gets the value if currently in the cache. If not, Runs the refresher immediately to obtain the result, then places an entry into the cache.- Returns:
- The result obtained from either the cache or this refresher
- See Also:
get(java.lang.Object),put(java.lang.Object, com.aoindustries.cache.BackgroundCache.Refresher)
-
get
public BackgroundCache.Result<V,E> get(K key)
Gets a cached result for the given key, null if not cached. Extends the expiration of the cache entry.
-
put
public BackgroundCache.Result<V,E> put(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher)
Runs the refresher immediately to obtain the result, then places an entry into the cache, replacing any existing entry under this key.- Returns:
- The result obtained from this refresher
-
put
public void put(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher, V value)
Places a result into the cache, replacing any existing entry under this key.
-
put
public void put(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher, E exception)
Places a result into the cache, replacing any existing entry under this key.
-
size
public int size()
Gets the current size of the cache.
-
-