Class SingleItemCache<T>
java.lang.Object
com.peterphi.std.guice.common.cached.util.SingleItemCache<T>
- Type Parameters:
T
- the type of value cached
Wrapper around a single item guava cache as a workaround for google not implementing the request for a single item cache made in 2012.
This is a light wrapper around Cache
-
Constructor Summary
ConstructorDescriptionSingleItemCache
(com.google.common.cache.CacheBuilder builder, Callable<T> supplier) Create a new cache, caching the result ofsupplier
using a cache built bybuilder
.SingleItemCache
(com.google.common.cache.CacheBuilder builder, Callable<T> supplier, boolean useCacheManager) Create a new cache, caching the result ofsupplier
using a cache built bybuilder
.SingleItemCache
(Callable<T> supplier, Timeout validity) Create a new cache, caching the result ofsupplier
for a maximum ofvalidity
. -
Method Summary
-
Constructor Details
-
SingleItemCache
Create a new cache, caching the result ofsupplier
for a maximum ofvalidity
. A new Cache will be constructed usingCacheBuilder
- Parameters:
supplier
- a function that generates some cache-worthy result; seeget()
for information on how/when this method is called. The supplier MUST NOT return nullvalidity
- the time after write that the cached result fromsupplier
should expire
-
SingleItemCache
Create a new cache, caching the result ofsupplier
using a cache built bybuilder
.- Parameters:
builder
- the cache builder; will be largely untouched, although the maximum size will be set to 1 (since the cache will only store a single item)supplier
- a function that generates some cache-worthy result; seeget()
for information on how/when this method is called. The supplier MUST NOT return null
-
SingleItemCache
public SingleItemCache(com.google.common.cache.CacheBuilder builder, Callable<T> supplier, boolean useCacheManager) Create a new cache, caching the result ofsupplier
using a cache built bybuilder
.- Parameters:
builder
- the cache builder; will be largely untouched, although the maximum size will be set to 1 (since the cache will only store a single item)supplier
- a function that generates some cache-worthy result; seeget()
for information on how/when this method is called. The supplier MUST NOT return nulluseCacheManager
- if true, will not be registered with the CacheManager to allow cache to be cleared manually
-
-
Method Details
-
getIfPresent
Get the value if it's sitting in the cache, otherwise return null- Returns:
- the value from the cache, or null if no value was cached
- See Also:
-
Cache.getIfPresent(Object)
-
get
Return the cached value (or regenerate it).- Returns:
- the cached result, or a newly-generated value if the cache had expired (or had been actively invalidated)
- Throws:
RuntimeException
- if the supplier function passed to the constructor throws- See Also:
-
Cache.get(Object, Callable)
-
invalidate
public void invalidate()Invalidate any cached result, such that subsequent calls toget()
will re-generate the value- See Also:
-
Cache.invalidateAll()
-
setName
-
getName
-
toString
-