Redisキャッシュ
デフォルトでは、Quarkus CacheはバックエンドとしてCaffeineを使用します。代わりにRedisを使用することも可能です。
この技術は、previewと考えられています。 preview では、下位互換性やエコシステムでの存在は保証されていません。具体的な改善には設定や API の変更が必要になるかもしれませんが、 stable になるための計画は現在進行中です。フィードバックは メーリングリスト や GitHub の課題管理 で受け付けています。 とりうるステータスの完全なリストについては、 FAQの項目 を参照してください。 |
キャッシュバックエンドとしてのRedis
QuarkusキャッシュのバックエンドとしてRedisを使用する場合、各キャッシュアイテムはRedisに保存されます:
-
バックエンドは <default> Redisクライアントを使うので(他に設定されていない場合)、設定されているようにしてください(または Redis Dev Service を使ってください)
-
the Redis key is built as follows:
cache:{cache-name}:{cache-key}
, wherecache-key
is the key the application uses andcache:{cache-name}
the prefix. -
必要に応じて、値はJSONにエンコードされます。
Redisバックエンドの使用
まず、 quarkus-redis-cache
のエクステンションをプロジェクトに追加する必要があります:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-redis-cache</artifactId>
</dependency>
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: Show more |
string |
|
The default type of the value stored in the cache. Environment variable: Show more |
string |
|
The key type, Environment variable: 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: Show more |
||
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: Show more |
||
The key prefix allowing to identify the keys belonging to the cache. If not set, the value "`cache:{cache-name`}" will be used. The variable "`{cache-name`}" is resolved from the value set in the cache annotations. Environment variable: Show more |
string |
|
Whether the access to the cache should be using optimistic locking. See Redis Optimistic Locking for details. Default is Environment variable: Show more |
ブーリアン |
|
If set, the Environment variable: Show more |
int |
|
Additional configuration applied to a specific Redis cache (highest precedence) |
タイプ |
デフォルト |
The default type of the value stored in the cache. Environment variable: Show more |
string |
|
The key type, Environment variable: 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: Show more |
||
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: Show more |
||
The key prefix allowing to identify the keys belonging to the cache. If not set, the value "`cache:{cache-name`}" will be used. The variable "`{cache-name`}" is resolved from the value set in the cache annotations. Environment variable: Show more |
string |
|
Whether the access to the cache should be using optimistic locking. See Redis Optimistic Locking for details. Default is Environment variable: Show more |
ブーリアン |
|
If set, the Environment variable: Show more |
int |
期間フォーマットについて
To write duration values, use the standard 数字で始まる簡略化した書式を使うこともできます:
その他の場合は、簡略化されたフォーマットが解析のために
|
Redisキーの設定
By default, the Redis backend stores the entry using the following keys pattern: cache:{cache-name}:{cache-key}
, where cache-key
is the key the application uses and cache:{cache-name}
the prefix. The variable {cache-name}
is resolved from the value set in the cache annotations.
So, you can find all the entries for a single cache using the Redis KEYS
command: KEYS cache:{cache-name}:*
The prefix can be configured by using the prefix
property:
# 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 # The variable "{cache-name}" is resolved from the value set in the cache annotations. quarkus.cache.redis.prefix=my-cache-{cache-name}
In this latest example, you can find all the keys managed by the default cache using KEYS my-cache-{cache-name}:*
.
楽観的ロックの有効化
キャッシュへのアクセスは、 直接アクセス することも、 楽観的ロック を使用することも可能です。デフォルトでは、楽観的なロックは無効です。
以下を使用して、楽観的なロックを有効にすることができます:
# Default configuration
quarkus.cache.redis.use-optimistic-locking=true
# Configuration for `expensiveResourceCache`
quarkus.cache.redis.expensiveResourceCache.use-optimistic-locking=true
使用すると、キーが ウォッチ され、トランザクション( MULTI/EXEC
)で SET コマンドが実行されます。