Skip to content

Commit fa81f5e

Browse files
Introduce dedicated objects for time to live and status changes.
This commit introduces several value objects capturing keys and their respective time to live/status change values.
1 parent b03bc2d commit fa81f5e

25 files changed

+1274
-122
lines changed

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,11 @@ public List<Long> hTtl(byte[] key, byte[]... fields) {
25962596
return this.delegate.hTtl(key, fields);
25972597
}
25982598

2599+
@Override
2600+
public List<Long> hpTtl(byte[] key, byte[]... fields) {
2601+
return this.delegate.hpTtl(key, fields);
2602+
}
2603+
25992604
@Override
26002605
public List<Long> hTtl(byte[] key, TimeUnit timeUnit, byte[]... fields) {
26012606
return this.delegate.hTtl(key, timeUnit, fields);
@@ -2636,6 +2641,11 @@ public List<Long> hTtl(String key, TimeUnit timeUnit, String... fields) {
26362641
return hTtl(serialize(key), timeUnit, serializeMulti(fields));
26372642
}
26382643

2644+
@Override
2645+
public List<Long> hpTtl(String key, String... fields) {
2646+
return hTtl(serialize(key), serializeMulti(fields));
2647+
}
2648+
26392649
@Override
26402650
public void setClientName(byte[] name) {
26412651
this.delegate.setClientName(name);

src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,13 @@ default List<Long> hTtl(byte[] key, TimeUnit timeUnit, byte[]... fields) {
15201520
return hashCommands().hTtl(key, timeUnit, fields);
15211521
}
15221522

1523+
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */
1524+
@Override
1525+
@Deprecated
1526+
default List<Long> hpTtl(byte[] key, byte[]... fields) {
1527+
return hashCommands().hpTtl(key, fields);
1528+
}
1529+
15231530
// GEO COMMANDS
15241531

15251532
/** @deprecated in favor of {@link RedisConnection#geoCommands()}}. */

src/main/java/org/springframework/data/redis/connection/RedisHashCommands.java

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18+
import java.time.Duration;
1819
import java.util.List;
1920
import java.util.Map;
2021
import java.util.Set;
@@ -253,37 +254,73 @@ public interface RedisHashCommands {
253254
Long hStrLen(byte[] key, byte[] field);
254255

255256
/**
256-
* Set time to live for given {@code field} in seconds.
257+
* Set time to live for given {@code fields} in seconds.
257258
*
258259
* @param key must not be {@literal null}.
259-
* @param seconds the amount of time after which the key will be expired in seconds, must not be {@literal null}.
260+
* @param seconds the amount of time after which the fields will be expired in seconds, must not be {@literal null}.
260261
* @param fields must not be {@literal null}.
261262
* @return a list of {@link Long} values for each of the fields provided: {@code 2} indicating the specific field is deleted
262263
* already due to expiration, or provided expiry interval is 0; {@code 1} indicating expiration time is set/updated;
263264
* {@code 0} indicating the expiration time is not set (a provided NX | XX | GT | LT condition is not met);
264265
* {@code -2} indicating there is no such field; {@literal null} when used in pipeline / transaction.
265266
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HEXPIRE</a>
266-
* @since 3.4
267+
* @since 3.5
267268
*/
268269
@Nullable
269270
List<Long> hExpire(byte[] key, long seconds, byte[]... fields);
270271

271272
/**
272-
* Set time to live for given {@code field} in milliseconds.
273+
* Set time to live for given {@code fields}.
273274
*
274275
* @param key must not be {@literal null}.
275-
* @param millis the amount of time after which the key will be expired in milliseconds, must not be {@literal null}.
276+
* @param ttl the amount of time after which the fields will be expired in {@link Duration#toSeconds() seconds} precision, must not be {@literal null}.
277+
* @param fields must not be {@literal null}.
278+
* @return a list of {@link Long} values for each of the fields provided: {@code 2} indicating the specific field is deleted
279+
* already due to expiration, or provided expiry interval is 0; {@code 1} indicating expiration time is set/updated;
280+
* {@code 0} indicating the expiration time is not set (a provided NX | XX | GT | LT condition is not met);
281+
* {@code -2} indicating there is no such field; {@literal null} when used in pipeline / transaction.
282+
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HEXPIRE</a>
283+
* @since 3.5
284+
*/
285+
@Nullable
286+
default List<Long> hExpire(byte[] key, Duration ttl, byte[]... fields) {
287+
return hExpire(key, ttl.toSeconds(), fields);
288+
}
289+
290+
/**
291+
* Set time to live for given {@code fields} in milliseconds.
292+
*
293+
* @param key must not be {@literal null}.
294+
* @param millis the amount of time after which the fields will be expired in milliseconds, must not be {@literal null}.
276295
* @param fields must not be {@literal null}.
277296
* @return a list of {@link Long} values for each of the fields provided: {@code 2} indicating the specific field is deleted
278297
* already due to expiration, or provided expiry interval is 0; {@code 1} indicating expiration time is set/updated;
279298
* {@code 0} indicating the expiration time is not set (a provided NX | XX | GT | LT condition is not met);
280299
* {@code -2} indicating there is no such field; {@literal null} when used in pipeline / transaction.
281300
* @see <a href="https://redis.io/docs/latest/commands/hpexpire/">Redis Documentation: HPEXPIRE</a>
282-
* @since 3.4
301+
* @since 3.5
283302
*/
284303
@Nullable
285304
List<Long> hpExpire(byte[] key, long millis, byte[]... fields);
286305

306+
/**
307+
* Set time to live for given {@code fields} in milliseconds.
308+
*
309+
* @param key must not be {@literal null}.
310+
* @param ttl the amount of time after which the fields will be expired in {@link Duration#toMillis() milliseconds} precision, must not be {@literal null}.
311+
* @param fields must not be {@literal null}.
312+
* @return a list of {@link Long} values for each of the fields provided: {@code 2} indicating the specific field is deleted
313+
* already due to expiration, or provided expiry interval is 0; {@code 1} indicating expiration time is set/updated;
314+
* {@code 0} indicating the expiration time is not set (a provided NX | XX | GT | LT condition is not met);
315+
* {@code -2} indicating there is no such field; {@literal null} when used in pipeline / transaction.
316+
* @see <a href="https://redis.io/docs/latest/commands/hpexpire/">Redis Documentation: HPEXPIRE</a>
317+
* @since 3.5
318+
*/
319+
@Nullable
320+
default List<Long> hpExpire(byte[] key, Duration ttl, byte[]... fields) {
321+
return hpExpire(key, ttl.toMillis(), fields);
322+
}
323+
287324
/**
288325
* Set the expiration for given {@code field} as a {@literal UNIX} timestamp.
289326
*
@@ -295,7 +332,7 @@ public interface RedisHashCommands {
295332
* set/updated; {@code 0} indicating the expiration time is not set (a provided NX | XX | GT | LT condition is not
296333
* met); {@code -2} indicating there is no such field; {@literal null} when used in pipeline / transaction.
297334
* @see <a href="https://redis.io/docs/latest/commands/hexpireat/">Redis Documentation: HEXPIREAT</a>
298-
* @since 3.4
335+
* @since 3.5
299336
*/
300337
@Nullable
301338
List<Long> hExpireAt(byte[] key, long unixTime, byte[]... fields);
@@ -311,7 +348,7 @@ public interface RedisHashCommands {
311348
* set/updated; {@code 0} indicating the expiration time is not set (a provided NX | XX | GT | LT condition is not
312349
* met); {@code -2} indicating there is no such field; {@literal null} when used in pipeline / transaction.
313350
* @see <a href="https://redis.io/docs/latest/commands/hpexpireat/">Redis Documentation: HPEXPIREAT</a>
314-
* @since 3.4
351+
* @since 3.5
315352
*/
316353
@Nullable
317354
List<Long> hpExpireAt(byte[] key, long unixTimeInMillis, byte[]... fields);
@@ -325,27 +362,27 @@ public interface RedisHashCommands {
325362
* {@code -1} field has no expiration time to be removed; {@code -2} indicating there is no such field;
326363
* {@literal null} when used in pipeline / transaction.{@literal null} when used in pipeline / transaction.
327364
* @see <a href="https://redis.io/docs/latest/commands/hpersist/">Redis Documentation: HPERSIST</a>
328-
* @since 3.4
365+
* @since 3.5
329366
*/
330367
@Nullable
331368
List<Long> hPersist(byte[] key, byte[]... fields);
332369

333370
/**
334-
* Get the time to live for {@code field} in seconds.
371+
* Get the time to live for {@code fields} in seconds.
335372
*
336373
* @param key must not be {@literal null}.
337374
* @param fields must not be {@literal null}.
338375
* @return a list of {@link Long} values for each of the fields provided: the time to live in seconds; or a negative value
339-
* to signal an error. The command returns {@code -1} if the key exists but has no associated expiration time.
340-
* The command returns {@code -2} if the key does not exist; {@literal null} when used in pipeline / transaction.
376+
* to signal an error. The command returns {@code -1} if the field exists but has no associated expiration time.
377+
* The command returns {@code -2} if the field does not exist; {@literal null} when used in pipeline / transaction.
341378
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HTTL</a>
342-
* @since 3.4
379+
* @since 3.5
343380
*/
344381
@Nullable
345382
List<Long> hTtl(byte[] key, byte[]... fields);
346383

347384
/**
348-
* Get the time to live for {@code field} in and convert it to the given {@link TimeUnit}.
385+
* Get the time to live for {@code fields} in and convert it to the given {@link TimeUnit}.
349386
*
350387
* @param key must not be {@literal null}.
351388
* @param timeUnit must not be {@literal null}.
@@ -354,8 +391,24 @@ public interface RedisHashCommands {
354391
* to signal an error. The command returns {@code -1} if the key exists but has no associated expiration time.
355392
* The command returns {@code -2} if the key does not exist; {@literal null} when used in pipeline / transaction.
356393
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HTTL</a>
357-
* @since 3.4
394+
* @since 3.5
358395
*/
359396
@Nullable
397+
// TODO: this is complete nonsense as it would jeopardize negative values
398+
// TODO: this should be a List<Map.Entry<byte, Expiration>>
360399
List<Long> hTtl(byte[] key, TimeUnit timeUnit, byte[]... fields);
400+
401+
/**
402+
* Get the time to live for {@code fields} in milliseconds.
403+
*
404+
* @param key must not be {@literal null}.
405+
* @param fields must not be {@literal null}.
406+
* @return a list of {@link Long} values for each of the fields provided: the time to live in seconds; or a negative value
407+
* to signal an error. The command returns {@code -1} if the key exists but has no associated expiration time.
408+
* The command returns {@code -2} if the key does not exist; {@literal null} when used in pipeline / transaction.
409+
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HTTL</a>
410+
* @since 3.5
411+
*/
412+
@Nullable
413+
List<Long> hpTtl(byte[] key, byte[]... fields);
361414
}

src/main/java/org/springframework/data/redis/connection/RedisKeyCommands.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.redis.connection;
1717

1818
import java.time.Duration;
19+
import java.time.Instant;
1920
import java.util.List;
2021
import java.util.Set;
2122
import java.util.concurrent.TimeUnit;
@@ -191,6 +192,20 @@ default Cursor<byte[]> scan(KeyScanOptions options) {
191192
@Nullable
192193
Boolean expire(byte[] key, long seconds);
193194

195+
/**
196+
* Set time to live for given {@code key} using {@link Duration#toSeconds() seconds} precision.
197+
*
198+
* @param key must not be {@literal null}.
199+
* @param duration
200+
* @return {@literal null} when used in pipeline / transaction.
201+
* @see <a href="https://redis.io/commands/expire">Redis Documentation: EXPIRE</a>
202+
* @since 3.5
203+
*/
204+
@Nullable
205+
default Boolean expire(byte[] key, Duration duration) {
206+
return expire(key, duration.toSeconds());
207+
}
208+
194209
/**
195210
* Set time to live for given {@code key} in milliseconds.
196211
*
@@ -202,6 +217,20 @@ default Cursor<byte[]> scan(KeyScanOptions options) {
202217
@Nullable
203218
Boolean pExpire(byte[] key, long millis);
204219

220+
/**
221+
* Set time to live for given {@code key} using {@link Duration#toMillis() milliseconds} precision.
222+
*
223+
* @param key must not be {@literal null}.
224+
* @param duration
225+
* @return {@literal null} when used in pipeline / transaction.
226+
* @see <a href="https://redis.io/commands/pexpire">Redis Documentation: PEXPIRE</a>
227+
* @since 3.5
228+
*/
229+
@Nullable
230+
default Boolean pExpire(byte[] key, Duration duration) {
231+
return pExpire(key, duration.toMillis());
232+
}
233+
205234
/**
206235
* Set the expiration for given {@code key} as a {@literal UNIX} timestamp.
207236
*
@@ -213,6 +242,21 @@ default Cursor<byte[]> scan(KeyScanOptions options) {
213242
@Nullable
214243
Boolean expireAt(byte[] key, long unixTime);
215244

245+
/**
246+
* Set the expiration for given {@code key} as a {@literal UNIX} timestamp in {@link Instant#getEpochSecond() seconds}
247+
* precision.
248+
*
249+
* @param key must not be {@literal null}.
250+
* @param unixTime
251+
* @return {@literal null} when used in pipeline / transaction.
252+
* @see <a href="https://redis.io/commands/expireat">Redis Documentation: EXPIREAT</a>
253+
* @since 3.5
254+
*/
255+
@Nullable
256+
default Boolean expireAt(byte[] key, Instant unixTime) {
257+
return expireAt(key, unixTime.getEpochSecond());
258+
}
259+
216260
/**
217261
* Set the expiration for given {@code key} as a {@literal UNIX} timestamp in milliseconds.
218262
*
@@ -224,6 +268,21 @@ default Cursor<byte[]> scan(KeyScanOptions options) {
224268
@Nullable
225269
Boolean pExpireAt(byte[] key, long unixTimeInMillis);
226270

271+
/**
272+
* Set the expiration for given {@code key} as a {@literal UNIX} timestamp in {@link Instant#toEpochMilli()
273+
* milliseconds} precision.
274+
*
275+
* @param key must not be {@literal null}.
276+
* @param unixTime
277+
* @return {@literal null} when used in pipeline / transaction.
278+
* @see <a href="https://redis.io/commands/pexpireat">Redis Documentation: PEXPIREAT</a>
279+
* @since 3.5
280+
*/
281+
@Nullable
282+
default Boolean pExpireAt(byte[] key, Instant unixTime) {
283+
return pExpireAt(key, unixTime.toEpochMilli());
284+
}
285+
227286
/**
228287
* Remove the expiration from given {@code key}.
229288
*

src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,6 +2333,7 @@ Long zRangeStoreRevByScore(String dstKey, String srcKey,
23332333
@Nullable
23342334
Long hStrLen(String key, String field);
23352335

2336+
// TODO: why why whay is this such a shitty api that there's missing all the NX, XX, GT Options
23362337
/**
23372338
* Set time to live for given {@code field} in seconds.
23382339
*
@@ -2412,21 +2413,21 @@ Long zRangeStoreRevByScore(String dstKey, String srcKey,
24122413
List<Long> hPersist(String key, String... fields);
24132414

24142415
/**
2415-
* Get the time to live for {@code field} in seconds.
2416+
* Get the time to live for {@code fields} in seconds.
24162417
*
24172418
* @param key must not be {@literal null}.
24182419
* @param fields must not be {@literal null}.
24192420
* @return a list of {@link Long} values for each of the fields provided: the time to live in milliseconds; or a negative value
24202421
* to signal an error. The command returns {@code -1} if the key exists but has no associated expiration time.
24212422
* The command returns {@code -2} if the key does not exist; {@literal null} when used in pipeline / transaction.
24222423
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HTTL</a>
2423-
* @since 3.4
2424+
* @since 3.5
24242425
*/
24252426
@Nullable
24262427
List<Long> hTtl(String key, String... fields);
24272428

24282429
/**
2429-
* Get the time to live for {@code field} in and convert it to the given {@link TimeUnit}.
2430+
* Get the time to live for {@code fields} in and convert it to the given {@link TimeUnit}.
24302431
*
24312432
* @param key must not be {@literal null}.
24322433
* @param timeUnit must not be {@literal null}.
@@ -2435,11 +2436,25 @@ Long zRangeStoreRevByScore(String dstKey, String srcKey,
24352436
* to signal an error. The command returns {@code -1} if the key exists but has no associated expiration time.
24362437
* The command returns {@code -2} if the key does not exist; {@literal null} when used in pipeline / transaction.
24372438
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HTTL</a>
2438-
* @since 3.4
2439+
* @since 3.5
24392440
*/
24402441
@Nullable
24412442
List<Long> hTtl(String key, TimeUnit timeUnit, String... fields);
24422443

2444+
/**
2445+
* Get the time to live for {@code fields} in seconds.
2446+
*
2447+
* @param key must not be {@literal null}.
2448+
* @param fields must not be {@literal null}.
2449+
* @return a list of {@link Long} values for each of the fields provided: the time to live in milliseconds; or a negative value
2450+
* to signal an error. The command returns {@code -1} if the key exists but has no associated expiration time.
2451+
* The command returns {@code -2} if the key does not exist; {@literal null} when used in pipeline / transaction.
2452+
* @see <a href="https://redis.io/docs/latest/commands/hexpire/">Redis Documentation: HTTL</a>
2453+
* @since 3.5
2454+
*/
2455+
@Nullable
2456+
List<Long> hpTtl(String key, String... fields);
2457+
24432458
// -------------------------------------------------------------------------
24442459
// Methods dealing with HyperLogLog
24452460
// -------------------------------------------------------------------------

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterHashCommands.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,18 @@ public List<Long> hTtl(byte[] key, TimeUnit timeUnit, byte[]... fields) {
375375
}
376376
}
377377

378+
@Override
379+
public List<Long> hpTtl(byte[] key, byte[]... fields) {
380+
Assert.notNull(key, "Key must not be null");
381+
Assert.notNull(fields, "Fields must not be null");
382+
383+
try {
384+
return connection.getCluster().hpttl(key, fields);
385+
} catch (Exception ex) {
386+
throw convertJedisAccessException(ex);
387+
}
388+
}
389+
378390
@Nullable
379391
@Override
380392
public Long hStrLen(byte[] key, byte[] field) {

src/main/java/org/springframework/data/redis/connection/jedis/JedisHashCommands.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ public List<Long> hTtl(byte[] key, TimeUnit timeUnit, byte[]... fields) {
288288
.toList(Converters.secondsToTimeUnit(timeUnit));
289289
}
290290

291+
@Override
292+
public List<Long> hpTtl(byte[] key, byte[]... fields) {
293+
return connection.invoke().just(Jedis::hpttl, PipelineBinaryCommands::hpttl, key, fields);
294+
}
295+
291296
@Nullable
292297
@Override
293298
public Long hStrLen(byte[] key, byte[] field) {

0 commit comments

Comments
 (0)