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はアプリケーションが使用するキーであり、cache:{cache-name}はプレフィックスです。 -
必要に応じて、値は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 |
|
This property is deprecated since The default time to live of the item stored in the cache. Environment variable: Show more |
||
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 |
|
期間フォーマットについて
期間の値を書くには、標準の 数字で始まる簡略化した書式を使うこともできます:
その他の場合は、簡略化されたフォーマットが解析のために
|
Redisキーの設定
デフォルトでは、Redisバックエンドは以下のキーパターンを使用してエントリを保存します: cache:{cache-name}:{cache-key} 。ここで、 cache-key はアプリケーションが使用するキーで、 cache:{cache-name} はプレフィックスです。
変数 {cache-name} はキャッシュアノテーションで設定された値から解決されます。そのため、Redis KEYS コマンドを使用すると、単一のキャッシュのすべてのエントリを見つけることができます: KEYS 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
# The variable "{cache-name}" is resolved from the value set in the cache annotations.
quarkus.cache.redis.prefix=my-cache-{cache-name}
この最新の例では、 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 コマンドが実行されます。