Instruction set for enabling and operating the Spring Cache abstraction in Spring Boot when implementing application-level caching for performance-sensitive workloads.
Spring Boot ships with a cache abstraction that wraps expensive service calls behind annotation-driven caches. This abstraction supports multiple cache providers (ConcurrentMap, Caffeine, Redis, Ehcache, JCache) without changing business code. The skill provides a concise workflow for enabling caching, managing cache lifecycles, and validating behavior in Spring Boot 3.5+ services.
@Cacheable, @CachePut, or @CacheEvict to Spring Boot service methods.Use trigger phrases such as "implement service caching", "configure CaffeineCacheManager", "evict caches on update", or "test Spring cache behavior" to load this skill.
spring-boot-starter-cache; add provider-specific starters as
needed (spring-boot-starter-data-redis, caffeine, ehcache, etc.).Add dependencies
<!-- Maven -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency> <!-- Optional: Caffeine -->
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
implementation "org.springframework.boot:spring-boot-starter-cache"
implementation "com.github.ben-manes.caffeine:caffeine"
Enable caching
@Configuration
@EnableCaching
class CacheConfig {
@Bean
CacheManager cacheManager() {
return new CaffeineCacheManager("users", "orders");
}
}
Annotate service methods
@Service
@CacheConfig(cacheNames = "users")
class UserService {
@Cacheable(key = "#id", unless = "#result == null")
User findUser(Long id) { ... }
@CachePut(key = "#user.id")
User refreshUser(User user) { ... }
@CacheEvict(key = "#id", beforeInvocation = false)
void deleteUser(Long id) { ... }
}
Verify behavior
cache endpoint (if enabled) for hit/miss counters.Kaynak: Spring Boot Caching Guide & Caffeine Cache Best Practices
allEntries=true opsiyonunu değerlendir).cache endpoint'i üzerinden hit/miss oranlarını izle.@SpringBootTest ile cache izolasyonunu ve tutarlılığını test et.| Aşama | Doğrulama |
|---|---|
| 1 | Transactional işlemler sırasında cache tutarlılığı (Data drift) bozuluyor mu? |
| 2 | "Cache-aside" veya "ReadOnly" stratejisi doğru uygulandı mı? |
| 3 | Çoklu instance yapısında "Cache Stampede" riski önlendi mi? |
Cache Patterns v2.0 - With Workflow
@CacheResult, @CacheRemove). Avoid mixing with Spring annotations
on the same method.Mono, Flux) or CompletableFuture values.
Spring stores resolved values and resubscribes on hits; consider TTL alignment
with publisher semantics.CacheControl when exposing cached responses
via REST.references/cache-examples.md for
progressive scenarios (basic product cache, conditional caching, multilevel
eviction, Redis integration).references/cache-core-reference.md
for annotation matrices, configuration tables, and property samples.references/spring-framework-cache-docs.md:
curated excerpts from the Spring Framework Reference Guide (official).references/spring-cache-doc-snippet.md:
narrative overview extracted from Spring documentation.references/cache-core-reference.md:
annotation parameters, dependency matrices, property catalogs.references/cache-examples.md:
end-to-end examples with tests.users, orders) to simplify eviction.