Chapter 4. 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.

4.1. Declarative Configuration

Declarative configuration conforms to a schema and is defined in XML or JSON formatted files.

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

<infinispan>
   <!-- Defines properties for all caches within the container and optionally names a default cache. -->
   <cache-container default-cache="local">
      <!-- Configures transport properties for clustered cache modes. -->
      <!-- Specifies the default JGroups UDP stack and names the cluster. -->
      <transport stack="udp" cluster="mycluster"/>
      <!-- Configures a local cache. -->
      <local-cache name="local"/>
      <!-- Configures an invalidation cache. -->
      <invalidation-cache name="invalidation"/>
      <!-- Configures a replicated cache. -->
      <replicated-cache name="replicated"/>
      <!-- Configures a distributed cache. -->
      <distributed-cache name="distributed"/>
   </cache-container>
</infinispan>

4.1.1. Cache Templates

Data Grid lets you define templates that you can use to create cache configurations.

For example, the following configuration contains a cache template:

<infinispan>
   <!-- Specifies the cache named "local" as the default. -->
   <cache-container default-cache="local">
      <!-- Adds a cache template for local caches. -->
      <local-cache-configuration name="local-template">
         <expiration interval="10000" lifespan="10" max-idle="10"/>
      </local-cache-configuration>
   </cache-container>
</infinispan>

Inheritance with configuration templates

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

Note

Cache 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 template inheritance:

<infinispan>
   <cache-container>
     <!-- Defines a cache template named "base-template". -->
      <local-cache-configuration name="base-template">
         <expiration interval="10000" lifespan="10" max-idle="10"/>
      </local-cache-configuration>
      <!-- Defines a cache template named "extended-template" that inherits settings from "base-template". -->
      <local-cache-configuration name="extended-template"
                                 configuration="base-template">
         <expiration lifespan="20"/>
         <memory max-size="2GB" />
      </local-cache-configuration>
   </cache-container>
</infinispan>
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" />.

4.1.2. Cache Configuration Wildcards

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

<infinispan>
  <cache-container>
    <!-- Uses the `*` wildcard to match any cache names that start with "basecache". -->
    <local-cache-configuration name="basecache*">
      <expiration interval="10500" lifespan="11" max-idle="11"/>
    </local-cache-configuration>
    <!-- Adds local caches that use the "basecache*" configuration. -->
    <local-cache name="basecache-1"/>
    <local-cache name="basecache-2"/>
  </cache-container>
</infinispan>
Note

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

4.1.3. Multiple Configuration Files

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

<infinispan xmlns:xi="http://www.w3.org/2001/XInclude">
    <cache-container default-cache="cache-1">
        <!-- Includes a local.xml file that contains a cache configuration. -->
        <xi:include href="local.xml" />
    </cache-container>
</infinispan>

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

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

Data Grid configuration 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.

4.2. Data Grid Configuration API

Configure Data Grid programmatically.

Global configuration

Use the GlobalConfiguration class to apply configuration to all caches under the Cache Manager.

GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
  .cacheContainer().statistics(true) 1
  .metrics().gauges(true).histograms(true) 2
  .jmx().enable() 3
  .build();
1
Enables Cache Manager statistics.
2
Exports statistics through the metrics endpoint.
3
Exports statistics via JMX MBeans.

References:

Cache configuration

Use the ConfigurationBuilder class to configure caches.

ConfigurationBuilder builder = new ConfigurationBuilder();
     builder.clustering() 1
            .cacheMode(CacheMode.DIST_SYNC) 2
            .l1().lifespan(25000L) 3
            .hash().numOwners(3) 4
            .statistics().enable(); 5
     Configuration cfg = builder.build();
1
Enables cache clustering.
2
Uses the distributed, synchronous cache mode.
3
Configures maximum lifespan for entries in the L1 cache.
4
Configures three cluster-wide replicas for each cache entry.
5
Enables cache statistics.

References:

4.3. Configuring Caches Programmatically

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.