Package com.sodius.oslc.core.cache
Class CacheBuilder<V extends Serializable>
- java.lang.Object
-
- com.sodius.oslc.core.cache.CacheBuilder<V>
-
- Type Parameters:
V
- the base value type for all caches created by this builder
public abstract class CacheBuilder<V extends Serializable> extends Object
A builder ofLoadingCache
andCache
instances.By default, this class uses a proxy class to provide the
LoadingCache
implementation out of theCache
implementation. Applications must implement thebuild(String)
method to provide this latter and override thebuild(String, CacheLoader)
method only if a behavior other than the proxy is required. It can either use a standard Guava implementation, or an application specific cache.- Since:
- 1.12.0
- See Also:
CacheFactory
-
-
Constructor Summary
Constructors Constructor Description CacheBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract com.google.common.cache.Cache<String,V>
build(String title)
Builds a synchronous cache which does not automatically load values when keys are requested.com.google.common.cache.LoadingCache<String,V>
build(String title, com.google.common.cache.CacheLoader<String,V> loader)
Creates a synchronous cache, which either returns an already-loaded value for a given key or atomically computes or retrieves it using the supplied CacheLoader.static <V extends Serializable>
com.google.common.cache.Cache<String,V>createCleanableCache(com.google.common.cache.Cache<String,V> cache)
Call this method duringCache creation
to make a Cache whose expired entries are removed every time a new entry is put or invalidated.CacheBuilder<V>
expireAfterWrite(long duration, TimeUnit unit)
Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.protected long
getExpireAfterWriteNanos()
Returns the duration in nanoseconds after an entry is created that it should be automatically removed.
-
-
-
Method Detail
-
createCleanableCache
public static <V extends Serializable> com.google.common.cache.Cache<String,V> createCleanableCache(com.google.common.cache.Cache<String,V> cache)
Call this method duringCache creation
to make a Cache whose expired entries are removed every time a new entry is put or invalidated. Expired entries are removed in a separate thread after the regular method call. Threads are created using the factory provided byThreads.getThreadFactory()
.- Type Parameters:
V
- the cache entry value type.- Parameters:
cache
- the cache to convert.- Returns:
- a Cache whose expired entries are removed every time a new entry is put or invalidated.
- Throws:
NullPointerException
- ifcache
isnull
.- Since:
- 3.3.0
-
expireAfterWrite
public CacheBuilder<V> expireAfterWrite(long duration, TimeUnit unit)
Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.- Parameters:
duration
- the length of time after an entry is created that it should be automatically removedunit
- the unit that duration is expressed in- Returns:
- this cache builder
-
build
public abstract com.google.common.cache.Cache<String,V> build(String title)
Builds a synchronous cache which does not automatically load values when keys are requested.Consider
build(String, CacheLoader)
instead, if it is feasible to implement a CacheLoader.- Parameters:
title
- the cache title- Returns:
- a cache having the requested features
-
build
public com.google.common.cache.LoadingCache<String,V> build(String title, com.google.common.cache.CacheLoader<String,V> loader)
Creates a synchronous cache, which either returns an already-loaded value for a given key or atomically computes or retrieves it using the supplied CacheLoader.- Parameters:
title
- the cache titleloader
- the cache loader used to obtain new values- Returns:
- a cache having the requested features
-
getExpireAfterWriteNanos
protected long getExpireAfterWriteNanos()
Returns the duration in nanoseconds after an entry is created that it should be automatically removed.- Returns:
- the duration in nanoseconds after an entry is created that it should be automatically removed
-
-