How to Use Spring Cache’s @CacheEvict Annotation to Remove Cached Data

This article explains how the @CacheEvict annotation in Spring Cache can be used to delete specific cache entries or clear entire caches, detailing its parameters such as value, allEntries, and beforeInvocation with a concrete code example.

Coder Trainee
Coder Trainee
Coder Trainee
How to Use Spring Cache’s @CacheEvict Annotation to Remove Cached Data

When working with caches, adding entries is only half the story; removing them is equally important. In Spring Cache, the @CacheEvict annotation serves as a trigger that deletes the specified cache entries each time the annotated method is invoked.

Usage Example

The following code demonstrates a REST endpoint that clears multiple caches using @CacheEvict:

@ApiOperation(value = "清空缓存")
@CacheEvict(value = {"asnavigation","navigation","banner","article",
        "grouphistory","groupinformation","groupoverview","imagetext",
        "singlepage","systemclosed","rechargeoutlets","links","busroute",
        "system_logo"}, key = "#id.toString()", allEntries = true, beforeInvocation = true)
@GetMapping("/clear")
public ApiResult clearCache() {
    ApiResult result = new ApiResult();
    result.getData().put("result", redisUtils.clear());
    return result;
}

From the code we see two key parameters of @CacheEvict: value and allEntries .

value : specifies the names of the caches that should be targeted for removal.

allEntries : determines whether the entire cache (all entries under the specified cache names) is cleared. Its default is false. When set to true, every entry in the listed caches is removed.

If allEntries is false, only the entry identified by the generated key (similar to the key used in @Cacheable) is deleted.

beforeInvocation : when true, the cache eviction occurs before the annotated method executes, guaranteeing that the cache is cleared regardless of whether the method succeeds or throws an exception.

By configuring these attributes appropriately, developers can precisely control cache invalidation behavior, ensuring that stale data does not persist in the application.

The article concludes with a brief reminder to experiment with @CacheEvict and invites readers to share feedback.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCacheSpringspring-bootCacheEvict
Coder Trainee
Written by

Coder Trainee

Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.