Step 4: Configuring the default cache

In the previous step we used the overloaded put() method to store mortal entries. But since we want all of our entries to expire with the same lifespan, we can configure the cache to have default expiration values. To do this we will construct the DefaultCacheManager by passing in a org.infinispan.configuration.cache.Configuration object. A configuration in Infinispan is mostly immutable, aside from some runtime-tunable parameters, and is constructed by means of a ConfigurationBuilder. Using the above use-case, let's create a cache configuration where we want to set the default expiration of entries to 5 seconds. The following code shows how it's done:

ConfigurationBuilder config = new ConfigurationBuilder();
config.expiration().lifespan(5, TimeUnit.SECONDS);
cacheManager = new DefaultCacheManager(config.build());
The configuration builder uses a fluent pattern, so you can tune more configuration aspects with chained methods. Running the application again: With this in mind let's run our application again:
$ git checkout -f step-4
$ mvn clean package exec:exec

will yield the same result as we got in the previous step. We didn't change the behaviour of the application, we just changed the semantics of the cache.

back to top