Class CacheManager
- java.lang.Object
-
- org.cache2k.CacheManager
-
- All Implemented Interfaces:
AutoCloseable
public abstract class CacheManager extends Object implements AutoCloseable
A cache manager holds a set of caches. Caches within a cache manager share the same class loader and may have a different default configuration. If a cache manager is not specified a default manager is used.Usually there is one cache manager per application which is retrieved by
getInstance()
. Multiple cache managers can be used to separate different caching aspects. This may help with a better organization of the cache configuration and operate with reasonable defaults within each aspect.Cache managers are identified by a unique name. If no name is specified the name "default" is used. The default name in use may be changed, see
setDefaultName(String)
.- Author:
- Jens Wilke
-
-
Field Summary
Fields Modifier and Type Field Description static Cache2kCoreProvider
PROVIDER
The singleton cache provider instance.static String
STANDARD_DEFAULT_MANAGER_NAME
Name of the default cache manager if not overridden, seesetDefaultName(String)
.
-
Constructor Summary
Constructors Constructor Description CacheManager()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract void
clear()
Clear all currently active caches in this cache managerabstract void
close()
Free all resources from managed caches.static void
close(ClassLoader cl, String name)
Close the named cache manager.static void
closeAll()
Close all cache managers.static void
closeAll(ClassLoader cl)
Close all cache manager associated with this class loader.abstract <K,V>
Cache<K,V>createCache(Cache2kConfig<K,V> cfg)
Create a new cache from the configuration.abstract Iterable<Cache<?,?>>
getActiveCaches()
Returns all open caches associated with this cache manager.abstract <K,V>
Cache<K,V>getCache(String name)
Return a known cache that must be created before via theCache2kBuilder
orcreateCache(Cache2kConfig)
abstract ClassLoader
getClassLoader()
Class loader this manager is using to load additional classes, resources or configuration.abstract Iterable<String>
getConfiguredCacheNames()
Returns a list of caches that are found in the XML based configuration.static String
getDefaultName()
Name of the default cache manager, which is ""default"" by default.static CacheManager
getInstance()
Get the default cache manager for the default class loader.static CacheManager
getInstance(ClassLoader cl)
Get the default cache manager for the specified class loader.static CacheManager
getInstance(ClassLoader cl, String managerName)
Retrieve a cache manager with the specified name using the specified classloader.static CacheManager
getInstance(String managerName)
Retrieve a cache manager with the specified name.abstract String
getName()
The name to uniquely identify the manager within a VM instance.abstract Properties
getProperties()
Properties for the cache manager, never null.abstract boolean
isClosed()
Returns true if this cache manager was closed.abstract boolean
isDefaultManager()
True if this is the default manager of the application, returned bygetInstance()
static void
setDefaultName(String managerName)
Change the default manager name.
-
-
-
Field Detail
-
STANDARD_DEFAULT_MANAGER_NAME
public static final String STANDARD_DEFAULT_MANAGER_NAME
Name of the default cache manager if not overridden, seesetDefaultName(String)
.- Since:
- 1.4
- See Also:
- Constant Field Values
-
PROVIDER
public static final Cache2kCoreProvider PROVIDER
The singleton cache provider instance.- Since:
- 2
-
-
Method Detail
-
getDefaultName
public static String getDefaultName()
Name of the default cache manager, which is ""default"" by default.
-
setDefaultName
public static void setDefaultName(String managerName)
Change the default manager name. The method can only be called once early in application startup, before the default manager instance is requested.It is also possible to set a different default manager name via the XML configuration.
The allowed characters in a manager name are identical to the characters in a cache name, this is documented at
Cache2kBuilder.name(String)
- See Also:
Cache2kBuilder.name(String)
-
getInstance
public static CacheManager getInstance()
Get the default cache manager for the default class loader. The default class loader is the class loader used to load the cache2k implementation classes.The name of default cache manager is ""default"". This may be changed, by
setDefaultName(String)
.
-
getInstance
public static CacheManager getInstance(ClassLoader cl)
Get the default cache manager for the specified class loader.
-
getInstance
public static CacheManager getInstance(String managerName)
Retrieve a cache manager with the specified name. If not existing, a manager with that name is created. The default class loader is used. The default class loader is the class loader used to load the cache2k implementation classes.The allowed characters in a manager name are identical to the characters in a cache name, this is documented at
Cache2kBuilder.name(String)
- See Also:
Cache2kBuilder.name(String)
-
getInstance
public static CacheManager getInstance(ClassLoader cl, String managerName)
Retrieve a cache manager with the specified name using the specified classloader. If not existing, a manager with that name is created. Different cache managers are created for different class loaders. Manager names should be unique within one VM instance.The allowed characters in a manager name are identical to the characters in a cache name, this is documented at
Cache2kBuilder.name(String)
- See Also:
Cache2kBuilder.name(String)
-
closeAll
public static void closeAll()
Close all cache managers.
-
closeAll
public static void closeAll(ClassLoader cl)
Close all cache manager associated with this class loader.
-
close
public static void close(ClassLoader cl, String name)
Close the named cache manager.
-
isDefaultManager
public abstract boolean isDefaultManager()
True if this is the default manager of the application, returned bygetInstance()
-
getName
public abstract String getName()
The name to uniquely identify the manager within a VM instance.- See Also:
getInstance(String)
-
getActiveCaches
public abstract Iterable<Cache<?,?>> getActiveCaches()
Returns all open caches associated with this cache manager. A cache returned by the iteration was created viacreateCache(Cache2kConfig)
orCache2kBuilder.build()
and is not closed yet.
-
getConfiguredCacheNames
public abstract Iterable<String> getConfiguredCacheNames()
Returns a list of caches that are found in the XML based configuration.
-
getCache
public abstract <K,V> Cache<K,V> getCache(String name)
Return a known cache that must be created before via theCache2kBuilder
orcreateCache(Cache2kConfig)
-
createCache
public abstract <K,V> Cache<K,V> createCache(Cache2kConfig<K,V> cfg)
Create a new cache from the configuration. The recommended way is to use theCache2kBuilder
to create a new cache. This method is identical to and a shorthand to:CacheManager manager = ... Cache2kConfiguration<K, V> config = ... Cache<K, V> cache = Cache2kBuilder.of(config).manager(manager).build();
-
clear
public abstract void clear()
Clear all currently active caches in this cache manager
-
close
public abstract void close()
Free all resources from managed caches. Same as calling allCache.close()
methods. A closed manager cannot be used to create new caches. A newCacheManager
with the same name may be requested viagetInstance(String)
. Multiple calls to close have no effect.- Specified by:
close
in interfaceAutoCloseable
-
isClosed
public abstract boolean isClosed()
Returns true if this cache manager was closed.- See Also:
close()
-
getProperties
public abstract Properties getProperties()
Properties for the cache manager, never null. By default, the properties are empty. Cache clients may store arbitrary information.
-
getClassLoader
public abstract ClassLoader getClassLoader()
Class loader this manager is using to load additional classes, resources or configuration.
-
-