Class CachingServiceImpl
- java.lang.Object
-
- org.dspace.services.caching.CachingServiceImpl
-
- All Implemented Interfaces:
ConfigChangeListener
,ServiceChangeListener
,CachingService
public final class CachingServiceImpl extends Object implements CachingService, ConfigChangeListener, ServiceChangeListener
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()
CacheProvider
getCacheProvider()
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.Class<?>[]
notifyForTypes()
Allows filtering so that notifications are only sent when classes implement one of the types specified.protected void
reloadConfig()
Reloads the configuration settings from the configuration service.void
resetCaches()
Clears all caches.void
serviceRegistered(String serviceName, Object service, List<Class<?>> implementedTypes)
This will be called when services are newly registered with the service manager.void
serviceUnregistered(String serviceName, Object service)
This will be called when services are removed from the service manager.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
-
getCacheProvider
public CacheProvider getCacheProvider()
-
notifyForTypes
public Class<?>[] notifyForTypes()
Description copied from interface:ServiceChangeListener
Allows filtering so that notifications are only sent when classes implement one of the types specified. The listener method will only be called once regardless of how many types match.Just return null or empty array to be notified for all service registrations.
- Specified by:
notifyForTypes
in interfaceServiceChangeListener
- Returns:
- an array of classes OR null to be notified of all service registrations
-
serviceRegistered
public void serviceRegistered(String serviceName, Object service, List<Class<?>> implementedTypes)
Description copied from interface:ServiceChangeListener
This will be called when services are newly registered with the service manager. It will not be called when the core services are starting up though.It is not called until the service is fully initialized. It is called once and only once per service that is registered.
- Specified by:
serviceRegistered
in interfaceServiceChangeListener
- Parameters:
serviceName
- the name of the serviceservice
- the service beanimplementedTypes
- a list of all the class types which this service implements
-
serviceUnregistered
public void serviceUnregistered(String serviceName, Object service)
Description copied from interface:ServiceChangeListener
This will be called when services are removed from the service manager. Services which are replaced will not have this method called and will only receiveServiceChangeListener.serviceRegistered(String, Object, List)
.It is called immediately before the service is completely destroyed so that the service object is still valid.
- Specified by:
serviceUnregistered
in interfaceServiceChangeListener
- Parameters:
serviceName
- the name of the serviceservice
- the service bean
-
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
-
-