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
,RequestListener
,Initializable
,Component
,Contextualizable
,Serviceable
- Direct Known Subclasses:
GuavaCacheManager
public abstract class AbstractCacheManager extends AbstractLogEnabled implements Component, Serviceable, Contextualizable, Initializable, RequestListener
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 RequestListenerManager
_requestListenerManager
RequestListener managerprotected Map<String,AbstractCacheManager.CacheInfo>
_requestsCacheInfos
Map linking id with CacheInfo of a request cacheprotected Map<String,CacheStats>
_requestsCacheStats
Map linking id with CacheStats 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, Duration duration, boolean isDispatchable)
Create a new cacheprotected void
_createCache(String id, I18nizableText name, I18nizableText description, AbstractCacheManager.CacheType cacheType, boolean computableSize, Duration duration, boolean isDispatchable)
Create a new cache and store it in memoryCache map if it's a MEMORY CacheType, Create a CacheInfo to create the cache later otherwisevoid
contextualize(Context context)
void
createMemoryCache(String id, I18nizableText name, I18nizableText description, boolean computableSize, Duration duration)
Create a new cache and store it in memoryCache map if it's a MEMORY CacheType, Create a CacheInfo to create the cache later otherwisevoid
createRequestCache(String id, I18nizableText name, I18nizableText description, boolean isDispatchable)
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<Cache>
getAllMemoryCaches()
Get all the memory caches identified by IdList<Map<String,Object>>
getCachesAsJSONMap()
Get list of memory caches in JSON formatboolean
hasCache(String id)
Returns true if the cache with given id existsvoid
initialize()
void
refreshStats(Request req, String id)
Called whenever a request caches' statistics need to be manually refreshedvoid
removeCache(String id, AbstractCacheManager.CacheType cacheType)
Remove the cache identified by the given id.void
requestEnded(HttpServletRequest req)
Called at the end of the HttpServlet.service() methodvoid
requestStarted(HttpServletRequest req)
Called at the beginning of the HttpServlet.service() methodvoid
service(ServiceManager serviceManager)
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
-
_requestsCacheStats
protected Map<String,CacheStats> _requestsCacheStats
Map linking id with CacheStats 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
-
_requestListenerManager
protected RequestListenerManager _requestListenerManager
RequestListener manager
-
-
Constructor Detail
-
AbstractCacheManager
public AbstractCacheManager()
-
-
Method Detail
-
initialize
public void initialize() throws Exception
- Specified by:
initialize
in interfaceInitializable
- Throws:
Exception
-
service
public void service(ServiceManager serviceManager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
requestEnded
public void requestEnded(HttpServletRequest req)
Description copied from interface:RequestListener
Called at the end of the HttpServlet.service() method- Specified by:
requestEnded
in interfaceRequestListener
- Parameters:
req
- the processed request
-
refreshStats
public void refreshStats(Request req, String id)
Called whenever a request caches' statistics need to be manually refreshed- Parameters:
req
- the processed requestid
- the if of the cache to refresh
-
requestStarted
public void requestStarted(HttpServletRequest req)
Description copied from interface:RequestListener
Called at the beginning of the HttpServlet.service() method- Specified by:
requestStarted
in interfaceRequestListener
- Parameters:
req
- the request being processed
-
contextualize
public void contextualize(Context context) throws ContextException
- Specified by:
contextualize
in interfaceContextualizable
- Throws:
ContextException
-
createMemoryCache
public void createMemoryCache(String id, I18nizableText name, I18nizableText description, boolean computableSize, Duration duration) 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
- descriptioncomputableSize
- true if the size of the cache can be computedduration
- the length of time after an entry is created that it should be automatically removed. Only used for MEMORY type caches- Throws:
CacheException
- if a cache already exists for the id
-
createRequestCache
public void createRequestCache(String id, I18nizableText name, I18nizableText description, boolean isDispatchable) 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
- descriptionisDispatchable
- true if the cache can be transmitted in sub-requests of DispatchGenerator- Throws:
CacheException
- if a cache already exists for the id
-
_createCache
protected void _createCache(String id, I18nizableText name, I18nizableText description, AbstractCacheManager.CacheType cacheType, boolean computableSize, Duration duration, boolean isDispatchable) 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 computedduration
- the length of time after an entry is created that it should be automatically removed. Only used for MEMORY type cachesisDispatchable
- true if the cache can be transmitted in sub-requests of DispatchGenerator- 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
-
getAllMemoryCaches
public List<Cache> getAllMemoryCaches()
Get all the memory caches identified by Id- Returns:
- all memory caches
-
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
-
hasCache
public boolean hasCache(String id)
Returns true if the cache with given id exists- Parameters:
id
- id of the cache- Returns:
- true if the cache with given id exists
-
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, Duration duration, boolean isDispatchable)
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 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- Returns:
- new cache
-
-