Package org.dspace.services.caching
Class CachingServiceImpl
- java.lang.Object
-
- org.dspace.services.caching.CachingServiceImpl
-
- All Implemented Interfaces:
ConfigChangeListener
,CachingService
public final class CachingServiceImpl extends Object implements CachingService, ConfigChangeListener
Implementation of the core caching service, which is available for anyone who is writing code for DSpace to use.- Author:
- Aaron Zeckoski (azeckoski @ gmail.com)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CachingServiceImpl.NameComparator
Compare two Cache objects by name.
-
Field Summary
Fields Modifier and Type Field Description protected net.sf.ehcache.CacheManager
cacheManager
The underlying cache manager; injected.protected static String
DEFAULT_CONFIG
The default configuration location.protected static String
EVENT_RESET
This is the event key for a full cache reset.-
Fields inherited from interface org.dspace.services.CachingService
REQUEST_CACHE
-
-
Constructor Summary
Constructors Constructor Description CachingServiceImpl()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
configurationChanged(List<String> changedSettingNames, Map<String,String> changedSettings)
Called whenever the configuration settings change (depending on the filter).void
destroyCache(String cacheName)
Flushes and destroys the cache with this name.protected static String
generateCacheStats(net.sf.ehcache.Ehcache cache)
Generate some stats for this cache.protected List<net.sf.ehcache.Ehcache>
getAllEhCaches(boolean sorted)
Return all caches from the CacheManager.Cache
getCache(String cacheName, CacheConfig cacheConfig)
Construct a Cache with the given name OR retrieve the one that already exists with this name.net.sf.ehcache.CacheManager
getCacheManager()
List<Cache>
getCaches()
Gets all the caches that the service knows about.protected Map<String,MapCache>
getRequestCaches()
String
getStatus(String cacheName)
Get a status report of cache usage which is suitable for log or screen output.void
init()
protected EhcacheCache
instantiateEhCache(String cacheName, CacheConfig cacheConfig)
Create an EhcacheCache (and the associated EhCache) using the supplied name (with default settings), or get the cache out of Spring or the current configured cache.protected MapCache
instantiateMapCache(String cacheName, CacheConfig cacheConfig)
Create a thread map cache using the supplied name with supplied settings.String[]
notifyForConfigNames()
Allows the listener to filter the change notifications so it is only notified when the named configuration items change.protected void
reloadConfig()
Reloads the configuration settings from the configuration service.void
resetCaches()
Clears all caches.void
setCacheManager(net.sf.ehcache.CacheManager cacheManager)
void
setConfigurationService(ConfigurationService configurationService)
void
setRequestService(RequestService requestService)
void
setServiceManager(ServiceManager serviceManager)
void
shutdown()
void
unbindRequestCaches()
Unbinds all request caches.
-
-
-
Field Detail
-
EVENT_RESET
protected static final String EVENT_RESET
This is the event key for a full cache reset.- See Also:
- Constant Field Values
-
DEFAULT_CONFIG
protected static final String DEFAULT_CONFIG
The default configuration location.- See Also:
- Constant Field Values
-
cacheManager
protected net.sf.ehcache.CacheManager cacheManager
The underlying cache manager; injected.
-
-
Method Detail
-
getRequestCaches
protected Map<String,MapCache> getRequestCaches()
- Returns:
- the current request map which is bound to the current thread
-
unbindRequestCaches
public void unbindRequestCaches()
Unbinds all request caches. Destroys the caches completely.- Specified by:
unbindRequestCaches
in interfaceCachingService
-
setConfigurationService
@Autowired(required=true) public void setConfigurationService(ConfigurationService configurationService)
-
setRequestService
@Autowired public void setRequestService(RequestService requestService)
-
setServiceManager
@Autowired(required=true) public void setServiceManager(ServiceManager serviceManager)
-
setCacheManager
@Autowired(required=true) public void setCacheManager(net.sf.ehcache.CacheManager cacheManager)
-
getCacheManager
public net.sf.ehcache.CacheManager getCacheManager()
-
reloadConfig
protected void reloadConfig()
Reloads the configuration settings from the configuration service.
-
notifyForConfigNames
public String[] notifyForConfigNames()
Description copied from interface:ConfigChangeListener
Allows the listener to filter the change notifications so it is only notified when the named configuration items change. For example, if this method returns an array containing"upload.enabled"
then whenever this configuration setting changes the listener will be called. If any other settings change the listener will not be called unless they are specific bean properties for this service (e.g.[email protected]
). If you want to be notified when any configuration setting changes then simply return a null or an empty string and the listener will be called for every configuration update.- Specified by:
notifyForConfigNames
in interfaceConfigChangeListener
- Returns:
- an array of configuration string names (e.g. {"system.name","upload.enabled"}) OR null/empty to be notified for every configuration setting that changes
-
configurationChanged
public void configurationChanged(List<String> changedSettingNames, Map<String,String> changedSettings)
Description copied from interface:ConfigChangeListener
Called whenever the configuration settings change (depending on the filter). This will only be called once for each config update regardless of the number of settings that were actually changed.NOTE: This will strip off the beanName from any service property settings. Example: [email protected] => downloadEnabled
- Specified by:
configurationChanged
in interfaceConfigChangeListener
- Parameters:
changedSettingNames
- includes the names of all settings that changedchangedSettings
- includes the map of all settings that changed
-
init
@PostConstruct public void init()
-
shutdown
@PreDestroy public void shutdown()
-
destroyCache
public void destroyCache(String cacheName)
Description copied from interface:CachingService
Flushes and destroys the cache with this name. Generally there is no reason to call this.- Specified by:
destroyCache
in interfaceCachingService
- Parameters:
cacheName
- the unique name for this cache (e.g. org.dspace.user.UserCache)
-
getCache
public Cache getCache(String cacheName, CacheConfig cacheConfig)
Description copied from interface:CachingService
Construct a Cache with the given name OR retrieve the one that already exists with this name. Often the name is the fully qualified classpath of the API for the service that is being cached, or of the class if there is no API. This will operate on system defaults (probably a distributed cache without replication) OR it will use the defaults which are configured for this cacheName (part of the underlying implementation) if the cacheConfig is null.This can only retrieve request caches for the current request.
If the cache already exists then the cacheConfig is ignored.
- Specified by:
getCache
in interfaceCachingService
- Parameters:
cacheName
- the unique name for this cache (e.g. org.dspace.user.UserCache)cacheConfig
- defines the configuration for this cache- Returns:
- a cache which can be used to store objects
-
getCaches
public List<Cache> getCaches()
Description copied from interface:CachingService
Gets all the caches that the service knows about. This will include caches of all scopes but only includes request caches for the current thread.- Specified by:
getCaches
in interfaceCachingService
- Returns:
- a list of all the caches which the caching service knows about
-
getStatus
public String getStatus(String cacheName)
Description copied from interface:CachingService
Get a status report of cache usage which is suitable for log or screen output.- Specified by:
getStatus
in interfaceCachingService
- Parameters:
cacheName
- (optional) the unique name for this cache (e.g. org.dspace.user.UserCache) OR null for status of all caches- Returns:
- a string representing the current status of the specified cache or all caches
-
resetCaches
public void resetCaches()
Description copied from interface:CachingService
Clears all caches. Generally there is no reason to call this.- Specified by:
resetCaches
in interfaceCachingService
-
getAllEhCaches
protected List<net.sf.ehcache.Ehcache> getAllEhCaches(boolean sorted)
Return all caches from the CacheManager.- Parameters:
sorted
- if true then sort by name- Returns:
- the list of all known ehcaches
-
instantiateEhCache
protected EhcacheCache instantiateEhCache(String cacheName, CacheConfig cacheConfig)
Create an EhcacheCache (and the associated EhCache) using the supplied name (with default settings), or get the cache out of Spring or the current configured cache.This expects that the cacheRecord has already been checked and will not check it again.
Will proceed in this order:
- Attempt to load a bean with the name of the cache
- Attempt to load cache from caching system
- Create a new cache by this name
- Put the cache in the cache record
- Parameters:
cacheName
- the name of the cachecacheConfig
- the config for this cache- Returns:
- a cache instance
-
instantiateMapCache
protected MapCache instantiateMapCache(String cacheName, CacheConfig cacheConfig)
Create a thread map cache using the supplied name with supplied settings.This expects that the cacheRecord has already been checked and will not check it again. It also places the cache into the request map.
- Parameters:
cacheName
- the name of the cachecacheConfig
- the config for this cache- Returns:
- a cache instance
-
generateCacheStats
protected static String generateCacheStats(net.sf.ehcache.Ehcache cache)
Generate some stats for this cache. Note that this is not cheap so do not use it very often.- Parameters:
cache
- an Ehcache- Returns:
- the stats of this cache as a string
-
-