Caching and messaging

@CacheEvict

Evict a key on successful method completion by default. Source-reviewed integration fragment or manual procedure, NOT RUN.

Formal Syntax & Structure

java syntaxjava
@CacheEvict(cacheNames="catalog", key="#p0 + ':' + #p1")

Working Example

Example Snippetjava
// Fragment: register this bean in a Spring context with @EnableCaching// and a bounded, TTL-configured cache manager. Call through the proxy.import org.springframework.cache.annotation.Cacheable;import org.springframework.cache.annotation.CacheEvict;public class CachedCatalog {    @Cacheable(cacheNames="catalog", key="#p0 + ':' + #p1", sync=true)    public int stock(String tenant, String sku) {        // Fictional read only. Production: authorized tenant-scoped repository.        if (!tenant.equals("tenant-a") || !sku.equals("B1"))            throw new IllegalArgumentException("unknown item");        return 5;    }    @CacheEvict(cacheNames="catalog", key="#p0 + ':' + #p1")    public void invalidate(String tenant, String sku) { }}
Java Reference