Chapter 5. Configuring Data Grid Caches


Data Grid lets you define properties and options for caches both declaratively and programmatically.

Declarative configuration uses XML files that adhere to a Data Grid schema. Programmatic configuration, on the other hand, uses Data Grid APIs.

In most cases, you use declarative configuration as a starting point for cache definitions. At runtime you can then programmatically configure your caches to tune settings or specify additional properties. However, Data Grid provides flexibility so you can choose either declarative, programmatic, or a combination of the two.

5.1. Declarative Configuration

You configure Data Grid caches by defining properties in infinispan.xml.

The following example shows the basic structure of a Data Grid configuration:

<infinispan> 1
   <cache-container default-cache="local"> 2
      <transport stack="udp" cluster="mycluster"/> 3
      <local-cache name="local"/> 4
      <invalidation-cache name="invalidation"/> 5
      <replicated-cache name="replicated"/> 6
      <distributed-cache name="distributed"/> 7
   </cache-container>
</infinispan>
1
adds the root element for the Data Grid configuration. The minimum valid configuration is <infinispan />; however this provides very basic capabilities with no clustering and no cache instances.
2
defines properties for all caches within the container and names the default cache.
3
defines transport properties for clustered cache modes. In the preceding example, stack="udp" specifies the default JGroups UDP transport stack and names the Data Grid cluster.
4
local cache.
5
invalidation cache.
6
replicated cache.
7
distributed cache.

5.1.1. Cache Configuration Templates

Data Grid lets you define configuration templates that you can apply to multiple cache definitions or use as the basis for complex configurations.

For example, the following configuration contains a configuration template for local caches:

<infinispan>
   <cache-container default-cache="local"> 1
      <local-cache-configuration name="local-template"> 2
         <expiration interval="10000" lifespan="10" max-idle="10"/>
      </local-cache-configuration>
      <local-cache name="local" configuration="local-template" /> 3
   </cache-container>
</infinispan>
1
specifies the "local" cache as the default.
2
defines a configuration template named "local-template" that defines an expiration policy for local caches.
3
names a local cache instance that uses the configuration template.

Inheritance with configuration templates

Configuration templates can also inherit from other templates to extend and override settings.

Note

Configuration template inheritance is hierarchical. For a child configuration template to inherit from a parent, you must include it after the parent template.

The following is an example of configuration template inheritance:

<infinispan>
   <cache-container default-cache="local">
      <local-cache-configuration name="base-template"> 1
         <expiration interval="10000" lifespan="10" max-idle="10"/>
      </local-cache-configuration>

      <local-cache-configuration name="extended-template"
                                 configuration="base-template"> 2
         <expiration lifespan="20"/>
         <memory>
            <object size="2000"/>
         </memory>
      </local-cache-configuration>

      <local-cache name="local" configuration="base-template" /> 3
      <local-cache name="local-bounded" configuration="extended-template" /> 4
   </cache-container>
</infinispan>
1
defines a configuration template named "base-template" that defines an expiration policy for local caches. In this example, "base-template" is the parent configuration template.
2
defines a configuration template named "extended-template" that inherits settings from "base-template", modifies the lifespan attribute for expiration, and adds a memory element to the configuration. In this example, "extended-template" is a child of "base-template".
3
names a local cache that uses the configuration settings in "base-template".
4
names a local cache that uses the configuration settings in "extended-template".
Important

Configuration template inheritance is additive for elements that have multiple values, such as property. Resulting child configurations merge values from parent configurations.

For example, <property value_x="foo" /> in a parent configuration merges with <property value_y="bar" /> in a child configuration to result in <property value_x="foo" value_y="bar" />.

5.1.2. Cache Configuration Wildcards

You can use wildcards to match cache definitions to configuration templates.

<infinispan>
    <cache-container>
        <local-cache-configuration name="basecache*"> 1
            <expiration interval="10500" lifespan="11" max-idle="11"/>
        </local-cache-configuration>
        <local-cache name="basecache-1"/> 2
        <local-cache name="basecache-2"/> 3
    </cache-container>
</infinispan>
1
uses the * wildcard to match any cache names that start with "basecache".
2
names a local cache "basecache-1" that uses the "basecache*" configuration template.
3
names a local cache "basecache-2" that uses the "basecache*" configuration template.
Note

Data Grid throws exceptions if cache names match more than one wildcard.

5.1.3. Multiple Configuration Files

Data Grid supports XML inclusions (XInclude) that allow you to split configuration across multiple files.

For example, the following configuration uses an XInclude:

<infinispan xmlns:xi="http://www.w3.org/2001/XInclude">
    <cache-container default-cache="cache-1">
        <xi:include href="local.xml" /> 1
    </cache-container>
</infinispan>
1
includes an local.xml file that contains the following cache definition:
<local-cache name="mycache"/>

