# Infinispan Cache

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:

pom.xml

    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-infinispan-cache</artifactId>
    </dependency>

build.gradle

    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 serialize and deserialize Java primitive types when storing or retrieving data from the cache. No additional marshalling configuration is required for Infinispan.

    @CacheResult(cacheName = "weather-cache") (1)
    public String getDailyForecast(String dayOfWeek, int dayOfMonth, String city) { (2)
        return dayOfWeek + " will be " + getDailyResult(dayOfMonth % 4) + " in " + city;
    }

1 | Ask this method execution to be cached in the 'weather-cache'
2 | The key combines `String` dayOfWeek, `int` dayOfMonth and `String` city. The associated value is of type `String`.

Quarkus uses Protobuf for data serialization in Infinispan by default. Infinispan recommends using Protobuf as the preferred way to structure data. Therefore, when working with Plain Old Java Objects (POJOs), users need to supply the schema for marshalling in Infinispan.

### Marshalling Java types

Let’s say we want a composite Key using `java.time.LocalDate`.

    @CacheResult(cacheName = "weather-cache") (1)
    public String getDailyForecast(LocalDate date, String city) { (2)
        return date.getDayOfWeek() + " will be " + getDailyResult(date.getDayOfMonth() % 4) + " in " + city;
    }

1 | Request that the response of this method execution be cached in 'weather-cache'
2 | The key combines a `java.util.LocalDate` date and a `String` city. The associated value is of type 'String'.

Since Infinispan serializes data by default using Protobuf in Quarkus, executing the code will result in the following error:

    java.lang.IllegalArgumentException:
    No marshaller registered for object of Java type java.time.LocalDate

Protobuf does not inherently know how to marshal `java.time.LocalDate`. Fortunately, Protostream provides a straightforward solution to this problem using `@ProtoAdapter` and `@ProtoSchema`.

    @ProtoAdapter(LocalDate.class)
    public class LocalDateAdapter {
        @ProtoFactory
        LocalDate create(String localDate) {
            return LocalDate.parse(localDate);
        }

        @ProtoField(1)
        String getLocalDate(LocalDate localDate) {
            return localDate.toString();
        }
    }

    @ProtoSchema(includeClasses = LocalDateAdapter.class, schemaPackageName = "quarkus")
    public interface Schema extends GeneratedSchema {
    }

### Marshalling your POJOs

Just like with Java types, when using your POJOs as keys or values, you can follow this approach:

    @CacheResult(cacheName = "my-cache") (1)
    public ExpensiveResponse requestApi(String id) { (2)
        // very expensive call

        return new ExpensiveResponse(...);
    }

1 | Request that the response of this method execution be cached in 'my-cache'
2 | The key is a `String`. The associated value is of type `org.acme.ExpensiveResponse`.

In this case, you’ll need to define the schema for your Java type using `@Proto` and `@ProtoSchema`. This schema can encompass all types and adapters used in your Quarkus application.

    @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

Configuration property | Type | Default
--- | --- | ---
`quarkus.cache.infinispan.client-name` The name of the named Infinispan client to be used for communicating with Infinispan. If not set, use the default Infinispan client. Environment variable: `QUARKUS_CACHE_INFINISPAN_CLIENT_NAME` | string |
`quarkus.cache.infinispan.lifespan` The default lifespan of the item stored in the cache Environment variable: `QUARKUS_CACHE_INFINISPAN_LIFESPAN` | Duration |
`quarkus.cache.infinispan.max-idle` The default max-idle of the item stored in the cache Environment variable: `QUARKUS_CACHE_INFINISPAN_MAX_IDLE` | Duration |
Additional configuration applied to a specific Infinispan cache (highest precedence) | Type | Default
`quarkus.cache.infinispan."cache-name".lifespan` The default lifespan of the item stored in the cache Environment variable: `QUARKUS_CACHE_INFINISPAN__CACHE_NAME__LIFESPAN` | Duration |
`quarkus.cache.infinispan."cache-name".max-idle` The default max-idle of the item stored in the cache Environment variable: `QUARKUS_CACHE_INFINISPAN__CACHE_NAME__MAX_IDLE` | Duration |

| 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`.