How to iterate over an Infinispan cache

The simplest way to iterate over an Infinispan Cache is by means of iterating over its Set of entries, as in the following example, which uses JDK 1.8 (Lambda expression) style:

cache.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));

 The above example will print cache statistics for the whole cluster. On the other hand, if you want to display Cache entries just for the current node, then you could use the Flag.SKIP_REMOTE_LOOKUP on the Cache object:

cache.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP)
.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));

 See the full example: https://github.com/infinispan/infinispan-simple-tutorials/blob/master/distributed/src/main/java/org/infinispan/tutorial/simple/distributed/InfinispanDistributed.java

Follow us on Twitter