Search

Chapter 4. Configuring Data Grid Caches

download PDF

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

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.

4.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" />.

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

4.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-11.0.xsd schema:

include-with-schema.xml

<local-cache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="urn:infinispan:config:11.0 https://infinispan.org/schemas/infinispan-config-fragment-11.0.xsd"
             xmlns="urn:infinispan:config:11.0"
             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.

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