Module com.aoapps.hodgepodge
Package com.aoapps.hodgepodge.cache
Class BackgroundCache<K,V,Ex extends Throwable>
java.lang.Object
com.aoapps.hodgepodge.cache.BackgroundCache<K,V,Ex>
- Type Parameters:
Ex
- An arbitrary exception type that may be thrown
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 ClassesModifier and TypeClassDescriptionstatic interface
BackgroundCache.Refresher<K,
V, Ex extends Throwable> A callable used to refresh the cache.static class
BackgroundCache.Result<V,
Ex extends Throwable> The result of a refresh. -
Constructor Summary
ConstructorsConstructorDescriptionBackgroundCache
(String name, Class<? extends Ex> exceptionClass, long refreshInterval, long expirationAge) Uses the default logger.BackgroundCache
(String name, Class<? extends Ex> exceptionClass, long refreshInterval, long expirationAge, Logger logger) -
Method Summary
Modifier and TypeMethodDescriptionGets a cached result for the given key, null if not cached.Gets the value if currently in the cache.Runs the refresher immediately to obtain the result, then places an entry into the cache, replacing any existing entry under this key.void
Places a result into the cache, replacing any existing entry under this key.void
Places a result into the cache, replacing any existing entry under this key.int
size()
Gets the current size of the cache.void
stop()
Stops this cache.toString()
-
Constructor Details
-
BackgroundCache
public BackgroundCache(String name, Class<? extends Ex> 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.
-
BackgroundCache
public BackgroundCache(String name, Class<? extends Ex> exceptionClass, long refreshInterval, long expirationAge) Uses the default logger.- See Also:
-
-
Method Details
-
toString
-
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,Ex> get(K key, BackgroundCache.Refresher<? super K, ? extends V, ? extends Ex> 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
Gets a cached result for the given key, null if not cached. Extends the expiration of the cache entry. -
put
public BackgroundCache.Result<V,Ex> put(K key, BackgroundCache.Refresher<? super K, ? extends V, ? extends Ex> 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 Ex> 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 Ex> refresher, Ex 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.
-