The English version of quarkus.io is the official project site. Translated sites are community supported on a best-effort basis.

Redis Cache

By default, Quarkus Cache uses Caffeine as backend. It’s possible to use Redis instead.

这项技术被认为是preview。

preview(预览) 中,不保证向后兼容和在生态系统中的存在。具体的改进可能需要改变配置或API,并且正在计划变得 稳定 。欢迎在我们的 邮件列表 中提供反馈,或在我们的 GitHub问题列表 中提出问题。

For a full list of possible statuses, check our FAQ entry.

Redis as cache backend

When using Redis as the backend for Quarkus cache, each cached item will be stored in Redis:

  • The backend uses the <default> Redis client (if not configured otherwise), so make sure it’s configured (or use the redis dev service)

  • the Redis key is built as follows: cache:$cache-name:$cache-key, where cache-key is the key the application uses.

  • the value is encoded to JSON if needed

Use the Redis backend

First, you need to add the quarkus-redis-cache extension to your project:

pom.xml
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-redis-cache</artifactId>
</dependency>
build.gradle
implementation("io.quarkus:quarkus-redis-cache")

Then, use the @CacheResult and others cache annotations as explained in the Quarkus Cache guide:

@GET
@Path("/{keyElement1}/{keyElement2}/{keyElement3}")
@CacheResult(cacheName = "expensiveResourceCache")
public ExpensiveResponse getExpensiveResponse(@PathParam("keyElement1") @CacheKey String keyElement1,
        @PathParam("keyElement2") @CacheKey String keyElement2, @PathParam("keyElement3") @CacheKey String keyElement3,
        @QueryParam("foo") String foo) {
    invocations.incrementAndGet();
    ExpensiveResponse response = new ExpensiveResponse();
    response.setResult(keyElement1 + " " + keyElement2 + " " + keyElement3 + " too!");
    return response;
}

@POST
@CacheInvalidateAll(cacheName = "expensiveResourceCache")
public void invalidateAll() {

}

Configure the Redis backend

The Redis backend uses the <default> Redis client. See the Redis reference to configure the access to Redis.

In dev mode, you can use the Redis Dev Service.

If you want to use another Redis for your cache, configure the client-name as follows:

quarkus.cache.redis.client-name=my-redis-for-cache

When writing to Redis or reading from Redis, Quarkus needs to know the type. Indeed, the objects need to be serialized and deserialized. For that purpose, you may need to configure type (class names) of the key and value you want to cache. At build time, Quarkus tries to deduce the types from the application code, but that decision can be overridden using:

# Default configuration
quarkus.cache.redis.key-type=java.lang.String
quarkus.cache.redis.value-type=org.acme.Person

# Configuration for `expensiveResourceCache`
quarkus.cache.redis.expensiveResourceCache.key-type=java.lang.String
quarkus.cache.redis.expensiveResourceCache.value-type=org.acme.Supes

You can also configure the time to live of the cached entries:

# Default configuration
quarkus.cache.redis.expire-after-write=10s

# Configuration for `expensiveResourceCache`
quarkus.cache.redis.expensiveResourceCache.expire-after-write=1h

If the expire-after-write is not configured, the entry won’t be evicted. You would need to invalidate the values using the @CacheInvalidateAll or @CacheInvalidate annotations.

The following table lists the supported properties:

Configuration property fixed at build time - All other configuration properties are overridable at runtime

Configuration property

类型

默认

The name of the named Redis client to be used for communicating with Redis. If not set, use the default Redis client.

Environment variable: QUARKUS_CACHE_REDIS_CLIENT_NAME

Show more

string

The default type of the value stored in the cache.

Environment variable: QUARKUS_CACHE_REDIS_VALUE_TYPE

Show more

string

The key type, String by default.

Environment variable: QUARKUS_CACHE_REDIS_KEY_TYPE

Show more

string

Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry’s creation, or the most recent replacement of its value.

Environment variable: QUARKUS_CACHE_REDIS_EXPIRE_AFTER_WRITE

Show more

Duration

Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the last access of its value.

Environment variable: QUARKUS_CACHE_REDIS_EXPIRE_AFTER_ACCESS

Show more

Duration

the key prefix allowing to identify the keys belonging to the cache. If not set, use "cache:$cache-name"

Environment variable: QUARKUS_CACHE_REDIS_PREFIX

Show more

string

Whether the access to the cache should be using optimistic locking. See Redis Optimistic Locking for details. Default is false.

Environment variable: QUARKUS_CACHE_REDIS_USE_OPTIMISTIC_LOCKING

Show more

boolean

Additional configuration applied to a specific Redis cache (highest precedence)

类型

默认

The default type of the value stored in the cache.

Environment variable: QUARKUS_CACHE_REDIS__CACHE_NAME__VALUE_TYPE

Show more

string

The key type, String by default.

Environment variable: QUARKUS_CACHE_REDIS__CACHE_NAME__KEY_TYPE

Show more

string

Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry’s creation, or the most recent replacement of its value.

Environment variable: QUARKUS_CACHE_REDIS__CACHE_NAME__EXPIRE_AFTER_WRITE

Show more

Duration

Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the last access of its value.

Environment variable: QUARKUS_CACHE_REDIS__CACHE_NAME__EXPIRE_AFTER_ACCESS

Show more

Duration

the key prefix allowing to identify the keys belonging to the cache. If not set, use "cache:$cache-name"

Environment variable: QUARKUS_CACHE_REDIS__CACHE_NAME__PREFIX

Show more

string

Whether the access to the cache should be using optimistic locking. See Redis Optimistic Locking for details. Default is false.

Environment variable: QUARKUS_CACHE_REDIS__CACHE_NAME__USE_OPTIMISTIC_LOCKING

Show more

boolean

About the Duration format

To write duration values, use the standard java.time.Duration format. See the Duration#parse() Java API documentation for more information.

You can also use a simplified format, starting with a number:

  • If the value is only a number, it represents time in seconds.

  • If the value is a number followed by ms, it represents time in milliseconds.

In other cases, the simplified format is translated to the java.time.Duration format for parsing:

  • If the value is a number followed by h, m, or s, it is prefixed with PT.

  • If the value is a number followed by d, it is prefixed with P.

Configure the Redis key

By default, the Redis backend stores the entry using the following keys: cache:$cache-name:$cache-key, where cache-key is the key the application uses. So, you can find all the entries for a single cache using the Redis KEYS command: KEYS cache:$cache-name:*

The cache:$cache-name: segment can be configured using the prefix property:

# Default configuration
quarkus.cache.redis.prefix=my-cache

# Configuration for `expensiveResourceCache`
quarkus.cache.redis.expensiveResourceCache.prefix=my-expensive-cache

In these cases, you can find all the keys managed by the default cache using KEYS my-cache:*, and all the keys managed by the expensiveResourceCache cache using: KEYS my-expensive-cache:*.

Enable optimistic locking

The access to the cache can be direct or use optimistic locking. By default, optimistic locking is disabled.

You can enable optimistic locking using:

# Default configuration
quarkus.cache.redis.use-optimistic-locking=true

# Configuration for `expensiveResourceCache`
quarkus.cache.redis.expensiveResourceCache.use-optimistic-locking=true

When used, the key is watched and the SET command is executed in a transaction (MULTI/EXEC).

Related content