-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31865 from cescoffier/redis-cache
Implement a backend for Quarkus Cache using Redis
- Loading branch information
Showing
54 changed files
with
3,082 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
//// | ||
This guide is maintained in the main Quarkus repository | ||
and pull requests should be submitted there: | ||
https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc | ||
//// | ||
= Redis Cache | ||
:extension-status: preview | ||
include::_attributes.adoc[] | ||
:categories: data | ||
:summary: Use Redis as the Quarkus cache backend | ||
|
||
By default, Quarkus Cache uses Caffeine as backend. | ||
It's possible to use Redis instead. | ||
|
||
include::{includes}/extension-status.adoc[] | ||
|
||
== 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 xref:redis-dev-services.adoc[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: | ||
|
||
[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"] | ||
.pom.xml | ||
---- | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-redis-cache</artifactId> | ||
</dependency> | ||
---- | ||
|
||
[source,gradle,role="secondary asciidoc-tabs-target-sync-gradle"] | ||
.build.gradle | ||
---- | ||
implementation("io.quarkus:quarkus-redis-cache") | ||
---- | ||
|
||
Then, use the `@CacheResult` and others cache annotations as explained in the xref:cache.adoc[Quarkus Cache guide]: | ||
|
||
[source, java] | ||
---- | ||
@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() { | ||
} | ||
---- | ||
|
||
[[redis-cache-configuration-reference]] | ||
== Configure the Redis backend | ||
|
||
The Redis backend uses the `<default>` Redis client. | ||
See the xref:redis-reference.adoc[Redis reference] to configure the access to Redis. | ||
|
||
TIP: In dev mode, you can use the xref:redis-dev-services.adoc[Redis Dev Service]. | ||
|
||
If you want to use another Redis for your cache, configure the `client-name` as follows: | ||
|
||
[source, properties] | ||
---- | ||
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: | ||
|
||
[source, properties] | ||
---- | ||
# 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: | ||
|
||
[source, properties] | ||
---- | ||
# Default configuration | ||
quarkus.cache.redis.ttl=10s | ||
# Configuration for `expensiveResourceCache` | ||
quarkus.cache.redis.expensiveResourceCache.ttl=1h | ||
---- | ||
|
||
If the `ttl` 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: | ||
|
||
include::{generated-dir}/config/quarkus-cache-redis.adoc[opts=optional, leveloffset=+1] | ||
|
||
== 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: | ||
|
||
|
||
[source, properties] | ||
---- | ||
# 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 https://redis.io/docs/manual/transactions/#optimistic-locking-using-check-and-set[optimistic locking]. | ||
By default, optimistic locking is disabled. | ||
|
||
You can enable optimistic locking using: | ||
[source, properties] | ||
---- | ||
# 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`). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ployment-spi/src/main/java/io/quarkus/cache/deployment/spi/CacheManagerInfoBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.cache.deployment.spi; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
import io.quarkus.cache.CacheManagerInfo; | ||
|
||
/** | ||
* A build item that makes sure a {@link CacheManagerInfo} is available at runtime for consideration as the cache backend | ||
*/ | ||
public final class CacheManagerInfoBuildItem extends MultiBuildItem { | ||
|
||
private final CacheManagerInfo info; | ||
|
||
public CacheManagerInfoBuildItem(CacheManagerInfo info) { | ||
this.info = info; | ||
} | ||
|
||
public CacheManagerInfo get() { | ||
return info; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-cache-parent</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>quarkus-cache-runtime-spi</artifactId> | ||
<name>Quarkus - Cache - Runtime SPI</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.smallrye.reactive</groupId> | ||
<artifactId>mutiny</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-extension-processor</artifactId> | ||
<version>${project.version}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
Oops, something went wrong.