Package org.openbravo.cache
Class TimeInvalidatedCache<K,V>
- java.lang.Object
-
- org.openbravo.cache.TimeInvalidatedCache<K,V>
-
- Type Parameters:
K
- Key class type used by the Cache(for example String for a UUID)V
- Value class type used by the Cache
public class TimeInvalidatedCache<K,V> extends Object
Cache API that allows creating a cache whose entries will be invalidated after a period of time Entries will be invalidated after a period of time defined by the cache user, starting from the moment the value of a given key has last been computed. That means this cache is lazy, it will only compute a value of a given key if the key is accessed. An invalidated entry means that on the next read of that entry on the cache, the value will be recomputed with the previously provided loader function. Use the newBuilder static function to create a new cache and then follow the builder structure.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description V
get(K key)
Returns a value from the cache associated to a given keyV
get(K key, Function<? super K,? extends V> mappingFunction)
Returns a value from the cache associated to a given key.Map<K,V>
getAll(Collection<K> keys)
Returns a map of Key-Value of all the given keys It will never contain null keys or values.Map<K,V>
getAll(Collection<K> keys, Function<? super Set<? extends K>,? extends Map<? extends K,? extends V>> mappingFunction)
Returns a map of Key-Value of all the given keys.String
getName()
Name of the cachevoid
invalidate(K key)
Invalidates given key in the cachevoid
invalidateAll()
Invalidates all the keys in the cachestatic TimeInvalidatedCacheBuilder<Object,Object>
newBuilder()
Creates a new instance ofTimeInvalidatedCacheBuilder
, class contains instructions on building a TimeInvalidatedCache.
-
-
-
Method Detail
-
newBuilder
public static TimeInvalidatedCacheBuilder<Object,Object> newBuilder()
Creates a new instance ofTimeInvalidatedCacheBuilder
, class contains instructions on building a TimeInvalidatedCache.- Returns:
- a new
TimeInvalidatedCacheBuilder
object
-
get
public V get(K key)
Returns a value from the cache associated to a given key- Parameters:
key
- Key to retrieve corresponding value- Returns:
- Value corresponding to given key
- Throws:
NullPointerException
- if the specified key is null (not the value associated with the key)
-
get
public V get(K key, Function<? super K,? extends V> mappingFunction)
Returns a value from the cache associated to a given key. If the key is not in the cache and is not computable using the loader function, it will return the result of executing the provided mappingFunction.- Parameters:
key
- Key to retrieve corresponding valuemappingFunction
- Mapping function that will compute the value of the key if not present in the cache- Returns:
- Value corresponding to given key
- Throws:
NullPointerException
- if the specified key is null (not the value associated with the key)
-
getAll
public Map<K,V> getAll(Collection<K> keys)
Returns a map of Key-Value of all the given keys It will never contain null keys or values. If a key is not computable to a value, it will not appear in the resulting Map.- Parameters:
keys
- Collection of keys to retrieve values from- Returns:
- map of Key-Value of all the given keys
- Throws:
NullPointerException
- if any of the specified keys is null (not the value associated with the key)
-
getAll
public Map<K,V> getAll(Collection<K> keys, Function<? super Set<? extends K>,? extends Map<? extends K,? extends V>> mappingFunction)
Returns a map of Key-Value of all the given keys. If the keys are not in the cache and some are not computable using the loader function, it will return the result of executing the provided mappingFunction for those keys. A single request tomappingFunction
is performed for all keys which are not already present in the cache The returned map contains entries that were already cached, combined with the newly loaded entries. It will never contain null keys or values. If a key is not computable to a value, it will not appear in the resulting Map.- Parameters:
keys
- Collection of keys to retrieve values frommappingFunction
- Mapping function that will compute the values of the keys if not present in the cache- Returns:
- map of Key-Value of all the given keys
- Throws:
NullPointerException
- if any of the specified keys is null (not the value associated with the key)
-
invalidate
public void invalidate(K key)
Invalidates given key in the cache
-
invalidateAll
public void invalidateAll()
Invalidates all the keys in the cache
-
getName
public String getName()
Name of the cache- Returns:
- Name of the cache
-
-