Skip to content

Introduce immediate attribute on @CacheEvict#36858

Open
guhyunwoo wants to merge 1 commit into
spring-projects:mainfrom
guhyunwoo:gh-36806-immediate-cache-evict
Open

Introduce immediate attribute on @CacheEvict#36858
guhyunwoo wants to merge 1 commit into
spring-projects:mainfrom
guhyunwoo:gh-36806-immediate-cache-evict

Conversation

@guhyunwoo
Copy link
Copy Markdown

Fixes #36806.

By default, @CacheEvict delegates to Cache#clear / Cache#evict, which the Cache contract allows to be performed in an asynchronous or deferred fashion. With a Redis-backed cache, for example, Cache#clear may translate to an asynchronous UNLINK that can later remove an entry written by a concurrent @CachePut.

This PR introduces a new immediate attribute on @CacheEvict that delegates to Cache#invalidate / Cache#evictIfPresent instead, expecting all affected entries to be invisible for subsequent lookups as soon as the operation returns.

beforeInvocation=true continues to imply immediate eviction for backwards compatibility, since the original contract has always used Cache#invalidate in that case.

It also aligns the call site with the existing immediate parameter on AbstractCacheInvoker#doEvict / #doClear, which CacheAspectSupport had been driving from isBeforeInvocation(), conflating scheduling with visibility.

By default, @CacheEvict delegates to Cache#clear / Cache#evict, which
the Cache contract allows to be performed asynchronously or in a
deferred fashion. With a Redis-backed cache, for example, Cache#clear
may translate to an asynchronous UNLINK that can later remove an entry
written by a concurrent @cACHEpUT.

The new immediate attribute delegates to Cache#invalidate /
Cache#evictIfPresent instead, expecting all affected entries to be
invisible for subsequent lookups as soon as the operation returns.
beforeInvocation=true continues to imply immediate eviction.

This also aligns the call site with the existing immediate parameter
on AbstractCacheInvoker#doEvict / #doClear, which CacheAspectSupport
had been driving from isBeforeInvocation(), conflating scheduling
with visibility.

Closes spring-projectsgh-36806

Signed-off-by: Hyunwoo Gu <realization0327@gmail.com>
@guhyunwoo guhyunwoo force-pushed the gh-36806-immediate-cache-evict branch from e4be6b3 to 5867512 Compare June 1, 2026 02:19
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@CacheEvict(allEntries=true) may delete newly inserted keys when beforeInvocation=false

3 participants