Developer-kit spring-boot-cache
Provides patterns for implementing Spring Boot caching: configures Redis/Caffeine/EhCache providers with TTL and eviction policies, applies @Cacheable/@CacheEvict/@CachePut annotations, validates cache hit/miss behavior, and exposes metrics via Actuator. Use when adding caching to Spring Boot services, configuring cache expiration, evicting stale data, or diagnosing cache misses.
git clone https://github.com/giuseppe-trisciuoglio/developer-kit
T=$(mktemp -d) && git clone --depth=1 https://github.com/giuseppe-trisciuoglio/developer-kit "$T" && mkdir -p ~/.claude/skills && cp -r "$T/plugins/developer-kit-java/skills/spring-boot-cache" ~/.claude/skills/giuseppe-trisciuoglio-developer-kit-spring-boot-cache && rm -rf "$T"
plugins/developer-kit-java/skills/spring-boot-cache/SKILL.mdSpring Boot Cache Abstraction
Overview
6-step workflow for enabling cache abstraction, configuring providers (Caffeine, Redis, Ehcache), annotating service methods, and validating behavior in Spring Boot 3.5+ applications. Apply
@Cacheable for reads, @CachePut for
writes, @CacheEvict for deletions. Configure TTL/eviction policies and expose
metrics via Actuator.
When to Use
- Add
,@Cacheable
, or@CachePut
to service methods.@CacheEvict - Configure Caffeine, Redis, or Ehcache with TTL and capacity policies.
- Implement eviction strategies for stale data.
- Diagnose cache misses or invalidation issues.
- Expose hit/miss metrics via Actuator or Micrometer.
Instructions
-
Add dependencies —
plus a provider:spring-boot-starter-cache- Caffeine:
startercaffeine - Redis:
spring-boot-starter-data-redis - Ehcache:
starterehcache
- Caffeine:
-
Enable caching — annotate a
class with@Configuration
and define a@EnableCaching
bean.CacheManager -
Annotate methods —
for reads,@Cacheable
for writes,@CachePut
for deletions.@CacheEvict -
Configure TTL/eviction — set
,spring.cache.caffeine.spec
, orspring.cache.redis.time-to-live
.spring.cache.ehcache.config -
Shape keys — use SpEL in
attributes; guard withkey
/condition
for selective caching.unless -
Validate setup — run integration test to confirm cache hit on second call; check
to verify cache manager registration; queryGET /actuator/caches
for hit/miss ratios.GET /actuator/metrics/cache.gets
Examples
Example 1: Basic @Cacheable
Usage
@Cacheable@Service @CacheConfig(cacheNames = "users") class UserService { @Cacheable(key = "#id", unless = "#result == null") User findUser(Long id) { ... } }
First call → cache miss, repository invoked Second call → cache hit, repository skipped
Example 2: Conditional Caching with SpEL
@Cacheable(value = "products", key = "#id", condition = "#price > 100") public Product getProduct(Long id, BigDecimal price) { ... } // Only expensive products are cached
Example 3: Cache Eviction
@CacheEvict(value = "users", key = "#id") public void deleteUser(Long id) { ... }
For progressive scenarios (basic product cache, multilevel eviction, Redis
integration), load
.references/cache-examples.md
Advanced Options
- Use JCache annotations (
,@CacheResult
) for providers favoring JSR-107 interoperability; avoid mixing with Spring annotations on the same method.@CacheRemove - Cache reactive return types (
,Mono
) orFlux
values.CompletableFuture - Apply HTTP
headers when exposing cached responses via REST.CacheControl - Schedule periodic eviction with
for time-bound caches.@Scheduled - Create a
for programmaticCacheManagementService
.cacheManager.getCache(name)
Troubleshooting
If cache misses persist after adding
@Cacheable:
- Verify
is present on a@EnableCaching
class.@Configuration - Confirm the method is public and called from outside the class (Spring uses proxies; self-invocation bypasses the cache).
- Validate SpEL key expressions resolve correctly.
- Confirm the cache manager bean is registered as
or explicitly referenced viacacheManager
.cacheManager = "myCacheManager"
References
: curated excerpts from Spring Framework Reference Guide.references/spring-framework-cache-docs.md
: narrative overview from Spring documentation.references/spring-cache-doc-snippet.md
: annotation parameters, dependency matrices, property catalogs.references/cache-core-reference.md
: end-to-end examples with tests.references/cache-examples.md
Best Practices
- Prefer constructor injection and immutable DTOs for cache entries.
- Separate cache names per aggregate (
,users
) to simplify eviction.orders - Log cache hits/misses only at debug; push metrics via Micrometer.
- Tune TTLs based on data staleness tolerance; document rationale in code.
- Guard caches storing PII or credentials with encryption or avoid caching.
- Align cache eviction with transactional boundaries to prevent dirty reads.
Constraints and Warnings
- Avoid caching mutable entities that depend on open persistence contexts.
- Do not mix Spring cache annotations with JCache annotations on the same method.
- Validate serialization compatibility when caching across service instances.
- Monitor memory footprint to prevent OOM with in-memory stores.
- Caffeine + Redis multi-level caches require publish/subscribe invalidation channels.