Time To Live
存储在 Redis 中的对象可能只在一定时间内有效。这对于在 Redis 中持久化短生命期对象非常有用,而无需在它们达到使用寿命时手动删除它们。到期时间(以秒为单位)可以使用 @RedisHash(timeToLive=…)
设置,也可以使用 KeyspaceSettings
设置(参见 Keyspaces)。
Objects stored in Redis may be valid only for a certain amount of time.
This is especially useful for persisting short-lived objects in Redis without having to remove them manually when they reach their end of life.
The expiration time in seconds can be set with @RedisHash(timeToLive=…)
as well as by using KeyspaceSettings
(see Keyspaces).
可以通过在数值属性或一个方法上使用 @TimeToLive
注释来设置更灵活的过期时间。但是,不要对同一个类中的方法和属性都应用 @TimeToLive
。以下示例显示了属性和方法上的 @TimeToLive
注释:
More flexible expiration times can be set by using the @TimeToLive
annotation on either a numeric property or a method.
However, do not apply @TimeToLive
on both a method and a property within the same class.
The following example shows the @TimeToLive
annotation on a property and on a method:
public class TimeToLiveOnProperty {
@Id
private String id;
@TimeToLive
private Long expiration;
}
public class TimeToLiveOnMethod {
@Id
private String id;
@TimeToLive
public long getTimeToLive() {
return new Random().nextLong();
}
}
使用 |
Annotating a property explicitly with |
存储库实现确保通过 RedisMessageListenerContainer
订阅 Redis keyspace notifications。
The repository implementation ensures subscription to Redis keyspace notifications via RedisMessageListenerContainer
.
如果将过期时间设置为正值,则会运行相应的 EXPIRE
命令。除了持久化原件之外,还会在 Redis 中持久化一个幻像副本,并将其设置为在原件过期五分钟后过期。这样做是为了让存储库支持发布 RedisKeyExpiredEvent
,在键过期时,将过期值保存在 Spring 的 ApplicationEventPublisher
中,即使原始值已删除。使用 Spring Data Redis 存储库的所有已连接应用程序都会收到到期事件。
When the expiration is set to a positive value, the corresponding EXPIRE
command is run.
In addition to persisting the original, a phantom copy is persisted in Redis and set to expire five minutes after the original one.
This is done to enable the Repository support to publish RedisKeyExpiredEvent
, holding the expired value in Spring’s ApplicationEventPublisher
whenever a key expires, even though the original values have already been removed.
Expiry events are received on all connected applications that use Spring Data Redis repositories.
默认情况下,在初始化应用程序时会禁用键到期侦听器。可以在 @EnableRedisRepositories
或 RedisKeyValueAdapter
中调整启动模式,以便与应用程序一起启动侦听器或在首次使用 TTL 插入实体时启动。有关可能的值,请参阅 link:https://docs.spring.io/spring-data/redis/docs/{version}/api/org/springframework/data/redis/core/RedisKeyValueAdapter.EnableKeyspaceEvents.html[EnableKeyspaceEvents
。
By default, the key expiry listener is disabled when initializing the application.
The startup mode can be adjusted in @EnableRedisRepositories
or RedisKeyValueAdapter
to start the listener with the application or upon the first insert of an entity with a TTL.
See EnableKeyspaceEvents
for possible values.
RedisKeyExpiredEvent
既包含过期域对象及其键的副本。
The RedisKeyExpiredEvent
holds a copy of the expired domain object as well as the key.
延迟或禁用过期事件侦听器的启动将影响 |
Delaying or disabling the expiry event listener startup impacts |
键空间通知消息侦听器将更改 Redis 中的 |
The keyspace notification message listener alters |
Redis Pub/Sub 消息不是持久的。如果应用程序宕机时密钥过期,则不会处理过期事件,这可能导致次要索引包含对过期的对象的引用。 |
Redis Pub/Sub messages are not persistent. If a key expires while the application is down, the expiry event is not processed, which may lead to secondary indexes containing references to the expired object. |
|
|