public class CacheManager extends Object
Ehcache
s that maintain all aspects of their lifecycle.
CacheManager may be either be a singleton if created with factory methods, or multiple instances may exist, in which case resources
required by each must be unique.
A CacheManager holds references to Caches and Ehcaches and manages their creation and lifecycle.Modifier and Type | Field and Description |
---|---|
static List<CacheManager> |
ALL_CACHE_MANAGERS
Keeps track of all known CacheManagers.
|
protected CacheManagerEventListenerRegistry |
cacheManagerEventListenerRegistry
The listener registry
|
protected Map<String,CacheManagerPeerListener> |
cacheManagerPeerListeners
The map of listeners
|
protected Map<String,CacheManagerPeerProvider> |
cacheManagerPeerProviders
The map of providers
|
static String |
DEFAULT_NAME
Default name if not specified in the configuration/
|
static String |
ENABLE_SHUTDOWN_HOOK_PROPERTY
System property to enable creation of a shutdown hook for CacheManager.
|
static double |
ON_HEAP_THRESHOLD
Threshold, in percent of the available heap, above which the CacheManager will warn if the configured memory
|
protected Thread |
shutdownHook
The shutdown hook thread for CacheManager.
|
protected Status |
status
Status of the Cache Manager
|
Constructor and Description |
---|
CacheManager()
Constructor.
|
CacheManager(Configuration configuration)
An constructor for CacheManager, which takes a configuration object, rather than one created by parsing
an ehcache.xml file.
|
CacheManager(InputStream configurationInputStream)
An ordinary constructor for CacheManager.
|
CacheManager(String configurationFileName)
An ordinary constructor for CacheManager.
|
CacheManager(URL configurationURL)
An ordinary constructor for CacheManager.
|
Modifier and Type | Method and Description |
---|---|
void |
addCache(Cache cache)
Adds a
Cache to the CacheManager. |
void |
addCache(Ehcache cache)
Adds an
Ehcache to the CacheManager. |
void |
addCache(String cacheName)
Adds a
Ehcache based on the defaultCache with the given name. |
Ehcache |
addCacheIfAbsent(Ehcache cache)
Only adds the cache to the CacheManager should not one with the same name already be present
|
Ehcache |
addCacheIfAbsent(String cacheName)
Only creates and adds the cache to the CacheManager should not one with the same name already be present
|
void |
addDecoratedCache(Ehcache decoratedCache)
Adds a decorated
Ehcache to the CacheManager. |
void |
addDecoratedCacheIfAbsent(Ehcache decoratedCache)
Same as
addDecoratedCache(Ehcache) but does not throw exception if another cache with same name already exists. |
boolean |
cacheExists(String cacheName)
Checks whether a cache of type ehcache exists.
|
protected void |
checkStatus()
Checks the state of the CacheManager for legal operation
|
void |
clearAll()
Clears the contents of all caches in the CacheManager, but without
removing any caches.
|
void |
clearAllStartingWith(String prefix)
Clears the contents of all caches in the CacheManager with a name starting with the prefix,
but without removing them.
|
static CacheManager |
create()
A factory method to create a singleton CacheManager with default config, or return it if it exists.
|
static CacheManager |
create(Configuration config)
A factory method to create a singleton CacheManager from a net.sf.ehcache.config.Configuration.
|
static CacheManager |
create(InputStream inputStream)
A factory method to create a singleton CacheManager from a java.io.InputStream.
|
static CacheManager |
create(String configurationFileName)
A factory method to create a singleton CacheManager with a specified configuration.
|
static CacheManager |
create(URL configurationFileURL)
A factory method to create a singleton CacheManager from an URL.
|
CacheEventListener |
createTerracottaEventReplicator(Ehcache cache)
Create/access the appropriate clustered cache event replicator for the given cache
|
Store |
createTerracottaStore(Ehcache cache)
Create/access the appropriate terracotta clustered store for the given cache
|
WriteBehind |
createTerracottaWriteBehind(Ehcache cache)
Create/access the appropriate clustered write behind queue for the given cache
|
String |
getActiveConfigurationText()
Returns the active configuration text for this
CacheManager |
String |
getActiveConfigurationText(String cacheName)
Returns the active configuration text for the input cacheName
|
Cache |
getCache(String name)
Returns a concrete implementation of Cache, it it is available in the CacheManager.
|
static CacheManager |
getCacheManager(String name)
Checks if a cacheManager already exists for a given name and gets it.
|
CacheManagerEventListener |
getCacheManagerEventListener()
Returns the composite listener.
|
CacheManagerEventListenerRegistry |
getCacheManagerEventListenerRegistry()
Gets the CacheManagerEventListenerRegistry.
|
CacheManagerPeerProvider |
getCacheManagerPeerProvider(String scheme)
Gets the
CacheManagerPeerProvider , matching the given scheme
For distributed caches, the peer provider finds other cache managers and their caches in the same cluster |
Map<String,CacheManagerPeerProvider> |
getCacheManagerPeerProviders() |
String[] |
getCacheNames()
Returns a list of the current cache names.
|
CacheManagerPeerListener |
getCachePeerListener(String scheme)
When CacheManage is configured as part of a cluster, a CacheManagerPeerListener will
be registered in it.
|
CacheCluster |
getCluster(ClusterScheme scheme)
Returns access to information about the cache cluster.
|
protected ClusteredInstanceFactory |
getClusteredInstanceFactory(Ehcache cache)
Return the clustered instance factory for a cache of this cache manager.
|
String |
getClusterUUID()
Returns unique cluster-wide id for this cache-manager.
|
Configuration |
getConfiguration()
Get the CacheManager configuration
|
DiskStorePathManager |
getDiskStorePathManager()
Returns the disk store path manager.
|
Ehcache |
getEhcache(String name)
Gets an Ehcache
|
FeaturesManager |
getFeaturesManager()
Get the features manager.
|
static CacheManager |
getInstance()
A factory method to create a singleton CacheManager with default config, or return it if it exists.
|
String |
getName()
Gets the name of the CacheManager.
|
protected NonstopExecutorService |
getNonstopExecutorService()
Return the
NonstopExecutorService associated with this cacheManager |
Pool |
getOnDiskPool()
Return this cache manager's shared on-disk pool
|
Pool |
getOnHeapPool()
Return this cache manager's shared on-heap pool
|
TransactionIDFactory |
getOrCreateTransactionIDFactory()
Get or create a TransactionIDFactory
|
String |
getOriginalConfigurationText()
Returns the original configuration text for this
CacheManager |
String |
getOriginalConfigurationText(String cacheName)
Returns the original configuration text for the input cacheName
|
Status |
getStatus()
Gets the status attribute of the Ehcache
|
FailSafeTimer |
getTimer()
Returns a
FailSafeTimer associated with this CacheManager |
TransactionController |
getTransactionController()
Get the TransactionController
|
protected void |
init(Configuration initialConfiguration,
String configurationFileName,
URL configurationURL,
InputStream configurationInputStream)
initialises the CacheManager
|
boolean |
isNamed()
Indicate whether the CacheManager is named or not.
|
static CacheManager |
newInstance()
A factory method to create a CacheManager with default config, or return it if it exists.
|
static CacheManager |
newInstance(Configuration config)
A factory method to create a CacheManager from a net.sf.ehcache.config.Configuration.
|
static CacheManager |
newInstance(InputStream inputStream)
A factory method to create a CacheManager from a java.io.InputStream.
|
static CacheManager |
newInstance(String configurationFileName)
A factory method to create a CacheManager with a specified configuration.
|
static CacheManager |
newInstance(URL configurationFileURL)
A factory method to create a CacheManager from an URL.
|
void |
removalAll()
Removes all caches using
removeCache(String) for each cache. |
void |
removeCache(String cacheName)
Remove a cache from the CacheManager.
|
void |
replaceCacheWithDecoratedCache(Ehcache ehcache,
Ehcache decoratedCache)
Replaces in the map of Caches managed by this CacheManager an Ehcache with a decorated version of the same
Ehcache.
|
void |
setCacheManagerEventListener(CacheManagerEventListener cacheManagerEventListener)
Same as getCacheManagerEventListenerRegistry().registerListener(cacheManagerEventListener);
Left for backward compatiblity
|
void |
setName(String name)
Sets the name of the CacheManager.
|
void |
shutdown()
Shuts down the CacheManager.
|
String |
toString() |
public static final String DEFAULT_NAME
public static final double ON_HEAP_THRESHOLD
public static final List<CacheManager> ALL_CACHE_MANAGERS
public static final String ENABLE_SHUTDOWN_HOOK_PROPERTY
protected volatile Status status
protected final Map<String,CacheManagerPeerProvider> cacheManagerPeerProviders
protected final Map<String,CacheManagerPeerListener> cacheManagerPeerListeners
protected final CacheManagerEventListenerRegistry cacheManagerEventListenerRegistry
protected Thread shutdownHook
public CacheManager(Configuration configuration) throws CacheException
create()
methods are called, a new singleton instance will be created, separate from any instances
created in this method.
Since 2.5, every newly created CacheManager is registered with its name (uses a default name if unnamed), and trying to create multiple
CacheManager with same names (or multiple unnamed CacheManagers) is not allowed and throws an exception.
It is recommended to use one of the newInstance()
methods to instantiate new CacheManagers as those methods return the same instance
of CacheManager for same names (or unnamed). Shutting down the CacheManager will deregister it and new ones can be created again.configuration
- CacheException
public CacheManager(String configurationFileName) throws CacheException
create()
methods are called, a new singleton will be created,
separate from any instances created in this method.
Since 2.5, every newly created CacheManager is registered with its name (uses a default name if unnamed), and trying to create multiple
CacheManager with same names (or multiple unnamed CacheManagers) is not allowed and throws an exception. Using any of the newInstance()
methods also registers the CacheManager with its name.
It is recommended to use one of the newInstance()
methods to instantiate new CacheManagers as those methods return the same instance
of CacheManager for same names (or unnamed). Shutting down the CacheManager will deregister it and new ones can be created again.configurationFileName
- an xml configuration file available through a file name. The configuration File
is created
using new File(configurationFileName)
CacheException
newInstance(String)
public CacheManager(URL configurationURL) throws CacheException
create()
methods are called, a new singleton will be created,
separate from any instances created in this method.
Since 2.5, every newly created CacheManager is registered with its name (uses a default name if unnamed), and trying to create multiple
CacheManager with same names (or multiple unnamed CacheManagers) is not allowed and throws an exception. Using any of the newInstance()
methods also registers the CacheManager with its name.
It is recommended to use one of the newInstance()
methods to instantiate new CacheManagers as those methods return the same instance
of CacheManager for same names (or unnamed). Shutting down the CacheManager will deregister it and new ones can be created again.
This method can be used to specify a configuration resource in the classpath other than the default of \"/ehcache.xml\":
URL url = this.getClass().getResource("/ehcache-2.xml");Note that
Class.getResource(String)
will look for resources in the same package unless a leading "/" is used, in which case it will
look in the root of the classpath.
You can also load a resource using other class loaders. e.g. Thread.getContextClassLoader()
configurationURL
- an xml configuration available through a URL.CacheException
newInstance(java.net.URL)
public CacheManager(InputStream configurationInputStream) throws CacheException
create()
methods are called, a new singleton will be created,
separate from any instances created in this method.
Since 2.5, every newly created CacheManager is registered with its name (uses a default name if unnamed), and trying to create multiple
CacheManager with same names (or multiple unnamed CacheManagers) is not allowed and throws an exception. Using any of the newInstance()
methods also registers the CacheManager with its name.
It is recommended to use one of the newInstance()
methods to instantiate new CacheManagers as those methods return the same instance
of CacheManager for same names (or unnamed). Shutting down the CacheManager will deregister it and new ones can be created again.configurationInputStream
- an xml configuration file available through an inputstreamCacheException
newInstance(java.io.InputStream)
public CacheManager() throws CacheException
newInstance()
methods also registers the CacheManager with its name.
It is recommended to use one of the newInstance()
methods to instantiate new CacheManagers as those methods return the same instance
of CacheManager for same names (or unnamed). Shutting down the CacheManager will deregister it and new ones can be created again.CacheException
protected void init(Configuration initialConfiguration, String configurationFileName, URL configurationURL, InputStream configurationInputStream)
public Pool getOnHeapPool()
public Pool getOnDiskPool()
public String getClusterUUID()
public Store createTerracottaStore(Ehcache cache)
cache
- The cache for which the Store should be createdpublic WriteBehind createTerracottaWriteBehind(Ehcache cache)
cache
- The cache for which the write behind queue should be createdpublic CacheEventListener createTerracottaEventReplicator(Ehcache cache)
cache
- The cache for which the clustered event replicator should be createdprotected ClusteredInstanceFactory getClusteredInstanceFactory(Ehcache cache)
cache
- the cache the clustered instance factory has to be returned forpublic static CacheManager create() throws CacheException
Ehcache
s created and required stores initialized. When the CacheManager
is no longer
required, call shutdown to free resources.CacheException
- if the CacheManager cannot be createdpublic static CacheManager newInstance() throws CacheException
Ehcache
s created and required stores initialized. When the CacheManager
is no longer
required, call shutdown to free resources.CacheException
- if the CacheManager cannot be createdpublic static CacheManager getInstance() throws CacheException
create()
Same as create()
CacheException
- if the CacheManager cannot be createdpublic static CacheManager create(String configurationFileName) throws CacheException
configurationFileName
- an xml file compliant with the ehcache.xsd schema
The configuration will be read, Ehcache
s created and required stores initialized. When the CacheManager
is
no longer required, call shutdown to free resources.CacheException
public static CacheManager newInstance(String configurationFileName) throws CacheException
If the specified configuration has different names for the CacheManager, it will return a new one for each unique name or return already created one.
configurationFileName
- an xml file compliant with the ehcache.xsd schema
The configuration will be read, Ehcache
s created and required stores initialized. When the CacheManager
is
no longer required, call shutdown to free resources.CacheException
public static CacheManager create(URL configurationFileURL) throws CacheException
URL url = this.getClass().getResource("/ehcache-2.xml");Note that
Class.getResource(String)
will look for resources in the same package unless a leading "/" is used, in which case it will
look in the root of the classpath.
You can also load a resource using other class loaders. e.g. Thread.getContextClassLoader()
configurationFileURL
- an URL to an xml file compliant with the ehcache.xsd schema
The configuration will be read, Ehcache
s created and required stores initialized. When the CacheManager
is
no longer required, call shutdown to free resources.CacheException
public static CacheManager newInstance(URL configurationFileURL) throws CacheException
URL url = this.getClass().getResource("/ehcache-2.xml");Note that
Class.getResource(String)
will look for resources in the same package unless a leading "/" is used, in which case it will
look in the root of the classpath.
You can also load a resource using other class loaders. e.g. Thread.getContextClassLoader()
If the specified configuration has different names for the CacheManager, it will return a new one for each unique name or return already created one.configurationFileURL
- an URL to an xml file compliant with the ehcache.xsd schema
The configuration will be read, Ehcache
s created and required stores initialized. When the CacheManager
is
no longer required, call shutdown to free resources.CacheException
public static CacheManager create(InputStream inputStream) throws CacheException
inputStream
- InputStream of xml compliant with the ehcache.xsd schema
The configuration will be read, Ehcache
s created and required stores initialized. When the CacheManager
is
no longer required, call shutdown to free resources.CacheException
public static CacheManager newInstance(InputStream inputStream) throws CacheException
inputStream
- InputStream of xml compliant with the ehcache.xsd schema
The configuration will be read, Ehcache
s created and required stores initialized. When the CacheManager
is
no longer required, call shutdown to free resources.CacheException
public static CacheManager create(Configuration config) throws CacheException
config
- CacheException
public static CacheManager newInstance(Configuration config)
config
- public static CacheManager getCacheManager(String name)
name
- the cacheManager name.name
is null,
returns the default unnamed cacheManager if it has been created
already otherwise returns nullpublic Cache getCache(String name) throws IllegalStateException, ClassCastException
Ehcache
decorator is present in the CacheManager, its not necessary that a
Cache
instance is also present for the same name. Decorators can have different names other than the name of the cache its
decorating.IllegalStateException
- if the cache is not Status.STATUS_ALIVE
ClassCastException
getEhcache(String)
public Ehcache getEhcache(String name) throws IllegalStateException
IllegalStateException
- if the cache is not Status.STATUS_ALIVE
public void addCache(String cacheName) throws IllegalStateException, ObjectExistsException, CacheException
Ehcache
based on the defaultCache with the given name.
Memory and Disk stores will be configured for it and it will be added to the map of caches.
Also notifies the CacheManagerEventListener after the cache was initialised and added.
It will be created with the defaultCache attributes specified in ehcache.xmlcacheName
- the name for the cacheObjectExistsException
- if the cache already existsCacheException
- if there was an error creating the cache.IllegalStateException
public void addCache(Cache cache) throws IllegalStateException, ObjectExistsException, CacheException
Cache
to the CacheManager.
Memory and Disk stores will be configured for it and it will be added to the map of caches. Also notifies the
CacheManagerEventListener after the cache was initialised and added.cache
- IllegalStateException
- if the cache is not Status.STATUS_UNINITIALISED
before this method is called.ObjectExistsException
- if the cache already exists in the CacheManagerCacheException
- if there was an error adding the cache to the CacheManagerpublic void addCache(Ehcache cache) throws IllegalStateException, ObjectExistsException, CacheException
Ehcache
to the CacheManager.
Memory and Disk stores will be configured for it and it will be added to the map of caches. Also notifies the
CacheManagerEventListener after the cache was initialised and added.cache
- IllegalStateException
- if the cache is not Status.STATUS_UNINITIALISED
before this method is called.ObjectExistsException
- if the cache already exists in the CacheManagerCacheException
- if there was an error adding the cache to the CacheManagerpublic void addDecoratedCache(Ehcache decoratedCache) throws ObjectExistsException
Ehcache
to the CacheManager. This method neither creates the memory/disk store
nor initializes the cache. It only adds the cache reference to the map of caches held by this
cacheManager.
It is generally required that a decorated cache, once constructed, is made available to other execution threads. The simplest way of
doing this is to either add it to the cacheManager with a different name or substitute the original cache with the decorated one.
This method adds the decorated cache assuming it has a different name. If another cache (decorated or not) with the same name already
exists, it will throw ObjectExistsException
. For replacing existing cache with another decorated cache having same name,
please use replaceCacheWithDecoratedCache(Ehcache, Ehcache)
Note that any overridden Ehcache methods by the decorator will take on new behaviours without casting. Casting is only required for
new methods that the decorator introduces. For more information see the well known Gang of Four Decorator pattern.decoratedCache
- ObjectExistsException
- if another cache with the same name already exists.public void addDecoratedCacheIfAbsent(Ehcache decoratedCache) throws ObjectExistsException
addDecoratedCache(Ehcache)
but does not throw exception if another cache with same name already exists.decoratedCache
- ObjectExistsException
public boolean cacheExists(String cacheName) throws IllegalStateException
cacheName
- the cache name to check forIllegalStateException
- if the cache is not Status.STATUS_ALIVE
public void removalAll()
removeCache(String)
for each cache.public void removeCache(String cacheName) throws IllegalStateException
cacheName
- the cache nameIllegalStateException
- if the cache is not Status.STATUS_ALIVE
public void shutdown()
public String[] getCacheNames() throws IllegalStateException
String
sIllegalStateException
- if the cache is not Status.STATUS_ALIVE
protected void checkStatus()
public Status getStatus()
public void clearAll() throws CacheException
Ehcache.removeAll()
mehod on each cache is called.CacheException
public void clearAllStartingWith(String prefix) throws CacheException
Ehcache.removeAll()
method on each cache is called.prefix
- The prefix the cache name should start withCacheException
public CacheManagerPeerProvider getCacheManagerPeerProvider(String scheme)
CacheManagerPeerProvider
, matching the given scheme
For distributed caches, the peer provider finds other cache managers and their caches in the same clusterscheme
- the replication scheme to use. Schemes shipped with ehcache are RMI, JGROUPS, JMSpublic Map<String,CacheManagerPeerProvider> getCacheManagerPeerProviders()
CacheManagerPeerProvider
s keyed by scheme.public CacheManagerPeerListener getCachePeerListener(String scheme)
scheme
- the replication scheme to use. Schemes shipped with ehcache are RMI, JGROUPS, JMSpublic CacheManagerEventListener getCacheManagerEventListener()
public void setCacheManagerEventListener(CacheManagerEventListener cacheManagerEventListener)
cacheManagerEventListener
- the listener to set.public CacheManagerEventListenerRegistry getCacheManagerEventListenerRegistry()
public void replaceCacheWithDecoratedCache(Ehcache ehcache, Ehcache decoratedCache) throws CacheException
BlockingCache
- A cache that blocks other threads from getting a null element until
the first thread has placed a value in it.
SelfPopulatingCache
- A BlockingCache that has the additional property of knowing how
to load its own entries.
ehcache
- decoratedCache
- An implementation of Ehcache that wraps the original cache.CacheException
- if the two caches do not equal each other.public String getName()
which uses either the name or Object.toString()
public boolean isNamed()
public void setName(String name)
name
- a name with characters legal in a JMX ObjectNamepublic String toString()
public DiskStorePathManager getDiskStorePathManager()
public FailSafeTimer getTimer()
FailSafeTimer
associated with this CacheManager
FailSafeTimer
associated with this cache managerpublic CacheCluster getCluster(ClusterScheme scheme) throws ClusterSchemeNotAvailableException
scheme
- The clustering scheme to retrieve information about (such as "Terracotta")ClusterSchemeNotAvailableException
- If the CacheCluster specified by scheme is not available.ClusterScheme
public String getOriginalConfigurationText()
CacheManager
CacheManager
public String getActiveConfigurationText()
CacheManager
CacheManager
public String getOriginalConfigurationText(String cacheName) throws CacheException
cacheName
- CacheException
- if the cache with cacheName
does not exist in the original configpublic String getActiveConfigurationText(String cacheName) throws CacheException
cacheName
- CacheException
- if the cache with cacheName
does not existpublic Configuration getConfiguration()
public Ehcache addCacheIfAbsent(Ehcache cache)
cache
- The Ehcache to be addedpublic Ehcache addCacheIfAbsent(String cacheName)
cacheName
- the name of the Cache to be createdpublic TransactionController getTransactionController()
public TransactionIDFactory getOrCreateTransactionIDFactory()
protected NonstopExecutorService getNonstopExecutorService()
NonstopExecutorService
associated with this cacheManagerNonstopExecutorService
associated with this cacheManagerpublic FeaturesManager getFeaturesManager()
Copyright © 2003-2013 Terracotta, Inc.. All Rights Reserved.