Skip to content

Time To Live

Objects stored in Valkey may be valid only for a certain amount of time. This is especially useful for persisting short-lived objects in Valkey without having to remove them manually when they reach their end of life. The expiration time in seconds can be set with @ValkeyHash(timeToLive=...) as well as by using io.valkey.springframework.data.core.convert.KeyspaceConfiguration$KeyspaceSettings (see Keyspaces).

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();
}
}

The repository implementation ensures subscription to Valkey keyspace notifications via io.valkey.springframework.data.listener.ValkeyMessageListenerContainer.

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 Valkey and set to expire five minutes after the original one. This is done to enable the Repository support to publish io.valkey.springframework.data.core.ValkeyKeyExpiredEvent, 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 Valkey repositories.

By default, the key expiry listener is disabled when initializing the application. The startup mode can be adjusted in @EnableValkeyRepositories or ValkeyKeyValueAdapter to start the listener with the application or upon the first insert of an entity with a TTL. See io.valkey.springframework.data.core.ValkeyKeyValueAdapter$EnableKeyspaceEvents for possible values.

The ValkeyKeyExpiredEvent holds a copy of the expired domain object as well as the key.