Configuration

The RedisCacheConfiguration object is the entry point to configure a Redis cache and enable its features.

Key Function

Use keyFunction(KeyFunction function) to set the KeyFunction used to compute Redis keys.

Default is KeyFunction.SIMPLE which produces keys in the form <cache>:<key>.

Key Expiration

To enable entry time-to-live (TTL) use entryTtl(Duration duration) or entryTtl(TtlFunction function).

Default is TtlFunction.PERSISTENT which does not expire keys.

RedisCacheConfiguration config = RedisCacheConfiguration.defaultConfig()
    .entryTtl(Duration.ofMinutes(10));

Redis Data Type

Use redisType(RedisType type) to change the type of data-structure backing the cache.

Possible values are HASH, STRING, and JSON.

Default is HASH.

RedisCacheConfiguration config = RedisCacheConfiguration.defaultConfig()
    .redisType(RedisType.JSON);

Value Mappers

Each type has a corresponding value mapper which can be overridden:

HASH

Set with RedisCacheConfiguration.hashMapper(RedisHashMapper mapper). Defaults to com.redis.cache.common.mapping.ObjectHashMapper.

STRING

Set with RedisCacheConfiguration.stringMapper(RedisStringMapper mapper). Defaults to com.redis.cache.common.mapping.JdkSerializationStringMapper.

JSON

Set with RedisCacheConfiguration.jsonMapper(RedisStringMapper mapper). Defaults to com.redis.cache.common.mapping.GenericJackson2JsonMapper.

Jackson Version Selection

The default JSON mapper remains Jackson 2 for backward compatibility. The library keeps both Jackson major lines optional, so the mapper you select must already be present on your application classpath.

Use jackson2JsonMapper() or jackson3JsonMapper() to select the cache JSON mapper explicitly:

RedisCacheConfiguration boot3Config = RedisCacheConfiguration.defaultConfig()
    .json()
    .jackson2JsonMapper();

RedisCacheConfiguration boot4Config = RedisCacheConfiguration.defaultConfig()
    .json()
    .jackson3JsonMapper();

If you already have a customized application ObjectMapper, adapt it with the mapper builder so Redis Cache preserves your custom serializers and deserializers while enabling the cache-specific type metadata it needs:

RedisCacheConfiguration config = RedisCacheConfiguration.defaultConfig()
    .json()
    .jsonMapper(GenericJackson3JsonMapper.builder()
        .objectMapper(objectMapper)
        .defaultTyping(true)
        .registerNullValueSerializer(true)
        .build());

Async Mode

Use async(boolean async) to enable asynchronous (non-blocking) cache operations.

When async mode is enabled (async(true)), write operations like put() and evict() return immediately without waiting for Redis to complete the operation. This can significantly improve application throughput by reducing latency for cache write operations.

RedisCacheConfiguration config = RedisCacheConfiguration.defaultConfig()
    .async(true);

Default is false (synchronous mode).

In async mode:

  • put() and evict() operations are non-blocking (fire-and-forget)

  • get() and putIfAbsent() operations are always blocking, regardless of async mode

  • There may be a brief delay before written values are visible in Redis

  • Async mode works with all Redis data types (HASH, STRING, JSON)

Async mode is particularly useful for:

  • High-throughput applications where cache write latency is a bottleneck

  • Scenarios where eventual consistency is acceptable

  • Applications that can tolerate brief delays in cache propagation

// Example: High-throughput cache configuration
RedisCacheConfiguration config = RedisCacheConfiguration.defaultConfig()
    .async(true)
    .entryTtl(Duration.ofMinutes(10))
    .redisType(RedisType.JSON);

RedisCacheManager cacheManager = RedisCacheManager.builder(client)
    .defaults(config)
    .build();

Next Steps