Package org.ametys.core.cache
Interface Cache<K,V>
- Type Parameters:
K
- the type of the keys in cacheV
- the type of the values in cache
- All Known Implementing Classes:
GuavaCache
public interface Cache<K,V>
A cache represents a store of objects indexed with a key
-
Method Summary
Modifier and TypeMethodDescriptionasMap()
Get the cache as a simple MapReturns the value associated withkey
in this cache if it existReturns the value associated withkey
in this cache if it exist, obtaining that value from given function if necessary and store it.getAll
(AbstractCacheKey key) Get the cache as a simple Map, filtered by filled values of the cache keyGet all statistics about cacheGet the description of cachegetId()
Get the id of cachegetLabel()
Get the label of cachelong
Get the max size allocated to the cache, in bytes.long
Get the size the cache take in byteslong
Get the number of values in cacheboolean
return true if key is present in cachevoid
invalidate
(K key) Discards any cached value for keykey
.void
Discards all entries in the cache, without reseting statisticsboolean
Is this cache size computableboolean
Can the cache be transmitted in sub-requests of DispatchGeneratorboolean
Is this cache filled at least once.void
Associatesvalue
withkey
in this cache.void
Copies all of the mappings from the specified map to the cache.void
Create or reset the whole cache (including statistics).
The CACHE_RESET event is not notified by this method, but should be by callers.Get the definition in JSON Map
-
Method Details
-
get
Returns the value associated withkey
in this cache if it exist, obtaining that value from given function if necessary and store it.- Parameters:
key
- the key to get the elementfunction
- the function to compute the value if key is not present- Returns:
- V the value associated with key
- Throws:
CacheException
- throw CacheException if the key is null or invalid, or if failed to compute the new value
-
get
Returns the value associated withkey
in this cache if it exist- Parameters:
key
- the key to get the element- Returns:
- V the value linked to key
-
getAll
Get the cache as a simple Map, filtered by filled values of the cache key- Parameters:
key
- the key to get the element.- Returns:
- the Map<K, V> representing the cache
-
put
Associatesvalue
withkey
in this cache. If the cache previously contained a value associated withkey
, the old value is replaced byvalue
. Thekey
can not be null.- Parameters:
key
- the key to get the elementvalue
- the value associated with key
-
putAll
Copies all of the mappings from the specified map to the cache.- Parameters:
map
- map of key/values to be stored in the cache
-
hasKey
return true if key is present in cache- Parameters:
key
- the key to be checked- Returns:
- true if key is present in cache
-
invalidate
Discards any cached value for keykey
.- Parameters:
key
- the key to invalidate
-
invalidateAll
void invalidateAll()Discards all entries in the cache, without reseting statistics -
resetCache
void resetCache()Create or reset the whole cache (including statistics).
The CACHE_RESET event is not notified by this method, but should be by callers. -
getCacheStats
Get all statistics about cache- Returns:
- statistic about the cache (hit, miss, evictions...)
-
getMemorySize
Get the size the cache take in bytes- Returns:
- the size of the cache if it's computable, return 0 otherwise
- Throws:
CacheException
- throw CacheException if memory size can not be computed
-
getMaxSize
long getMaxSize()Get the max size allocated to the cache, in bytes. This value can be set to Long.MAX_VALUE for infinite cache- Returns:
- the max size allocated to the cache
-
getNumberOfElements
long getNumberOfElements()Get the number of values in cache- Returns:
- the number of values in cache
-
getLabel
Get the label of cache- Returns:
- the label of cache
-
getDescription
Get the description of cache- Returns:
- the description of cache
-
getId
Get the id of cache- Returns:
- the id of cache
-
isComputableSize
boolean isComputableSize()Is this cache size computable- Returns:
- true if the cache size is computable
-
asMap
Get the cache as a simple Map- Returns:
- the Map<K, V> representing the cache
-
toJSONMap
Get the definition in JSON Map- Parameters:
logger
- Logger to log an error when the size cannot be computed- Returns:
- the JSON for the cache
-
isInitialized
boolean isInitialized()Is this cache filled at least once.- Returns:
- true if the cache have been filled (put or putAll have been called even without values), false when the cache is new, reseted or if all values have been cleaned
-
isDispatchable
boolean isDispatchable()Can the cache be transmitted in sub-requests of DispatchGenerator- Returns:
- true if the cache can be transmitted in sub-requests of DispatchGenerator
-