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

Redisキャッシュ

デフォルトでは、Quarkus CacheはバックエンドとしてCaffeineを使用します。代わりにRedisを使用することも可能です。

この技術は、previewと考えられています。

preview では、下位互換性やエコシステムでの存在は保証されていません。具体的な改善には設定や API の変更が必要になるかもしれませんが、 stable になるための計画は現在進行中です。フィードバックは メーリングリストGitHub の課題管理 で受け付けています。

とりうるステータスの完全なリストについては、 FAQの項目 を参照してください。

キャッシュバックエンドとしてのRedis

QuarkusキャッシュのバックエンドとしてRedisを使用する場合、各キャッシュアイテムはRedisに保存されます:

  • バックエンドは <default> Redisクライアントを使用します(他に設定されていない場合)。そのため、設定されていることを確認してください (あるいは redis dev service を使用してください)

  • Redisキーは次のように構築されます: cache:$cache-name:$cache-key 。ここで、 cache-key はアプリケーションが使用するキーです。

  • 必要に応じて、値はJSONにエンコードされます。

Redisバックエンドの使用

まず、 quarkus-redis-cache のエクステンションをプロジェクトに追加する必要があります:

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

その後、 Quarkus Cacheガイド で説明されているように、 @CacheResult や、その他のキャッシュアノテーションを使用します:

@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のバックエンドの設定

Redisバックエンドは、 <default> Redisクライアントを使用します。Redisへのアクセスを設定するには、 Redisリファレンス を参照してください。

開発モードでは、 Redis Dev Service を使用することができます。

キャッシュに別のRedisを使用する場合は、 client-name を以下のように設定してください:

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

Redisに書き込んだり、Redisから読み込んだりするとき、Quarkusは型を知っておく必要があります。実際、オブジェクトをシリアライズしたりデシリアライズしたりする必要があります。そのため、キャッシュしたいキーと値の型(クラス名)を設定する必要がある場合があります。ビルド時に、Quarkusはアプリケーションコードから型を推測しようとしますが、この決定は、以下の方法でオーバーライドできます:

# 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

また、キャッシュされたエントリーの生存時間(TTL) を設定することも可能です:

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

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

expire-after-write が構成されていない場合、エントリは退避されません。 @CacheInvalidateAll または @CacheInvalidate アノテーションを使用して値を無効にする必要があります。

次の表は、サポートされるプロパティの一覧です:

ビルド時に固定される構成プロパティ - 他のすべての構成プロパティは実行時にオーバーライド可能

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

ブーリアン

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

ブーリアン

期間フォーマットについて

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

数字で始まる簡略化した書式を使うこともできます:

  • 数値のみの場合は、秒単位の時間を表します。

  • 数値の後に ms が続く場合は、ミリ秒単位の時間を表します。

その他の場合は、簡略化されたフォーマットが解析のために java.time.Duration フォーマットに変換されます:

  • 数値の後に hms が続く場合は、その前に PT が付けられます。

  • 数値の後に d が続く場合は、その前に P が付けられます。

Redisキーの設定

デフォルトでは、Redisバックエンドは以下のキーを使用してエントリーを保存します: cache:$cache-name:$cache-key 。ここで、 cache-key はアプリケーションが使用するキーです。そのため、Redis KEYS コマンドを使用すると、1 つのキャッシュのすべてのエントリーを見つけることができます: KEYS cache:$cache-name:*

cache:$cache-name: セグメントは、 prefix プロパティを使用して設定することができます:

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

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

このような場合、デフォルトキャッシュで管理されているすべての鍵は KEYS my-cache:* で、 expensiveResourceCache キャッシュで管理されているすべての鍵は KEYS my-expensive-cache:* で検索できます。

楽観的ロックの有効化

キャッシュへのアクセスは、 直接アクセス することも、 楽観的ロック を使用することも可能です。デフォルトでは、楽観的なロックは無効です。

以下を使用して、楽観的なロックを有効にすることができます:

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

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

使用すると、キーが ウォッチ され、トランザクション( MULTI/EXEC )で SET コマンドが実行されます。

関連コンテンツ