简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM