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:

Example 1. Expirations
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();
  }
}

使用 @TimeToLive 显式注释属性将从 Redis 读回实际的 TTLPTTL 值。-1 表示对象没有关联过期时间。

Annotating a property explicitly with @TimeToLive reads back the actual TTL or PTTL value from Redis. -1 indicates that the object has no associated expiration.

存储库实现确保通过 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.

默认情况下,在初始化应用程序时会禁用键到期侦听器。可以在 @EnableRedisRepositoriesRedisKeyValueAdapter 中调整启动模式,以便与应用程序一起启动侦听器或在首次使用 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.

延迟或禁用过期事件侦听器的启动将影响 RedisKeyExpiredEvent 发布。已禁用的事件侦听器不会发布过期事件。延迟启动可能会导致事件丢失,原因在于侦听器初始化延迟。

Delaying or disabling the expiry event listener startup impacts RedisKeyExpiredEvent publishing. A disabled event listener does not publish expiry events. A delayed startup can cause loss of events because of the delayed listener initialization.

键空间通知消息侦听器将更改 Redis 中的 notify-keyspace-events 设置,前提是未设置这些设置。不会覆盖现有的设置,因此必须正确设置这些设置(或将其留空)。请注意,在 AWS ElastiCache 上已禁用 CONFIG,启用侦听器会导致错误。要解决此问题,请将 keyspaceNotificationsConfigParameter 参数设置为一个空字符串。这会阻止 CONFIG 命令使用。

The keyspace notification message listener alters notify-keyspace-events settings in Redis, if those are not already set. Existing settings are not overridden, so you must set up those settings correctly (or leave them empty). Note that CONFIG is disabled on AWS ElastiCache, and enabling the listener leads to an error. To work around this behavior, set the keyspaceNotificationsConfigParameter parameter to an empty string. This prevents CONFIG command usage.

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.

@EnableKeyspaceEvents(shadowCopy = OFF) 禁用幻像副本的存储,并减小 Redis 中的数据大小。RedisKeyExpiredEvent 将仅包含过期密钥的 id

@EnableKeyspaceEvents(shadowCopy = OFF) disable storage of phantom copies and reduces data size within Redis. RedisKeyExpiredEvent will only contain the id of the expired key.