Backend Development 9 min read

Master Spring Boot Cache Management with Actuator: An End‑to‑End Guide

This article explains how to use Spring Boot Actuator to monitor and control Spring Cache, covering essential cache annotations, a complete CRUD example with caching, cache management endpoints, custom CacheManager configuration, and optional Redis integration for production‑grade caching.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Spring Boot Cache Management with Actuator: An End‑to‑End Guide

1. Introduction

Caching is a key technique for improving performance, and Spring Cache offers declarative support that simplifies cache management. Effective cache monitoring, dynamic eviction, and programmatic control are essential for stable and responsive applications. This article shows how to use Spring Boot Actuator to manage Spring Cache and how to control caches directly in code.

2. Core Spring Cache Annotations

@EnableCaching : Enable caching, typically placed on the main application class.

@Cacheable : Define a cache; Spring checks the cache before executing the method and returns the cached result if present.

@CachePut : Always execute the method and update the cache with the result.

@CacheEvict : Remove cache entries, either specific keys or all entries.

@Caching : Combine multiple cache operations.

@CacheConfig : Define common cache settings at the class level.

3. Simple Application Example

The following example demonstrates basic CRUD operations with cache annotations. No database is involved; the focus is on cache behavior.

<code>// Cacheable: cache if not present, otherwise return cached value
@Cacheable(cacheNames = "users", key = "#user.id")
public User save(User user) {
    System.out.printf("Saving user: %s%n", user);
    return user;
}

// CachePut: always execute and update cache
@CachePut(cacheNames = "users", key = "#user.id")
public User update(User user) {
    System.out.printf("Updating user: %s%n", user);
    return user;
}

// CacheEvict: remove cache entry
@CacheEvict(cacheNames = "users", key = "#id")
public void deleteById(Long id) {
    System.out.printf("Deleting user [%d]%n", id);
}

// Cacheable with condition to avoid caching null
@Cacheable(cacheNames = "users", key = "#id", unless = "#result == null")
public User queryById(Long id) {
    System.out.printf("Querying user [%d]%n", id);
    return null;
}</code>

Controller endpoints for the service:

<code>@PostMapping("")
public User save(@RequestBody User user) {
    return userService.save(user);
}

@PutMapping("")
public User update(@RequestBody User user) {
    return userService.update(user);
}

@GetMapping("/{id}")
public User queryById(@PathVariable("id") Long id) {
    return userService.queryById(id);
}

@DeleteMapping("/{id}")
public String deleteById(@PathVariable("id") Long id) {
    userService.deleteById(id);
    return "success";
}</code>

4. Managing Cache with Actuator

Add the Actuator starter dependency:

<code><dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency></code>

Expose all actuator endpoints (including cache endpoints) in application.yml :

<code>management:
  endpoints:
    web:
      exposure:
        include: '*'
  base-path: /ac</code>

After restarting, you can view cache information via /ac/caches :

When you invoke the CRUD endpoints, the cache entries appear under the users cache:

To delete a specific cache, call DELETE /ac/caches/{cacheName} :

4.1 Custom CacheManager

You can define your own CacheManager bean:

<code>@Bean
public CacheManager packCacheManager(){
    return new ConcurrentMapCacheManager();
}</code>

Or define multiple managers and mark one as primary:

<code>@Bean
@Primary
public CacheManager packCacheManager(){
    return new ConcurrentMapCacheManager();
}

@Bean
public CacheManager customCacheManager(){
    return new ConcurrentMapCacheManager();
}</code>

Specify the manager in a cache annotation:

<code>@Cacheable(cacheNames = "users", key = "#user.id", cacheManager = "customCacheManager")
public User save(User user) { ... }</code>

4.2 Programmatic Cache Control

Inject CacheManager and manipulate caches directly:

<code>private final CacheManager cacheManager;

public ProductService(CacheManager cacheManager) {
    this.cacheManager = cacheManager;
}

public Product save(Product product) {
    Cache cache = this.cacheManager.getCache("products");
    ValueWrapper wrapper = cache.get(product.getId());
    if (wrapper == null) {
        cache.put(product.getId(), product);
    }
    return product;
}</code>

4.3 Redis Cache Integration

Switch to Redis by configuring the cache type:

<code>spring:
  cache:
    type: redis
    redis:
      cache-null-values: false
      time-to-live: 5m</code>

This enables Redis‑backed caching with configurable TTL.

5. Conclusion

The article covered declarative cache annotations, actuator‑based cache monitoring, custom cache manager setup, programmatic cache manipulation, and Redis integration, providing a comprehensive guide for managing Spring Cache in Spring Boot applications.

backendJavaCacheSpring BootSpring CacheActuator
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login 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.