Multimap Cache with Embedded Infinispan
What You Will Learn
How to use the Infinispan embedded multimap cache to associate multiple values with a single key, using the asynchronous MultimapCache API.
Prerequisites
-
Java 17+
Step 1: Add Dependencies
Add the Infinispan core and multimap modules to your pom.xml:
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-multimap</artifactId>
</dependency>
Step 2: Create a Multimap Cache
Obtain a MultimapCacheManager from a regular cache manager, then define and retrieve a multimap cache:
// Construct a local cache manager with default configuration
cacheManager = new DefaultCacheManager();
// Obtain a multimap cache manager from the regular cache manager
multimapCacheManager = EmbeddedMultimapCacheManagerFactory.from(cacheManager);
// Define de multimap cache configuration, as a regular cache
multimapCacheManager.defineConfiguration("multimap", new ConfigurationBuilder().build());
// Get the MultimapCache
multimap = multimapCacheManager.get("multimap");
Step 3: Store and Retrieve Multiple Values
Put multiple values under the same key. All operations return CompletableFuture:
// Store multiple values in a key
CompletableFuture.allOf(
multimap.put("key", "value1"),
multimap.put("key", "value2"),
multimap.put("key", "value3"))
.whenComplete((nil, ex) -> {
// Retrieve the values
multimap.get("key").whenComplete((values, ex2) -> {
// Print them out
System.out.println(values);
});
}).get(10, TimeUnit.SECONDS);
Step 4: Run the Tutorial
mvn package exec:java
The output prints a collection containing value1, value2, and value3 for the single key.
What’s Next
-
Start with basic map operations for single-value caching
-
Try Ickle queries to search cached data


