Usage

This guide shows you how to integrate Redis Cache Java into your Java application.

Dependency

Add Redis Cache Java to your project using Maven or Gradle.

Maven

<dependency>
    <groupId>com.redis</groupId>
    <artifactId>redis-cache-core</artifactId>
    <version>0.5.0</version>
</dependency>

Gradle

dependencies {
    implementation 'com.redis:redis-cache-core:0.5.0'
}

Spring Boot 3 Compatibility

Redis Cache Java is built primarily against Spring Boot 4. A Spring Boot 3 variant is published with a -spring-boot3 suffix (for example, redis-cache-core-spring-boot3).

When using the Boot 3 variant, you must override two managed versions in your build because Spring Boot 3.x’s BOM pins older Lettuce and Netty versions than Redis Cache Java requires:

ext['lettuce.version'] = '7.5.1.RELEASE'
ext['netty.version']   = '4.2.12.Final'

Without these overrides, you will see NoClassDefFoundError: io/lettuce/core/api/sync/RediSearchCommands (Lettuce too old) or NoClassDefFoundError: io/netty/channel/MultiThreadIoEventLoopGroup (Netty too old). Netty 4.2.x is binary-compatible with 4.1.x for the Spring Boot surface area.

For Maven, set the same properties in your <properties> block:

<properties>
    <lettuce.version>7.5.1.RELEASE</lettuce.version>
    <netty.version>4.2.12.Final</netty.version>
</properties>

Basic Setup

To use Redis Cache Java as a backing implementation, add RedisCacheManager to your configuration:

@Bean
public RedisCacheManager cacheManager(RedisClient client) {
   return RedisCacheManager.create(client);
}

Then you can tag the methods in your application that you want to apply caching to.

Cached Method Example

@Cacheable("popular")
public List<MovieDb> fetchPopularMovies(int page) throws Exception {
    return tmdbService.fetchPopularMovies(page);
}

Advanced Configuration

RedisCacheManager behavior can be configured with RedisCacheManagerBuilder, letting you set the default RedisCacheConfiguration and predefined caches.

RedisCacheManager cacheManager = RedisCacheManager.builder(client)
  .defaults(RedisCacheConfiguration.defaultConfig())
  .configuration("hashCache", RedisCacheConfiguration.defaultConfig().hash())
  .configuration("jsonCache", RedisCacheConfiguration.defaultConfig().json())
  .configuration("stringCache", RedisCacheConfiguration.defaultConfig().string())
  .build();

As shown in the preceding example, RedisCacheManager allows custom configuration on a per-cache basis.

The behavior of RedisCache created by RedisCacheManager is defined with RedisCacheConfiguration.

Next Steps