Chriskt
Chriskt

Reputation: 87

What's the difference between @CacheEvict(...) and @CacheEvict(value , allEntries )?

I noticed a few lines of @CacheEvict("Settings") in some source code and was curious if does the same thing as CacheEvict(value = "Settings" , allEntries = true )?

Upvotes: 1

Views: 4333

Answers (2)

teppic
teppic

Reputation: 7286

When @CacheEvict is specified without a key attribute, all method parameters are used to construct the key for the entry to be evicted, so

@CacheEvict("Settings")
public String doThing(String foo, Integer bar) {
    // ...
}

... will evict the entry with the composite key {foo, bar}. The default key generator used to construct the composite key is SimpleKeyGenerator, which returns SimpleKey instances that hold references to the method parameters for comparison.

If the method has no parameters, the default key is SimpleKey.EMPTY.

The attribute allEntries can't be set to true if you do specify a key attribute; they're mutually exclusive. If it is set to true all entries in the cache will be removed every time the annotated method is called.

So...

@CacheEvict("Settings", allEntries = true)
public String doThing(String foo, Integer bar) {
    // ...
}

... will empty the Settings cache every time the method is called, regardless of the method parameters.

Upvotes: 3

LppEdd
LppEdd

Reputation: 21104

Just give a look at its Javadoc.
You'll notice the default value for allEntries is false. So no, it is not the same thing.
Use allEntries = true only when you want a fresh cache each time an operation is performed.

Whether all the entries inside the cache(s) are removed. By default, only the value under the associated key is removed.

If you want to see the implementation details, just look at Coffeine.

Upvotes: 3

Related Questions