Package org.ametys.core.cache
Class AbstractCacheManager
- java.lang.Object
-
- org.ametys.runtime.plugin.component.AbstractLogEnabled
-
- org.ametys.core.cache.AbstractCacheManager
-
- All Implemented Interfaces:
LogEnabled
,Component
,Contextualizable
- Direct Known Subclasses:
GuavaCacheManager
public abstract class AbstractCacheManager extends AbstractLogEnabled implements Component, Contextualizable
Component that handle all the caches
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractCacheManager.CacheInfo
Encapsulation of name and description of a cachestatic class
AbstractCacheManager.CacheType
The type of cache
-
Field Summary
Fields Modifier and Type Field Description protected Context
_context
Avalon contextprotected Map<String,Cache>
_memoryCaches
Map linking id with persistent cachesprotected Map<String,List<WeakReference<Cache>>>
_requestCaches
HashMap linking an id with a List of WeakReference of Caches.protected Map<String,AbstractCacheManager.CacheInfo>
_requestsCacheInfos
Map linking id with CacheInfo of a request cachestatic String
ROLE
Role
-
Constructor Summary
Constructors Constructor Description AbstractCacheManager()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract <K,V>
Cache<K,V>_createCache(String id, I18nizableText name, I18nizableText description, boolean computableSize)
create a new cachevoid
contextualize(Context context)
void
createCache(String id, I18nizableText name, I18nizableText description, AbstractCacheManager.CacheType cacheType, boolean computableSize)
create a new cache and store it in memoryCache map if it's a MEMORY CacheType, create a CacheInfo to create the cache later otherwise<K,V>
Cache<K,V>get(String id)
Get the cache by id.Map<Pair<String,AbstractCacheManager.CacheType>,List<Cache>>
getAllCaches()
Get all caches classified by Identifier and CacheType.List<Map<String,Object>>
getCachesAsJSONMap()
Get list of memory caches in JSON formatvoid
removeCache(String id, AbstractCacheManager.CacheType cacheType)
Remove the cache identified by the given id.boolean
setSize(String id, long size)
set new max size to the cache related to given id-
Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
-
-
-
Field Detail
-
_memoryCaches
protected Map<String,Cache> _memoryCaches
Map linking id with persistent caches
-
_requestsCacheInfos
protected Map<String,AbstractCacheManager.CacheInfo> _requestsCacheInfos
Map linking id with CacheInfo of a request cache
-
_requestCaches
protected Map<String,List<WeakReference<Cache>>> _requestCaches
HashMap linking an id with a List of WeakReference of Caches. this mean values can be destroyed by the garbage collector
-
-
Constructor Detail
-
AbstractCacheManager
public AbstractCacheManager()
-
-
Method Detail
-
contextualize
public void contextualize(Context context) throws ContextException
- Specified by:
contextualize
in interfaceContextualizable
- Throws:
ContextException
-
createCache
public void createCache(String id, I18nizableText name, I18nizableText description, AbstractCacheManager.CacheType cacheType, boolean computableSize) throws CacheException
create a new cache and store it in memoryCache map if it's a MEMORY CacheType, create a CacheInfo to create the cache later otherwise- Parameters:
id
- id of the cachename
- name of the cachedescription
- descriptioncacheType
- type of the cache (REQUEST or MEMORY)computableSize
- true if the size of the cache can be computed- Throws:
CacheException
- if a cache already exists for the id
-
removeCache
public void removeCache(String id, AbstractCacheManager.CacheType cacheType) throws CacheException
Remove the cache identified by the given id.- Parameters:
id
- id of the cachecacheType
- type of the cache- Throws:
CacheException
- if the cache does not exist for the id and type
-
get
public <K,V> Cache<K,V> get(String id) throws CacheException
Get the cache by id. If it's a request cache, create it and store it in request and in _requestCaches map.- Type Parameters:
K
- the type of the keys in cacheV
- the type of the values in cache- Parameters:
id
- id of the cache- Returns:
- the cache related to the id
- Throws:
CacheException
- if no cache exist for the id
-
getAllCaches
public Map<Pair<String,AbstractCacheManager.CacheType>,List<Cache>> getAllCaches()
Get all caches classified by Identifier and CacheType. All caches includes all running request caches in any existing request.- Returns:
- all cache
-
getCachesAsJSONMap
public List<Map<String,Object>> getCachesAsJSONMap()
Get list of memory caches in JSON format- Returns:
- the memory caches in JSON format
-
setSize
public boolean setSize(String id, long size) throws CacheException, UnsupportedOperationException
set new max size to the cache related to given id- Parameters:
id
- the id of cachesize
- the size of the cache in bytes- Returns:
- true if success
- Throws:
CacheException
- throw CacheException if the key is null or invalidUnsupportedOperationException
- not implemented yet
-
_createCache
protected abstract <K,V> Cache<K,V> _createCache(String id, I18nizableText name, I18nizableText description, boolean computableSize)
create a new cache- Type Parameters:
K
- Key type of the cacheV
- Value type of the cache- Parameters:
id
- the id of the cachename
- the name of the cachedescription
- the description of the cachecomputableSize
- true if the size of the cache can be computed- Returns:
- new cache
-
-