JBoss.org Community Documentation
An instance of the
Cache
interface can only be created
via a
CacheFactory
. (This is unlike JBoss Cache 1.x,
where an instance of the old
TreeCache
class could
be directly instantiated.)
CacheFactory
provides a number of overloaded methods
for creating a
Cache
, but they all do the same thing:
Configuration
, either
by having one passed in as a method parameter, or by parsing XML
content and constructing one. The XML content can come from a
provided input stream or from a classpath or filesystem location.
See the
chapter on Configuration
for
more on obtaining a
Configuration
.
Cache
and provide
it with a reference to the
Configuration
.
create()
and
start()
methods.
An example of the simplest mechanism for creating and starting a cache, using the default configuration values:
CacheFactory factory = DefaultCacheFactory.getInstance();
Cache cache = factory.createCache();
Here we tell the
CacheFactory
to find and
parse a configuration file on the classpath:
CacheFactory factory = DefaultCacheFactory.getInstance();
Cache cache = factory.createCache("cache-configuration.xml");
Here we configure the cache from a file, but want to programatically change a configuration element. So, we tell the factory not to start the cache, and instead do it ourselves:
CacheFactory factory = DefaultCacheFactory.getInstance();
Cache cache = factory.createCache("cache-configuration.xml", false);
Configuration config = cache.getConfiguration();
config.setClusterName(this.getClusterName());
// Have to create and start cache before using it
cache.create();
cache.start();