Interface CacheManager
- All Superinterfaces:
AutoCloseable, Closeable
CacheManager provides a means of establishing, configuring,
acquiring, closing and destroying uniquely named Caches.
Caches produced and owned by a CacheManager typically share
common infrastructure, for example, a common ClassLoader and
implementation specific Properties.
Implementations of CacheManager may additionally provide and share
external resources between the Caches being managed, for example,
the content of the managed Caches may be stored in the same cluster.
By default CacheManager instances are typically acquired through the
use of a CachingProvider. Implementations however may additionally
provide other mechanisms to create, acquire, manage and configure
CacheManagers, including:
- making use of
ServiceLoaders, - permitting the use of the
newoperator to create a concrete implementation, - providing the construction through the use of one or more builders, and
- through the use of dependency injection.
The default CacheManager however can always be acquired using the
default configured CachingProvider obtained by the Caching
class. For example:
CachingProvider provider = Caching.getCachingProvider();
CacheManager manager = provider.getCacheManager();
Within a Java process CacheManagers and the Caches they
manage are scoped and uniquely identified by a URI, the meaning of
which is implementation specific. To obtain the default URI,
ClassLoader and Properties for an implementation, consult the
CachingProvider class.
- Since:
- 1.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes theCacheManager.<K, V, C extends Configuration<K,V>>
Cache<K, V> createCache(String cacheName, C configuration) Creates a namedCacheat runtime.voiddestroyCache(String cacheName) Destroys a specifically named and managedCache.voidenableManagement(String cacheName, boolean enabled) Controls whether management is enabled.voidenableStatistics(String cacheName, boolean enabled) Enables or disables statistics gathering for a managedCacheat runtime.<K,V> Cache <K, V> Looks up a managedCachegiven its name.<K,V> Cache <K, V> Looks up a managedCachegiven its name.Get theCachingProviderthat created and is responsible for theCacheManager.Get theClassLoaderused by theCacheManager.Get thePropertiesthat were used to create thisCacheManager.getURI()Get the URI of theCacheManager.booleanisClosed()Determines whether theCacheManagerinstance has been closed.<T> TProvides a standard mechanism to access the underlying concrete caching implementation to provide access to further, proprietary features.
-
Method Details
-
getCachingProvider
CachingProvider getCachingProvider()Get theCachingProviderthat created and is responsible for theCacheManager.- Returns:
- the CachingProvider or
nullif theCacheManagerwas created without using aCachingProvider
-
getURI
-
getClassLoader
ClassLoader getClassLoader()Get theClassLoaderused by theCacheManager.- Returns:
- the
ClassLoaderused by theCacheManager
-
getProperties
Properties getProperties()Get thePropertiesthat were used to create thisCacheManager.Implementations are not required to re-configure the
CacheManagershould modifications to the returnedPropertiesbe made.- Returns:
- the Properties used to create the
CacheManager
-
createCache
<K, V, C extends Configuration<K,V>> Cache<K,V> createCache(String cacheName, C configuration) throws IllegalArgumentException Creates a namedCacheat runtime.If a
Cachewith the specified name is known to theCacheManager, a CacheException is thrown.If a
Cachewith the specified name is unknown theCacheManager, one is created according to the providedConfigurationafter which it becomes managed by theCacheManager.Prior to a
Cachebeing created, the providedConfigurations is validated within the context of theCacheManagerproperties and implementation.Implementers should be aware that the
Configurationmay be used to configure otherCaches.There's no requirement on the part of a developer to call this method for each
Cachean application may use. Implementations may support the use of declarative mechanisms to pre-configureCaches, thus removing the requirement to configure them in an application. In such circumstances a developer may simply call either thegetCache(String)orgetCache(String, Class, Class)methods to acquire a previously established or pre-configuredCache.- Type Parameters:
K- the type of keyV- the type of valueC- the type of the Configuration- Parameters:
cacheName- the name of theCache. Names should not use forward slashes(/) or colons(:), or start with java. or javax. These prefixes are reserved.configuration- aConfigurationfor theCache- Throws:
IllegalStateException- if theCacheManagerisClosed()CacheException- if there was an error configuring theCache, which includes trying to create a cache that already exists.IllegalArgumentException- if the configuration is invalidUnsupportedOperationException- if the configuration specifies an unsupported featureNullPointerException- if the cache configuration or name is nullSecurityException- when the operation could not be performed due to the current security settings
-
getCache
Looks up a managedCachegiven its name.Use this method to check runtime key and value types.
Use
getCache(String)where this check is not required.Implementations must ensure that the key and value types are the same as those configured for the
Cacheprior to returning from this method.Implementations may further perform type checking on mutative cache operations and throw a
ClassCastExceptionif these checks fail.Implementations that support declarative mechanisms for pre-configuring
Caches may return a pre-configuredCacheinstead ofnull.- Type Parameters:
K- the type of keyV- the type of value- Parameters:
cacheName- the name of the managedCacheto acquirekeyType- the expectedClassof the keyvalueType- the expectedClassof the value- Returns:
- the Cache or null if it does exist or can't be pre-configured
- Throws:
IllegalStateException- if theCacheManagerisisClosed()ClassCastException- if the specified key and/or value types are incompatible with the configured cache.NullPointerException- if either keyType or classType is null.SecurityException- when the operation could not be performed due to the current security settings
-
getCache
Looks up a managedCachegiven its name.This method may only be used to acquire
Caches that were configured without runtime key and value types, or were configured to use Object.class key and value types.Use the
getCache(String, Class, Class)method to acquireCaches with a check that the supplied key and value type parameters match the runtime types.Implementations that support declarative mechanisms for pre-configuring
Caches may return a pre-configuredCacheinstead ofnull.- Type Parameters:
K- the type of keyV- the type of value- Parameters:
cacheName- the name of the cache to look for- Returns:
- the Cache or null if it does exist or can't be pre-configured
- Throws:
IllegalStateException- if the CacheManager isisClosed()SecurityException- when the operation could not be performed due to the current security settings- See Also:
-
getCacheNames
Obtains anIterableover the names ofCaches managed by theCacheManager.Iterators returned by theIterableare immutable. If theCaches managed by theCacheManagerchange, theIterableand associatedIterators are not affected.Iterators returned by theIterablemay not provide all of theCaches managed by theCacheManager. For example: Internally defined or platform specificCaches that may be accessible by a call togetCache(String)orgetCache(String, Class, Class)may not be present in an iteration.- Returns:
- an
Iterableover the names of managedCaches. - Throws:
IllegalStateException- if theCacheManagerisisClosed()SecurityException- when the operation could not be performed due to the current security settings
-
destroyCache
Destroys a specifically named and managedCache. Once destroyed a newCacheof the same name but with a differentConfigurationmay be configured.This is equivalent to the following sequence of method calls:
followed by allowing the name of theCacheto be used for otherCacheconfigurations.From the time this method is called, the specified
Cacheis not available for operational use. An attempt to call an operational method on theCachewill throw anIllegalStateException.- Parameters:
cacheName- the cache to destroy- Throws:
IllegalStateException- if theCacheManagerisClosed()NullPointerException- if cacheName is nullSecurityException- when the operation could not be performed due to the current security settings
-
enableManagement
Controls whether management is enabled. If enabled theCacheMXBeanfor each cache is registered in the platform MBean server. The platform MBeanServer is obtained usingManagementFactory.getPlatformMBeanServer().Management information includes the name and configuration information for the cache.
Each cache's management object must be registered with an ObjectName that is unique and has the following type and attributes:
Type:
javax.cache:type=CacheConfigurationRequired Attributes:
- CacheManager the URI of the CacheManager
- Cache the name of the Cache
- Parameters:
cacheName- the name of the cache to registerenabled- true to enable management, false to disable.- Throws:
IllegalStateException- if theCacheManagerorCacheisClosed()SecurityException- when the operation could not be performed due to the current security settings
-
enableStatistics
Enables or disables statistics gathering for a managedCacheat runtime.Each cache's statistics object must be registered with an ObjectName that is unique and has the following type and attributes:
Type:
javax.cache:type=CacheStatisticsRequired Attributes:
- CacheManager the URI of the CacheManager
- Cache the name of the Cache
- Parameters:
cacheName- the name of the cache to registerenabled- true to enable statistics, false to disable.- Throws:
IllegalStateException- if theCacheManagerorCacheisClosed()NullPointerException- if cacheName is nullSecurityException- when the operation could not be performed due to the current security settings
-
close
void close()Closes theCacheManager.For each
Cachemanaged by theCacheManager, theCache.close()method will be invoked, in no guaranteed order.If a
Cache.close()call throws an exception, the exception will be ignored.After executing this method, the
isClosed()method will returntrue.All attempts to close a previously closed
CacheManagerwill be ignored. Closing a CacheManager does not necessarily destroy the contents of the Caches in the CacheManager.It simply signals that the CacheManager is no longer required by the application and that future uses of a specific CacheManager instance should not be permitted.
Depending on the implementation and Cache topology, (e.g. a storage-backed or distributed cache), the contents of closed Caches previously referenced by the CacheManager, may still be available and accessible by other applications.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
SecurityException- when the operation could not be performed due to the current security settings
-
isClosed
boolean isClosed()Determines whether theCacheManagerinstance has been closed. ACacheManageris considered closed if;- the
close()method has been called - the associated
getCachingProvider()has been closed, or - the
CacheManagerhas been closed using the associatedgetCachingProvider()
This method generally cannot be called to determine whether the
CacheManageris valid or invalid. A typical client can determine that aCacheManageris invalid by catching any exceptions that might be thrown when an operation is attempted.- Returns:
- true if this
CacheManagerinstance is closed; false if it is still open
- the
-
unwrap
Provides a standard mechanism to access the underlying concrete caching implementation to provide access to further, proprietary features.If the provider's implementation does not support the specified class, the
IllegalArgumentExceptionis thrown.- Type Parameters:
T- the type of the underlyingCacheManager- Parameters:
clazz- the proprietary class or interface of the underlying concreteCacheManager. It is this type that is returned.- Returns:
- an instance of the underlying concrete
CacheManager - Throws:
IllegalArgumentException- if the caching provider doesn't support the specified class.SecurityException- when the operation could not be performed due to the current security settings
-