Redis Cache
默认情况下,Quarkus Cache 使用 Caffeine 作为后端。也可以使用 Redis。
By default, Quarkus Cache uses Caffeine as backend. It’s possible to use Redis instead. Unresolved directive in cache-redis-reference.adoc - include::{includes}/extension-status.adoc[]
Redis as cache backend
在将 Redis 用作 Quarkus 缓存的后端时,每个缓存项都将存储在 Redis 中:
When using Redis as the backend for Quarkus cache, each cached item will be stored in Redis:
-
The backend uses the <default> Redis client (if not configured otherwise), so make sure it’s configured (or use the redis dev service)
-
the Redis key is built as follows:
cache:$cache-name:$cache-key
, wherecache-key
is the key the application uses. -
the value is encoded to JSON if needed
Use the Redis backend
首先,你需要将 `quarkus-redis-cache`扩展添加到你的项目中:
First, you need to add the quarkus-redis-cache
extension to your project:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-redis-cache</artifactId>
</dependency>
implementation("io.quarkus:quarkus-redis-cache")
然后,使用 `@CacheResult`和其他缓存注解,如 Quarkus Cache guide中所述:
Then, use the @CacheResult
and others cache annotations as explained 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 Redis backend
Redis 后端使用 `<default>`Redis 客户端。请参阅 Redis reference以配置对 Redis 的访问。
The Redis backend uses the <default>
Redis client.
See the Redis reference to configure the access to Redis.
在开发模式中,你可以使用 Redis Dev Service。 |
In dev mode, you can use the Redis Dev Service. |
如果你想为你的缓存使用另一个 Redis,请按如下方式配置 client-name
:
If you want to use another Redis for your cache, configure the client-name
as follows:
quarkus.cache.redis.client-name=my-redis-for-cache
在写入 Redis 或从 Redis 读取时,Quarkus 需要知道类型。事实上,对象需要序列化和反序列化。为此,你可能需要配置要缓存的键和值类型(类名)。在构建时,Quarkus 会尝试从应用程序代码中推断类型,但可以使用以下方法覆盖该决策:
When writing to Redis or reading from Redis, Quarkus needs to know the type. Indeed, the objects need to be serialized and deserialized. For that purpose, you may need to configure type (class names) of the key and value you want to cache. At build time, Quarkus tries to deduce the types from the application code, but that decision can be overridden using:
# 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
你还可以配置缓存条目的生存时间:
You can also configure the time to live of the cached entries:
# 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`注解使值无效。
If the expire-after-write
is not configured, the entry won’t be evicted.
You would need to invalidate the values using the @CacheInvalidateAll
or @CacheInvalidate
annotations.
下表列出了受支持的属性:
The following table lists the supported properties:
Unresolved directive in cache-redis-reference.adoc - include::{generated-dir}/config/quarkus-redis-cache.adoc[]
Configure the Redis key
默认情况下,Redis 后端使用以下键存储条目:cache:$cache-name:$cache-key
,其中 cache-key`是应用程序使用的键。因此,你可以使用 Redis `KEYS`命令找到单个缓存的所有条目:`KEYS cache:$cache-name:*
By default, the Redis backend stores the entry using the following keys: cache:$cache-name:$cache-key
, where cache-key
is the key the application uses.
So, you can find all the entries for a single cache using the Redis KEYS
command: KEYS cache:$cache-name:*
`cache:$cache-name:`部分可以使用`prefix`属性进行配置:
The cache:$cache-name:
segment can be configured 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:*`管理的。
In these cases, you can find all the keys managed by the default cache using KEYS my-cache:*
, and all the keys managed by the expensiveResourceCache
cache using: KEYS my-expensive-cache:*
.
Enable optimistic locking
可以_direct_缓存的访问或者使用 optimistic locking。默认情况下,乐观锁是禁用的。
The access to the cache can be direct or use optimistic locking. By default, optimistic locking is disabled.
你可以使用以下方式启用乐观锁:
You can enable optimistic locking using:
# Default configuration
quarkus.cache.redis.use-optimistic-locking=true
# Configuration for `expensiveResourceCache`
quarkus.cache.redis.expensiveResourceCache.use-optimistic-locking=true
使用时,键为_watched_并且_SET_命令在事务中执行(MULTI/EXEC
)。
When used, the key is watched and the SET command is executed in a transaction (MULTI/EXEC
).