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'
}

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