Cache / Redis

Redis cache for Spring Boot microservices

Spring Middleware provides a Redis-based cache module for Spring Boot microservices. It adds annotation-driven TTL configuration, runtime cache inspection, Micrometer-backed statistics, and programmatic cache operations while keeping caching behavior explicit and observable.

Redis cache integration Annotation-driven TTL Runtime cache statistics Micrometer metrics Cache inspection endpoints Programmatic cache management

What the cache module provides

The cache module extends Spring caching with Redis-specific helpers and runtime introspection. It is designed to keep cache behavior explicit instead of scattering TTL rules, statistics access, and cache clearing logic across services.

  • Annotation-driven TTL configuration
  • Automatic Redis cache registration
  • Runtime cache inspection endpoints
  • Micrometer-backed cache statistics

Core runtime pieces

  • @RedisCacheConfiguration: method-level TTL configuration for caches declared with @Cacheable.
  • RedisCacheConfigurationSupport: scans methods and registers per-cache Redis settings.
  • CacheService: clears caches and exposes runtime statistics.
  • CacheController: small REST API for cache config and inspection.

High-level cache view

Services declare cacheable methods as usual, then optionally add Redis-specific TTL metadata. The module collects that information at startup and wires the Redis cache manager accordingly.

Application Service Spring Middleware Cache Module TTL configuration • statistics inspection • cache management Redis Cache

How it works

Cache configuration stays close to the application code, but the module translates it into Redis cache manager settings at startup.

01

Declare a cacheable method

Use Spring's @Cacheable("my-cache") on the method as usual.

02

Add Redis TTL metadata

Optionally annotate the same method with @RedisCacheConfiguration.

03

Register runtime cache config

RedisCacheConfigurationSupport scans methods and customizes the Redis cache manager builder.

Startup behavior

The module uses reflection to find methods annotated with both @Cacheable and @RedisCacheConfiguration, resolves TTL values, and registers per-cache settings.

Runtime behavior

Once configured, cache statistics and cache operations can be observed and managed through the service API or the built-in controller endpoints.

Configuration

Redis caching builds on Spring cache configuration. Redis infrastructure is assumed to be configured separately.

Minimal configuration

spring:
  cache:
    type: redis
    redis:
      enable-statistics: true

TTL placeholder example

@Cacheable("catalog-events")
@RedisCacheConfiguration(
    ttlString = "${CACHE_TTL_SECONDS:300}",
    chronoUnitString = "SECONDS"
)
List<Item> findByCatalogId(UUID catalogId) { ... }

RedisCacheConfiguration options

  • ttl — numeric TTL
  • chronoUnit — time unit
  • ttlString — placeholder-based TTL
  • chronoUnitString — placeholder-based time unit

Recommended approach

Prefer ttlString with environment placeholders in production so cache TTLs can be tuned without code changes.

Runtime inspection and metrics

The module exposes runtime information about caches and can integrate with Micrometer metrics when statistics are enabled.

Cache statistics

Runtime statistics include hit, miss, pending, and puts through the CacheInformationStatistics DTO.

Micrometer integration

The service reads meters such as cache.gets and cache.puts to expose cache behavior programmatically.

Configured caches

The module can expose known cache names and their Redis configuration parameters at runtime.

Cache controller endpoints

A small REST API is available under /cache for inspection and cache clearing.

GET /cache/stadistics

Returns a collection of CacheInformationStatistics, optionally filtered by cache name.

GET /cache/config

Returns the current Redis cache configuration parameters known by the module.

GET /cache/clear

Clears the named cache and returns whether the operation succeeded.

Programmatic API

Services can interact with runtime cache information through the CacheService.

CacheService example

@Autowired
private CacheService cacheService;

cacheService.clearCache("catalog-events");

Collection<CacheInformationStatistics> stats =
    cacheService.getCacheInformationStadistics(
        CacheInformationStadisticsFilter.builder()
            .cacheNames(List.of("catalog-events"))
            .build()
    );

Why use the service API

  • Operational control: clear or inspect caches programmatically.
  • Consistency: use one platform API instead of custom cache utilities per service.
  • Observability: keep statistics accessible from service code.

Advanced usage

The module can be extended programmatically and includes a default JSON serializer suited for complex cached objects.

Programmatic extensions

RedisCacheConfigurationSupport#getSpecificRedisCacheConfigurations() can be overridden by framework modules to add extra cache configurations.

Default serializer

The module registers a GenericJackson2JsonRedisSerializer backed by an ObjectMapper configured for JDK8 and Java time types, with polymorphic typing enabled.

Best practices

Cache effectiveness depends on naming, TTL control, statistics usage, and serialization choices.

Use descriptive cache names

Keep cache responsibilities focused and avoid mixing unrelated workloads in one cache.

Enable statistics selectively

Use spring.cache.redis.enable-statistics when you need runtime visibility, but avoid unnecessary overhead.

Prefer environment-driven TTLs

Use placeholder-based TTL configuration so runtime tuning does not require code changes.

Where to look in the code

The cache module is centered around annotation processing, runtime cache configuration, and operational APIs.

RedisCacheConfiguration

cache annotations

.../cache/annotations/RedisCacheConfiguration.java

RedisCacheConfigurationSupport

cache config

.../cache/config/RedisCacheConfigurationSupport.java

CacheServiceImpl

cache service

.../cache/service/CacheServiceImpl.java