13.2. Creating CacheManagers


13.2.1. Create a New RemoteCacheManager

Procedure 13.1. Configure a New RemoteCacheManager

import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.Configuration;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;

Configuration conf = new 
ConfigurationBuilder().addServer().host("localhost").port(11222).build();
RemoteCacheManager manager = new RemoteCacheManager(conf);
RemoteCache defaultCache = manager.getCache();
  1. Use the ConfigurationBuilder() constructor to create a new configuration builder. The .addServer() method adds a remote server, configured via the .host(<hostname|ip>) and .port(<port>) properties.
  2. Create a new RemoteCacheManager using the supplied configuration.
  3. Retrieve the default cache from the remote server.

13.2.2. Create a New Embedded Cache Manager

Use the following instructions to create a new EmbeddedCacheManager without using CDI:

Procedure 13.2. Create a New Embedded Cache Manager

  1. Create a configuration XML file. For example, create the my-config-file.xml file on the classpath (in the resources/ folder) and add the configuration information in this file.
  2. Use the following programmatic configuration to create a cache manager using the configuration file:
    EmbeddedCacheManager manager = new DefaultCacheManager("my-config-file.xml");
    Cache defaultCache = manager.getCache();
The outlined procedure creates a new EmbeddedCacheManager using the configuration specified in my-config-file.xml.

13.2.3. Create a New Embedded Cache Manager Using CDI

Use the following steps to create a new EmbeddedCacheManager instance using CDI:

Procedure 13.3. Use CDI to Create a New EmbeddedCacheManager

  1. Specify a default configuration:
    public class Config
       @Produces
       public EmbeddedCacheManager defaultCacheManager() {
          ConfigurationBuilder builder = new ConfigurationBuilder();          
          Configuration configuration = builder.eviction().strategy(EvictionStrategy.LRU).maxEntries(100).build();
          return new DefaultCacheManager(configuration);
       }
    }
  2. Inject the default cache manager.
    <!-- Additional configuration information here -->
    @Inject
    EmbeddedCacheManager cacheManager;
    <!-- Additional configuration information here -->
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.