Reusable Java library of general tools with minimal external dependencies.
For questions or support, please contact us:
Email: support@aoindustries.com
Phone: 1-800-519-9541
Phone: +1-251-607-9556
Web: https://www.aoindustries.com/contact
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.
| Modifier and Type | Class and Description |
|---|---|
static interface |
BackgroundCache.Refresher<K,V,E extends Exception>
A callable used to refresh the cache.
|
static class |
BackgroundCache.Result<V,E extends Exception>
The result of a refresh.
|
| Constructor and 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) |
| Modifier and Type | Method and 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.
|
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.
|
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.
|
int |
size()
Gets the current size of the cache.
|
void |
stop()
Stops this cache.
|
String |
toString() |
public BackgroundCache(String name, Class<E> exceptionClass, long refreshInterval, long expirationAge, Logger logger)
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.public void stop()
public BackgroundCache.Result<V,E> get(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher)
get(java.lang.Object),
put(java.lang.Object, com.aoindustries.cache.BackgroundCache.Refresher)public BackgroundCache.Result<V,E> get(K key)
public BackgroundCache.Result<V,E> put(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher)
public void put(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher, V value)
public void put(K key, BackgroundCache.Refresher<? super K,? extends V,? extends E> refresher, E exception)
public int size()
Copyright © 2000–2016 AO Industries, Inc.. All rights reserved.