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
Modifier and TypeClassDescriptionprotected static final class
Encapsulation of name and description of a cachestatic enum
The type of cache -
Field Summary
Modifier and TypeFieldDescriptionprotected Context
Avalon contextMap linking id with persistent cachesprotected Map<String,
List<WeakReference<Cache>>> HashMap linking an id with a List of WeakReference of Caches.protected RequestListenerManager
RequestListener managerprotected Map<String,
AbstractCacheManager.CacheInfo> Map linking id with CacheInfo of a request cacheprotected Map<String,
CacheStats> Map linking id with CacheStats of a request cachestatic final String
Role -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected 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 the cache by id.Get all caches classified by Identifier and CacheType.Get all the memory caches identified by IdGet list of memory caches in JSON formatboolean
Returns true if the cache with given id existsvoid
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
Called at the end of the HttpServlet.service() methodvoid
Called at the beginning of the HttpServlet.service() methodReset all memory cachesresetCaches
(List<String> ids) Reset the cache with provided idsvoid
service
(ServiceManager serviceManager) boolean
set new max size to the cache related to given idMethods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
Field Details
-
ROLE
Role -
_memoryCaches
Map linking id with persistent caches -
_requestsCacheInfos
Map linking id with CacheInfo of a request cache -
_requestsCacheStats
Map linking id with CacheStats of a request cache -
_requestCaches
HashMap linking an id with a List of WeakReference of Caches. this mean values can be destroyed by the garbage collector -
_context
Avalon context -
_requestListenerManager
RequestListener manager
-
-
Constructor Details
-
AbstractCacheManager
public AbstractCacheManager()
-
-
Method Details
-
initialize
- Specified by:
initialize
in interfaceInitializable
- Throws:
Exception
-
service
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
requestEnded
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
Called whenever a request caches' statistics need to be manually refreshed- Parameters:
req
- the processed requestid
- the if of the cache to refresh
-
requestStarted
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
- 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
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
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
Get all the memory caches identified by Id- Returns:
- all memory caches
-
getAllCaches
Get all caches classified by Identifier and CacheType. All caches includes all running request caches in any existing request.- Returns:
- all cache
-
getCachesAsJSONMap
Get list of memory caches in JSON format- Returns:
- the memory caches in JSON format
-
hasCache
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
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
-
resetAllMemoryCaches
Reset all memory caches- Returns:
- the list of reseted cache's id
-
resetCaches
Reset the cache with provided ids- Parameters:
ids
- the list of cache to reset- Returns:
- the ids of cache that were actually reseted
-