Class GuavaCache<K,V>
- java.lang.Object
-
- org.ametys.plugins.core.impl.cache.GuavaCache<K,V>
-
- Type Parameters:
K
- the keyV
- the value
- All Implemented Interfaces:
Cache<K,V>
public class GuavaCache<K,V> extends Object implements Cache<K,V>
Implementation of AmetysCache with Guava library
-
-
Field Summary
Fields Modifier and Type Field Description protected Cache<K,Optional<V>>
_cache
the cache itself, using Guava implementationprotected boolean
_computableSize
True if the size of the cache can be computed, false otherwise (e.g., the cache store a component)protected I18nizableText
_description
Description of the cacheprotected Duration
_duration
The length of time after an entry is created that it should be automaticallyprotected String
_id
Id of the cacheprotected boolean
_isDispatchable
True if the cache can be transmitted in sub-requests of DispatchGenerator, false otherwises.protected boolean
_isInitialized
True if the put or putAll have been called (even without values), false otherwises.protected I18nizableText
_label
Label of the cacheprotected long
_size
Maximum size of the cache in bytes
-
Constructor Summary
Constructors Constructor Description GuavaCache(String id, I18nizableText label, I18nizableText description, long size, boolean computableSize, Duration duration, boolean isDispatchable)
Implementation of AmetysCache with Guava library
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private boolean
_checkKey(K key, List<Object> fields)
Map<K,V>
asMap()
Get the cache as a simple MapV
get(K key)
Returns the value associated withkey
in this cache if it existV
get(K key, Function<K,V> function)
Returns the value associated withkey
in this cache if it exist, obtaining that value from given function if necessary and store it.Map<K,V>
getAll(AbstractCacheKey filterKey)
Get the cache as a simple Map, filtered by filled values of the cache keyCacheStats
getCacheStats()
Get all statistics about cacheI18nizableText
getDescription()
Get the description of cacheString
getId()
Get the id of cacheI18nizableText
getLabel()
Get the label of cachelong
getMaxSize()
Get the max size allocated to the cache, in bytes.long
getMemorySize()
Get the size the cache take in byteslong
getNumberOfElements()
Get the number of values in cacheboolean
hasKey(K key)
return true if key is present in cachevoid
invalidate(K key)
Discards any cached value for keykey
.void
invalidateAll()
Discards all entries in the cache, without reseting statisticsboolean
isComputableSize()
Is this cache size computableboolean
isDispatchable()
Can the cache be transmitted in sub-requests of DispatchGeneratorboolean
isInitialized()
Is this cache filled at least once.void
put(K key, V value)
Associatesvalue
withkey
in this cache.void
putAll(Map<K,V> map)
Copies all of the mappings from the specified map to the cache.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.
-
-
-
Field Detail
-
_label
protected I18nizableText _label
Label of the cache
-
_description
protected I18nizableText _description
Description of the cache
-
_size
protected long _size
Maximum size of the cache in bytes
-
_computableSize
protected boolean _computableSize
True if the size of the cache can be computed, false otherwise (e.g., the cache store a component)
-
_duration
protected Duration _duration
The length of time after an entry is created that it should be automatically
-
_isInitialized
protected boolean _isInitialized
True if the put or putAll have been called (even without values), false otherwises. become false again when the cache is reseted or if all values have been cleaned.
-
_isDispatchable
protected boolean _isDispatchable
True if the cache can be transmitted in sub-requests of DispatchGenerator, false otherwises.
-
-
Constructor Detail
-
GuavaCache
public GuavaCache(String id, I18nizableText label, I18nizableText description, long size, boolean computableSize, Duration duration, boolean isDispatchable)
Implementation of AmetysCache with Guava library- Parameters:
id
- id of the cachelabel
- label of the cachedescription
- descriptionsize
- maximum size of the cachecomputableSize
- true if the size of the cache can be computedduration
- the length of time after an entry is created that it should be automatically removedisDispatchable
- true if the cache can be transmitted in sub-requests of DispatchGenerator
-
-
Method Detail
-
resetCache
public void resetCache()
Description copied from interface:Cache
Create or reset the whole cache (including statistics).
The CACHE_RESET event is not notified by this method, but should be by callers.- Specified by:
resetCache
in interfaceCache<K,V>
-
get
public V get(K key, Function<K,V> function) throws CacheException
Description copied from interface:Cache
Returns the value associated withkey
in this cache if it exist, obtaining that value from given function if necessary and store it.- Specified by:
get
in interfaceCache<K,V>
- 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
public V get(K key)
Description copied from interface:Cache
Returns the value associated withkey
in this cache if it exist
-
put
public void put(K key, V value)
Description copied from interface:Cache
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.
-
putAll
public void putAll(Map<K,V> map)
Description copied from interface:Cache
Copies all of the mappings from the specified map to the cache.
-
getCacheStats
public CacheStats getCacheStats()
Description copied from interface:Cache
Get all statistics about cache- Specified by:
getCacheStats
in interfaceCache<K,V>
- Returns:
- statistic about the cache (hit, miss, evictions...)
-
getMemorySize
public long getMemorySize() throws CacheException
Description copied from interface:Cache
Get the size the cache take in bytes- Specified by:
getMemorySize
in interfaceCache<K,V>
- Returns:
- the size of the cache if it's computable, return 0 otherwise
- Throws:
CacheException
- throw CacheException if memory size can not be computed
-
getNumberOfElements
public long getNumberOfElements()
Description copied from interface:Cache
Get the number of values in cache- Specified by:
getNumberOfElements
in interfaceCache<K,V>
- Returns:
- the number of values in cache
-
invalidate
public void invalidate(K key)
Description copied from interface:Cache
Discards any cached value for keykey
.- Specified by:
invalidate
in interfaceCache<K,V>
- Parameters:
key
- the key to invalidate
-
invalidateAll
public void invalidateAll()
Description copied from interface:Cache
Discards all entries in the cache, without reseting statistics- Specified by:
invalidateAll
in interfaceCache<K,V>
-
getDescription
public I18nizableText getDescription()
Description copied from interface:Cache
Get the description of cache- Specified by:
getDescription
in interfaceCache<K,V>
- Returns:
- the description of cache
-
getLabel
public I18nizableText getLabel()
Description copied from interface:Cache
Get the label of cache
-
getMaxSize
public long getMaxSize()
Description copied from interface:Cache
Get the max size allocated to the cache, in bytes. This value can be set to Long.MAX_VALUE for infinite cache- Specified by:
getMaxSize
in interfaceCache<K,V>
- Returns:
- the max size allocated to the cache
-
hasKey
public boolean hasKey(K key)
Description copied from interface:Cache
return true if key is present in cache
-
asMap
public Map<K,V> asMap()
Description copied from interface:Cache
Get the cache as a simple Map
-
isComputableSize
public boolean isComputableSize()
Description copied from interface:Cache
Is this cache size computable- Specified by:
isComputableSize
in interfaceCache<K,V>
- Returns:
- true if the cache size is computable
-
isInitialized
public boolean isInitialized()
Description copied from interface:Cache
Is this cache filled at least once.- Specified by:
isInitialized
in interfaceCache<K,V>
- 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
public boolean isDispatchable()
Description copied from interface:Cache
Can the cache be transmitted in sub-requests of DispatchGenerator- Specified by:
isDispatchable
in interfaceCache<K,V>
- Returns:
- true if the cache can be transmitted in sub-requests of DispatchGenerator
-
getAll
public Map<K,V> getAll(AbstractCacheKey filterKey)
Description copied from interface:Cache
Get the cache as a simple Map, filtered by filled values of the cache key
-
-