By default, Quarkus Cache uses Caffeine as backend. It’s possible to use Infinispan instead.
This technology is considered preview. In preview, backward compatibility and presence in the ecosystem is not guaranteed. Specific improvements might require changing configuration or APIs, and plans to become stable are under way. Feedback is welcome on our mailing list or as issues in our GitHub issue tracker. For a full list of possible statuses, check our FAQ entry. |
Infinispan as cache backend
When using Infinispan as the backend for Quarkus cache, each cached item will be stored in Infinispan:
-
The backend uses the <default> Infinispan client (unless configured differently), so ensure its configuration is set up accordingly (or use the Infinispan Dev Service)
-
Both the key and the value are marshalled using Protobuf with Protostream.
Use the Infinispan backend
First, add the quarkus-infinispan-cache
extension to your project:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-infinispan-cache</artifactId>
</dependency>
implementation("io.quarkus:quarkus-infinispan-cache")
Then, use the @CacheResult
and other cache annotations as detailed 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 Infinispan backend
The Infinispan backend uses the <default>
Infinispan client.
Refer to the Infinispan reference for configuring the access to Infinispan.
In dev mode, you can use the Infinispan Dev Service. |
If you want to use another Infinispan for your cache, configure the client-name
as follows:
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.
@Proto
public record ExpensiveResponse(String result) {
}
@ProtoSchema(includeClasses = { ExpensiveResponse.class })
interface Schema extends GeneratedSchema {
}
Read more about it in the 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.
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.
quarkus.cache.infinispan.my-cache.max-idle=100s
Configuration property fixed at build time - All other configuration properties are overridable at runtime
Type |
Default |
|
---|---|---|
The name of the named Infinispan client to be used for communicating with Infinispan. If not set, use the default Infinispan client. Environment variable: |
string |
|
The default lifespan of the item stored in the cache Environment variable: |
||
The default max-idle of the item stored in the cache Environment variable: |
||
Additional configuration applied to a specific Infinispan cache (highest precedence) |
Type |
Default |
The default lifespan of the item stored in the cache Environment variable: |
||
The default max-idle of the item stored in the cache Environment variable: |
About the Duration format
To write duration values, use the standard You can also use a simplified format, starting with a number:
In other cases, the simplified format is translated to the
|