Files
gh-giuseppe-trisciuoglio-de…/skills/spring-boot-cache/references/spring-framework-cache-docs.md
2025-11-29 18:28:34 +08:00

3.9 KiB

Spring Framework Cache Reference (Official)

Curated excerpts from the official Spring Framework reference documentation covering caching fundamentals and annotation usage. Source pages are from the Spring Framework Reference Guide 6.2.

Cache Abstraction Overview

  • Purpose: Transparently wrap expensive service methods and reuse results resolved from configured cache managers.

  • Enablement:

    @Configuration
    @EnableCaching
    public class CacheConfig {
        @Bean
        public CacheManager cacheManager() {
            return new ConcurrentMapCacheManager("books");
        }
    }
    

    Source: /integration/cache/annotations

  • Supported return types: CompletableFuture, Reactor Mono/Flux, blocking objects, and collections are all cacheable. For async/reactive types, Spring stores the resolved value and rehydrates it on retrieval.

Core Annotations

@Cacheable

  • Cache the method invocation result using the provided cache name and key.
  • Supports conditional caching with condition (pre-invocation) and unless (post-invocation, access #result).
@Cacheable(cacheNames = "book", condition = "#isbn.length() == 13", unless = "#result.hardback")
public Book findBook(String isbn) { ... }

Source: /integration/cache/annotations

@CachePut and @CacheEvict

  • @CachePut: Always run the method and update cache entry with fresh result.
  • @CacheEvict: Remove entries; use allEntries = true or beforeInvocation for pre-call eviction.
@CacheEvict(cacheNames = "books", key = "#isbn", beforeInvocation = true)
public void reset(String isbn) { ... }

Source: /integration/cache/annotations

@Caching

  • Bundle multiple cache operations on a single method:
@Caching(evict = {
    @CacheEvict("primary"),
    @CacheEvict(cacheNames = "secondary", key = "#isbn")
})
public Book importBooks(String isbn) { ... }

Source: /integration/cache/annotations

Store Configuration Highlights

  • Caffeine: Configure CaffeineCacheManager to create caches on demand.

    @Bean
    CacheManager cacheManager() {
        return new CaffeineCacheManager();
    }
    

    Source: /integration/cache/store-configuration

  • XML alternative: Use <cache:annotation-driven cache-manager="..."/> when annotation configuration is not feasible.

    <cache:annotation-driven cache-manager="cacheManager"/>
    <bean id="cacheManager" class="org.springframework.cache.caffeine.CaffeineCacheManager"/>
    

    Source: /integration/cache/declarative-xml

Reactive and Async Support

  • @Cacheable works with asynchronous signatures:

    @Cacheable("books")
    public Mono<Book> findBook(ISBN isbn) { ... }
    
    @Cacheable(cacheNames = "foos", sync = true)
    public CompletableFuture<Foo> executeExpensiveOperation(String id) { ... }
    

    Source: /integration/cache/annotations

Additional Resources