Questo contenuto non è disponibile nella lingua selezionata.

2.2. Instantiating and Starting the Cache


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.
The CacheFactory provides a number of overloaded methods for creating a Cache, but they all fundamentally do the same thing:
  • Gain access to a 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, from a classpath or file system location. See the Chapter 3, Configuration for more on obtaining a Configuration.
  • Instantiate the Cache and provide it with a reference to the Configuration.
  • Optionally invoke the cache's create() and start() methods.
Here is an example of the simplest mechanism for creating and starting a cache, using the default configuration values:
   CacheFactory factory = new DefaultCacheFactory();
   Cache cache = factory.createCache();
Copy to Clipboard Toggle word wrap
In this example, we tell the CacheFactory to find and parse a configuration file on the classpath:
   CacheFactory factory = new DefaultCacheFactory();
   Cache cache = factory.createCache("cache-configuration.xml");
Copy to Clipboard Toggle word wrap
In this example, we configure the cache from a file, but want to programmatically change a configuration element. So, we tell the factory not to start the cache, and instead do it ourselves:
   CacheFactory factory = new DefaultCacheFactory();
   Cache cache = factory.createCache("/opt/configurations/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();
Copy to Clipboard Toggle word wrap
Red Hat logoGithubredditYoutubeTwitter

Formazione

Prova, acquista e vendi

Community

Informazioni sulla documentazione di Red Hat

Aiutiamo gli utenti Red Hat a innovarsi e raggiungere i propri obiettivi con i nostri prodotti e servizi grazie a contenuti di cui possono fidarsi. Esplora i nostri ultimi aggiornamenti.

Rendiamo l’open source più inclusivo

Red Hat si impegna a sostituire il linguaggio problematico nel codice, nella documentazione e nelle proprietà web. Per maggiori dettagli, visita il Blog di Red Hat.

Informazioni su Red Hat

Forniamo soluzioni consolidate che rendono più semplice per le aziende lavorare su piattaforme e ambienti diversi, dal datacenter centrale all'edge della rete.

Theme

© 2026 Red Hat
Torna in cima