A
CacheManager is the primary mechanism for retrieving a {@link Cache} instance, and is often used as astarting point to using the {@link Cache}.
CacheManagers are heavyweight objects, and we foresee no more than one
CacheManager being used per JVM (unless specific configuration requirements require more than one; but either way, this would be a minimal and finite number of instances).
Constructing a
CacheManager is done via one of its constructors, which optionally take in a {@link org.infinispan.config.Configuration} or a path or URL to a configuration XML file.
Lifecycle -
CacheManagers have a lifecycle (it implements {@link Lifecycle}) and the default constructors also call {@link #start()}. Overloaded versions of the constructors are available, that do not start the
CacheManager, although it must be kept in mind that
CacheManagers need to be started before they can be used to create
Cache instances.
Once constructed,
CacheManagers should be made available to any component that requires a
Cache, via JNDI or via some other mechanism such as an IoC container.
You obtain
Cache instances from the
CacheManager by using one of the overloaded
getCache(), methods. Note that with
getCache(), there is no guarantee that the instance you get is brand-new and empty, since caches are named and shared. Because of this, the
CacheManager also acts as a repository of
Caches, and is an effective mechanism of looking up or creating
Caches on demand.
When the system shuts down, it should call {@link #stop()} on the
CacheManager. This will ensure all cacheswithin its scope are properly stopped as well.
Sample usage:
CacheManager manager = CacheManager.getInstance("my-config-file.xml"); Cache entityCache = manager.getCache("myEntityCache"); entityCache.put("aPerson", new Person()); Configuration myNewConfiguration = new Configuration(); myNewConfiguration.setCacheMode(Configuration.CacheMode.LOCAL); manager.defineCache("myLocalCache", myNewConfiguration); Cache localCache = manager.getCache("myLocalCache");
@author Manik Surtani (
manik@jboss.org)
@since 4.0