Janning Vygen
Janning Vygen

Reputation: 9192

Infinispan 8 - cache.clear() - How to understand the javadoc correctly?

In infinispan 8.2.5.Final I have one use case where I want to evict/clear all entries from the cache. I am calling cache.clear(). The Javadoc says:

void org.infinispan.Cache.clear()

Removes all mappings from the cache.

Note: This should never be invoked in production unless you can guarantee no other invocations are ran concurrently.

If the cache is transactional, it will not interact with the transaction.

Specified by: clear() in Map

Does it mean "no other invocations of cache.clear()"? Or does it mean "no other invocations of the underlying cache at all"? So no cache.put and cache.get concurrently?

Its a common use case to clear the cache (new data is coming in). Did I use the wrong method? Are there other ways to evict all items from an infinispan cache?

Edit: I am running my cache in invalidation mode. Does the clear() operation invalidate all entries in all nodes or just in the local node?

Upvotes: 1

Views: 1231

Answers (1)

pruivo
pruivo

Reputation: 1334

It means "no other write operation can be run". The clear() may corrupt the concurrent write operation (it can be applied in some nodes, but missing in another) since it doesn't try to acquire any locking.

If you are worried about concurrent writes, you can use the stream() or the iterator(). As an example:

cache.keySet().stream().forEach(BasicCache::remove);

or

for (Iterator<K> it = cache.keySet().iterator(); it.hasNext(); ) {
    it.next();
    it.remove();
}

should do the trick.

Upvotes: 5

Related Questions