public interface CacheManager extends Closeable
CacheManager
provides a means of establishing, configuring,
acquiring, closing and destroying uniquely named Cache
s.
Cache
s 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 Cache
s being managed, for example,
the content of the managed Cache
s 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
CacheManager
s, including:
ServiceLoader
s,new
operator to create a
concrete implementation, 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 CacheManager
s and the Cache
s 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.Caching
,
CachingProvider
,
Cache
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the
CacheManager . |
<K,V> Cache<K,V> |
createCache(String cacheName,
Configuration<K,V> configuration)
Creates a named
Cache at runtime. |
void |
destroyCache(String cacheName)
Destroys a specifically named and managed
Cache . |
void |
enableManagement(String cacheName,
boolean enabled)
Controls whether management is enabled.
|
void |
enableStatistics(String cacheName,
boolean enabled)
Enables or disables statistics gathering for a managed
Cache at
runtime. |
<K,V> Cache<K,V> |
getCache(String cacheName)
Looks up a managed
Cache given it's name. |
<K,V> Cache<K,V> |
getCache(String cacheName,
Class<K> keyType,
Class<V> valueType)
Looks up a managed
Cache given it's name. |
Iterable<String> |
getCacheNames()
|
CachingProvider |
getCachingProvider()
Get the
CachingProvider that created and is responsible for
the CacheManager . |
Properties |
getProperties()
Get the Properties that were used to create this
CacheManager . |
URI |
getURI()
Get the URI of the
CacheManager . |
UserTransaction |
getUserTransaction()
Obtains a UserTransaction for transactional
Cache s managed
by the CacheManager . |
boolean |
isClosed()
Determines whether the
CacheManager instance has been closed. |
<T> T |
unwrap(Class<T> clazz)
Provides a standard mechanism to access the underlying concrete caching
implementation to provide access to further, proprietary features.
|
CachingProvider getCachingProvider()
CachingProvider
that created and is responsible for
the CacheManager
.null
if the CacheManager
was created without using a CachingProvider
URI getURI()
CacheManager
.CacheManager
Properties getProperties()
CacheManager
.CacheManager
<K,V> Cache<K,V> createCache(String cacheName, Configuration<K,V> configuration) throws IllegalArgumentException
Cache
at runtime.
If a Cache
with the specified name is known to the CacheManager
, a CacheException is thrown.
If a Cache
with the specified name is unknown the CacheManager
,
one is created according to the provided
Configuration
after which it becomes managed by the CacheManager
.
Prior to a Cache
being created the provided Configuration
s is
validated within the context of the CacheManager
properties and
implementation.
For example: Attempting to use a Configuration
requiring
transactional support with an implementation that does not support
transactions will result in an UnsupportedOperationException
.
Implementers should be aware that the Configuration
may be used to
configure other Cache
s.
There's no requirement on the part of a developer to call this method for
each Cache
an application may use. Implementations may support
the use of declarative mechanisms to pre-configure Cache
s, thus
removing the requirement to configure them in an application. In such
circumstances a developer may simply call either the getCache(String)
or getCache(String, Class, Class)
methods to acquire a
previously established or pre-configured Cache
.cacheName
- the name of the Cache
configuration
- the Configuration
to use if the Cache
is knownIllegalStateException
- if the CacheManager
isClosed()
CacheException
- if there was an error configuring the
Cache
, which includes trying
to create a cache which already exists.IllegalArgumentException
- if the configuration is invalidUnsupportedOperationException
- if the configuration specifies
an unsupported featureNullPointerException
- if the cache configuration or name
is null<K,V> Cache<K,V> getCache(String cacheName, Class<K> keyType, Class<V> valueType)
Cache
given it's name.
This method must be used for Cache
s that were configured with
runtime key and value types. Use getCache(String)
for
Cache
s where these were not specified.
Implementations must ensure that the key and value types are the same as
those configured for the Cache
prior to returning from this method.
Implementations may further perform type checking on cache operations and
throw a ClassCastException
if said checks fail.
Implementations that support declarative mechanisms for pre-configuring
Cache
s may return a pre-configured Cache
instead of
null
.cacheName
- the name of the managed Cache
to acquirekeyType
- the expected Class
of the keyvalueType
- the expected Class
of the valueIllegalStateException
- if the CacheManager is isClosed()
IllegalArgumentException
- if the specified key and/or value types are
incompatible with the configured cache.<K,V> Cache<K,V> getCache(String cacheName)
Cache
given it's name.
This method may only be used to acquire Cache
s 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 acquire
Cache
s that were configured with specific runtime types.
Implementations must check if key and value types were configured
for the requested Cache
. If either the keyType or valueType of the
configured Cache
were specified (other than Object.class
)
an IllegalArgumentException
will be thrown.
Implementations that support declarative mechanisms for pre-configuring
Cache
s may return a pre-configured Cache
instead of
null
.cacheName
- the name of the cache to look forIllegalStateException
- if the CacheManager is isClosed()
IllegalArgumentException
- if the Cache
was configured with
specific types, this method cannot be usedgetCache(String, Class, Class)
Iterable<String> getCacheNames()
Iterable
over the names of Cache
s managed by the
CacheManager
.
Iterator
s returned by the Iterable
are immutable.
Any modification of the Iterator
, including remove, will
raise an IllegalStateException
. If the Cache
s managed by
the CacheManager
change, the Iterable
and
associated Iterator
s are not affected.void destroyCache(String cacheName)
Cache
. Once destroyed
a new Cache
of the same name but with a different Configuration
may be configured.
This is equivalent to the following sequence of method calls:
followed by allowing the name of the Cache
to be used for other
Cache
configurations.
From the time this method is called, the specified Cache
is not
available for operational use. An attempt to call an operational method on
the Cache
will throw an IllegalStateException
.cacheName
- the cache to destroyNullPointerException
- if cacheName is nullUserTransaction getUserTransaction()
Cache
s managed
by the CacheManager
.UnsupportedOperationException
- if JTA is not supportedvoid enableManagement(String cacheName, boolean enabled)
CacheMXBean
for
each cache is registered in the platform MBean server. The platform
MBeanServer is obtained using
ManagementFactory.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=Cache
Required Attributes:
cacheName
- the name of the cache to registerenabled
- true to enable management, false to disable.void enableStatistics(String cacheName, boolean enabled)
Cache
at
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=CacheStatistics
Required Attributes:
cacheName
- the name of the cache to registerenabled
- true to enable statistics, false to disable.IllegalStateException
- if the cache is isClosed()
NullPointerException
- if cacheName is nullvoid close()
CacheManager
.
For each Cache
managed by the CacheManager
, the
Cache.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 return
true
.
All attempts to close a previously closed CacheManager
will be
ignored.close
in interface AutoCloseable
close
in interface Closeable
boolean isClosed()
CacheManager
instance has been closed. A
CacheManager
is considered closed if;
close()
method has been calledgetCachingProvider()
has been closed, orCacheManager
has been closed using the associated
getCachingProvider()
CacheManager
is valid or invalid. A typical client can determine
that a CacheManager
is invalid by catching any exceptions that
might be thrown when an operation is attempted.CacheManager
instance is closed; false if it
is still open<T> T unwrap(Class<T> clazz)
IllegalArgumentException
is thrown.clazz
- the proprietary class or interface of the underlying concrete
CacheManager
. It is this type which is returned.CacheManager
IllegalArgumentException
- if the caching provider doesn't support the
specified class.Copyright © 2013. All Rights Reserved.