The CacheManager class is used to manage caches. Creation of, access to, and removal of caches is controlled by a named CacheManager.
A Cache is a thread-safe logical representation of a set of data elements, analogous to a cache region in many caching systems. Once a reference to a cache is obtained (through a CacheManager), logical actions can be performed. The physical implementation of these actions is relegated to the stores.
An element is an atomic entry in a cache. It has a key, a value, and a record of accesses. Elements are put into and removed from caches. They can also expire and be removed by the cache, depending on the cache settings.
Create a new CacheManager or return the existing one named in the configuration.
CacheManager.newInstance(Configuration configuration)
Create a new singleton CacheManager with default configuration, or return the existing singleton.
CacheManager.create()
CacheManager.getInstance()
Create a singleton CacheManager with the passed-in configuration, or return the existing singleton.
CacheManager.create(Configuration configuration)
Create a new CacheManager, or throw an exception if the CacheManager named in the configuration already exists or if the parameter (configuration) is null.
new CacheManager(Configuration configuration)
Create a singleton CacheManager with the passed-in configuration, or return the existing singleton.
CacheManager.create(Configuration configuration)
The following creates a CacheManager based on the configuration defined in the ehcache.xml file in the classpath.
CacheManager manager = CacheManager.newInstance()
The following creates a CacheManager based on a specified configuration file.
CacheManager manager = CacheManager.newInstance("src/config/ehcache.xml");
The following creates a CacheManager from a configuration resource in the classpath.
URL url = getClass().getResource("/anotherconfigurationname.xml");
CacheManager manager = CacheManager.newInstance(url);
The following creates a CacheManager from a configuration in an InputStream.
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath()); try { CacheManager manager = CacheManager.newInstance(fis); } finally { fis.close(); }
Obtaining a reference to a Cache
Cache cache = manager.getCache("sampleCache1");
Putting an Element in Cache
Cache cache = manager.getCache("sampleCache1"); Element element = new Element("key1", "value1"); cache.put(element);
Updating and Element in Cache
Cache cache = manager.getCache("sampleCache1"); cache.put(new Element("key1", "value1")); //This updates the entry for "key1" cache.put(new Element("key1", "value2"));
Getting an Element from Cache - The following gets a Serializable value from an element with a key of key1 .
Cache cache = manager.getCache("sampleCache1"); Element element = cache.get("key1"); Serializable value = element.getValue();
Getting an Element from Cache - The following gets a NonSerializable value from an element with a key of key1 .
Cache cache = manager.getCache("sampleCache1"); Element element = cache.get("key1"); Object value = element.getObjectValue();
Removing an Element from Cache
Cache cache = manager.getCache("sampleCache1");
cache.remove("key1");
Obtaining Cache Sizes - The following gets the number of elements currently in the cache.
Cache cache = manager.getCache("sampleCache1"); int elementsInMemory = cache.getSize();
Obtaining Cache Sizes - The following gets the number of elements currently in the MemoryStore.
Cache cache = manager.getCache("sampleCache1"); long elementsInMemory = cache.getMemoryStoreSize();
Obtaining Cache Sizes - The following gets the number of elements currently in the DiskStore.
Cache cache = manager.getCache("sampleCache1"); long elementsInMemory = cache.getDiskStoreSize();
The following shuts down the singleton CacheManager:
CacheManager.getInstance().shutdown();
The following shuts down a CacheManager instance, assuming you have a reference to the CacheManager called cacheManager :
cacheManager.shutdown();