Blogs Hot Rod per-cache configuration

Hot Rod per-cache configuration

Aside from being able to configure a Java Hot Rod client through a compact URI representation, Infinispan 11 brings some additional changes to remote cache configuration.

While remote caches did have some client-side configuration, this was never implemented cleanly, resorting to multiple overloaded variations on the getCache() method, for example to obtain a transactional cache, or enabling near-caching.

Infinispan 11 now allows specifying per-cache configuration both through the API and through the declarative properties file.

Let’s look at a few examples.

ConfigurationBuilder builder = new ConfigurationBuilder()
    .uri("hotrod://127.0.0.1");
    .remoteCache("closecache")
        .nearCacheMode(NearCacheMode.INVALIDATED)
        .nearCacheMaxEntries(10000)
    .remoteCache("txcache")
        .transactionMode(TransactionMode.NON_XA);
RemoteCacheManager manager = new RemoteCacheManager(builder.build());

In the above code snippet, we enable near-caching for the cache closecache and we enable NON_XA transactions on the cache txcache.

The equivalent hotrod-client.properties file:

infinispan.client.hotrod.uri=hotrod://127.0.0.1
infinispan.client.hotrod.cache.closecache.near_cache.mode=INVALIDATED
infinispan.client.hotrod.cache.closecache.near_cache.max_entries=10000
infinispan.client.hotrod.cache.txcache.transaction.transaction_mode=NON_XA

Automatic cache creation

A neat feature that has been implemented as part of per-cache configuration, is the ability to automatically create a cache on the server on first use, if it is missing, by supplying either an existing template or a full-blown configuration.

ConfigurationBuilder builder = new ConfigurationBuilder()
    .uri("hotrod://127.0.0.1");
    .remoteCache("mydistcache")
        .templateName("org.infinispan.DIST_SYNC");
RemoteCacheManager manager = new RemoteCacheManager(builder.build());
Cache<String, String> cache = manager.getCache("mydistcache");
...

The above example using a properties file would look like:

infinispan.client.hotrod.uri=hotrod://127.0.0.1
infinispan.client.hotrod.cache.mydistcache.template=org.infinispan.DIST_SYNC

Get it, Use it, Ask us!

We’re hard at work on new features, improvements and fixes, so watch this space for more announcements!

Please, download and test the latest release.

The source code is hosted on GitHub. If you need to report a bug or request a new feature, look for a similar one on our GitHub issues tracker. If you don’t find any, create a new issue.

If you have questions, are experiencing a bug or want advice on using Infinispan, you can use GitHub discussions. We will do our best to answer you as soon as we can.

The Infinispan community uses Zulip for real-time communications. Join us using either a web-browser or a dedicated application on the Infinispan chat.

Tristan Tarrant

Tristan has been leading the Infinispan Engineering Team at Red Hat for quite a while now, as well as being Principal Architect for Red Hat Data Grid. He's been a passionate open-source advocate and contributor for over three decades.