If you want to use a schema for your included fragments, use the infinispan-config-fragment-10.1.xsd schema:

include-with-schema.xml

<local-cache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="urn:infinispan:config:10.1 https://infinispan.org/schemas/infinispan-config-fragment-10.1.xsd"
             xmlns="urn:infinispan:config:10.1"
             name="mycache"/>

Note

Data Grid configurations provides only minimal support for the XInclude specification. For example, you cannot use the xpointer attribute, the xi:fallback element, text processing, or content negotiation.

5.2. Programmatic Configuration

Create new Configuration objects with the ConfigurationBuilder class and then define cache configurations with the Cache Manager.

Note

The examples in this section use EmbeddedCacheManager, which is a Cache Manager that runs in the same JVM as the client.

To configure caches remotely with HotRod clients, you use RemoteCacheManager. Refer to the HotRod documentation for more information.

Configure new cache instances

The following example configures a new cache instance:

EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-prod.xml");
Cache defaultCache = manager.getCache();
Configuration c = new ConfigurationBuilder().clustering() 1
  .cacheMode(CacheMode.REPL_SYNC) 2
  .build();

String newCacheName = "replicatedCache";
manager.defineConfiguration(newCacheName, c); 3
Cache<String, String> cache = manager.getCache(newCacheName);
1
creates a new Configuration object.
2
specifies distributed, synchronous cache mode.
3
defines a new cache named "replicatedCache" with the Configuration object.

Create new caches from existing configurations

The following examples create new cache configurations from existing ones:

EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-prod.xml");
Configuration dcc = manager.getDefaultCacheConfiguration(); 1
Configuration c = new ConfigurationBuilder().read(dcc) 2
  .clustering()
  .cacheMode(CacheMode.DIST_SYNC) 3
  .l1()
  .lifespan(60000L) 4
  .build();
 
String newCacheName = "distributedWithL1";
manager.defineConfiguration(newCacheName, c); 5
Cache<String, String> cache = manager.getCache(newCacheName);
1
returns the default cache configuration from the Cache Manager. In this example, infinispan-prod.xml defines a replicated cache as the default.
2
creates a new Configuration object that uses the default cache configuration as a base.
3
specifies distributed, synchronous cache mode.
4
adds an L1 lifespan configuration.
5
defines a new cache named "distributedWithL1" with the Configuration object.
EmbeddedCacheManager manager = new DefaultCacheManager("infinispan-prod.xml");
Configuration rc = manager.getCacheConfiguration("replicatedCache"); 1
Configuration c = new ConfigurationBuilder().read(rc)
  .clustering()
  .cacheMode(CacheMode.DIST_SYNC)
  .l1()
  .lifespan(60000L)
  .build();
 
String newCacheName = "distributedWithL1";
manager.defineConfiguration(newCacheName, c);
Cache<String, String> cache = manager.getCache(newCacheName);
1
uses a cache configuration named "replicatedCache" as a base.

5.2.1. Configuration Objects

Data Grid provides two abstractions for programmatic cache configuration:

GlobalConfigurationBuilder
Constructs global configuration objects that apply to all cache definitions.
ConfigurationBuilder
Constructs configuration objects specific to cache definitions.

Global configuration

An example of global configuration is to enable statistics and export them via JMX or the metrics endpoint at the Cache Manager level, as follows:

GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
  .cacheContainer().statistics(true)
  .metrics().gauges(true).histograms(true)
  .jmx().enable()
  .build();

Cache configuration

The following examples show how you can programmatically configure different settings for Data Grid caches.

  • Configure a distributed, synchronous clustered cache mode:

    Configuration config = new ConfigurationBuilder()
      .clustering()
        .cacheMode(CacheMode.DIST_SYNC)
        .l1().lifespan(25000L)
        .hash().numOwners(3)
      .build();
  • Configure eviction and expiration settings:

    Configuration config = new ConfigurationBuilder()
               .memory()
                 .size(20000)
              .expiration()
                 .wakeUpInterval(5000L)
                 .maxIdle(120000L)
               .build();
  • Configure persistent cache stores:

    Configuration config = new ConfigurationBuilder()
       .persistence().passivation(false)
       .addSingleFileStore().location("/tmp").async().enable()
       .threadPoolSize(20).preload(false).shared(false).build();
  • Configure transaction and locking:

    Configuration config = new ConfigurationBuilder()
      .locking()
        .concurrencyLevel(10000).isolationLevel(IsolationLevel.REPEATABLE_READ)
        .lockAcquisitionTimeout(12000L).useLockStriping(false)
        .versioning().enable().scheme(VersioningScheme.SIMPLE)
      .transaction()
        .transactionManagerLookup(new GenericTransactionManagerLookup())
        .recovery()
      .statistics()
      .build();
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.