-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
* Deprecates custom annotations * Implements the cache extension SPI
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
//// | ||
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 | ||
//// | ||
= Infinispan Cache | ||
Check warning on line 6 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
|
||
:extension-status: preview | ||
include::_attributes.adoc[] | ||
:categories: data | ||
:summary: Use Infinispan as the Quarkus cache backend | ||
:topics: infinispan,cache,data | ||
:extensions: io.quarkus:quarkus-infinispan-cache,io.quarkus:quarkus-infinispan-client | ||
|
||
By default, Quarkus Cache uses Caffeine as backend. | ||
Check warning on line 14 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
|
||
It's possible to use Infinispan instead. | ||
|
||
include::{includes}/extension-status.adoc[] | ||
|
||
== Infinispan as cache backend | ||
Check warning on line 19 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
|
||
|
||
When using Infinispan as the backend for Quarkus cache, each cached item will be stored in Infinispan: | ||
Check warning on line 21 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
Check warning on line 21 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
|
||
|
||
- The backend uses the _<default>_ Infinispan client (unless configured differently), so ensure its configuration is | ||
set up accordingly (or use the xref:infinispan-dev-services.adoc[Infinispan Dev Service]) | ||
- Both the key and the value are marshalled using Protobuf with Protostream. | ||
Check warning on line 25 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
Check warning on line 25 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
Check warning on line 25 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
|
||
|
||
== Use the Infinispan backend | ||
|
||
First, add the `quarkus-infinispan-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-infinispan-cache</artifactId> | ||
</dependency> | ||
---- | ||
|
||
[source,gradle,role="secondary asciidoc-tabs-target-sync-gradle"] | ||
.build.gradle | ||
Check warning on line 41 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
|
||
---- | ||
implementation("io.quarkus:quarkus-infinispan-cache") | ||
---- | ||
|
||
Then, use the `@CacheResult` and other cache annotations as detailed in the xref:cache.adoc[Quarkus Cache guide]: | ||
Check warning on line 46 in docs/src/main/asciidoc/cache-infinispan-reference.adoc GitHub Actions / Linting with Vale
|
||
|
||
[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() { | ||
} | ||
---- | ||
|
||
[[infinispan-cache-configuration-reference]] | ||
== Configure the Infinispan backend | ||
|
||
The Infinispan backend uses the `<default>` Infinispan client. | ||
Refer to the xref:infinispan-client-reference.adoc[Infinispan reference] for configuring the access to Infinispan. | ||
|
||
TIP: In dev mode, you can use the xref:infinispan-dev-services.adoc[Infinispan Dev Service]. | ||
|
||
If you want to use another Infinispan for your cache, configure the `client-name` as follows: | ||
|
||
[source, properties] | ||
---- | ||
quarkus.cache.infinispan.client-name=another | ||
---- | ||
|
||
== Marshalling | ||
|
||
When interacting with Infinispan in Quarkus, you can easily marshal and unmarshal | ||
Java simple types when writing to or reading from the cache. However, when dealing | ||
with Plain Old Java Objects (POJOs), users of Infinispan need to provide the marshalling | ||
schema. | ||
|
||
[source, java] | ||
---- | ||
@Proto | ||
public record ExpensiveResponse(String result) { | ||
} | ||
@ProtoSchema(includeClasses = { ExpensiveResponse.class }) | ||
interface Schema extends GeneratedSchema { | ||
} | ||
---- | ||
|
||
Read more about it in the xref:infinispan-client-reference.adoc[Infinispan reference] in the Annotation | ||
based serialization section. | ||
|
||
== Expiration | ||
|
||
You have the option to configure two properties for data expiration: *lifespan* and *max-idle*. | ||
|
||
=== Lifespan | ||
|
||
In Infinispan, *lifespan* refers to a configuration parameter that determines the maximum time an | ||
entry (or an object) can remain in the cache since it was created or last accessed before it is | ||
considered expired and removed from the cache. | ||
|
||
When you configure the *lifespan* parameter for entries in an Infinispan cache, | ||
you specify a time duration. After an entry has been added to the cache or accessed | ||
(read or written), it starts its lifespan countdown. If the time since the entry | ||
was created or last accessed exceeds the specified "lifespan" duration, the entry | ||
is considered expired and becomes eligible for eviction from the cache. | ||
|
||
[source, properties] | ||
---- | ||
quarkus.cache.infinispan.my-cache.lifespan=10s | ||
---- | ||
|
||
=== Max Idle | ||
When you configure the *max-idle* parameter for entries in an Infinispan cache, you specify a time | ||
duration. After an entry has been accessed (read or written) in the cache, if there are no subsequent | ||
accesses to that entry within the specified duration, it is considered idle. Once the idle time | ||
exceeds the *max-idle* duration, the entry is considered expired and eligible for eviction from | ||
the cache. | ||
|
||
[source, properties] | ||
---- | ||
quarkus.cache.infinispan.my-cache.max-idle=100s | ||
---- | ||
|
||
include::{generated-dir}/config/quarkus-cache-infinispan.adoc[opts=optional, leveloffset=+1] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>quarkus-infinispan-cache-parent</artifactId> | ||
<groupId>io.quarkus</groupId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>quarkus-infinispan-cache-deployment</artifactId> | ||
<name>Quarkus - Infinispan - Cache - Deployment</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-infinispan-client-deployment</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-cache-deployment</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-infinispan-cache</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5-internal</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.rest-assured</groupId> | ||
<artifactId>rest-assured</artifactId> | ||
<scope>test</scope> | ||
</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> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<profiles> | ||
<profile> | ||
<id>test-infinispan</id> | ||
<activation> | ||
<property> | ||
<name>test-containers</name> | ||
</property> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<skip>false</skip> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<configuration> | ||
<skip>false</skip> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
</project> |