From 60be40c79372a2ccc28aa7b18888556098f401f5 Mon Sep 17 00:00:00 2001 From: dengliming Date: Thu, 4 Mar 2021 16:34:24 +0800 Subject: [PATCH] Add support for RESTORE ABSTTL #1634 Original pull request: #1634. --- .../io/lettuce/core/RedisCommandBuilder.java | 4 +++ .../java/io/lettuce/core/RestoreArgs.java | 25 +++++++++++++++++++ .../lettuce/core/protocol/CommandKeyword.java | 2 +- .../commands/KeyCommandIntegrationTests.java | 8 ++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/lettuce/core/RedisCommandBuilder.java b/src/main/java/io/lettuce/core/RedisCommandBuilder.java index ef360da897..9f7e124a78 100644 --- a/src/main/java/io/lettuce/core/RedisCommandBuilder.java +++ b/src/main/java/io/lettuce/core/RedisCommandBuilder.java @@ -1800,6 +1800,10 @@ Command restore(K key, byte[] value, RestoreArgs restoreArgs) { args.add(REPLACE); } + if (restoreArgs.absttl) { + args.add(ABSTTL); + } + return createCommand(RESTORE, new StatusOutput<>(codec), args); } diff --git a/src/main/java/io/lettuce/core/RestoreArgs.java b/src/main/java/io/lettuce/core/RestoreArgs.java index a7ae4fba02..fd845bd734 100644 --- a/src/main/java/io/lettuce/core/RestoreArgs.java +++ b/src/main/java/io/lettuce/core/RestoreArgs.java @@ -26,6 +26,7 @@ * {@link RestoreArgs} is a mutable object and instances should be used only once to avoid shared mutable state. * * @author Mark Paluch + * @author dengliming * @since 5.1 */ public class RestoreArgs { @@ -34,6 +35,8 @@ public class RestoreArgs { boolean replace; + boolean absttl; + /** * Builder entry points for {@link XAddArgs}. */ @@ -116,4 +119,26 @@ public RestoreArgs replace(boolean replace) { return this; } + /** + * TTL will represent an absolute Unix timestamp (in milliseconds) in which the key will expire. + * + * @return {@code this}. + * @since 6.1 + */ + public RestoreArgs absttl() { + return absttl(true); + } + + /** + * TTL will represent an absolute Unix timestamp (in milliseconds) in which the key will expire. + * + * @param absttl + * @return {@code this}. + * @since 6.1 + */ + public RestoreArgs absttl(boolean absttl) { + + this.absttl = absttl; + return this; + } } diff --git a/src/main/java/io/lettuce/core/protocol/CommandKeyword.java b/src/main/java/io/lettuce/core/protocol/CommandKeyword.java index 7c0fc9ff3e..776ea21b79 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandKeyword.java +++ b/src/main/java/io/lettuce/core/protocol/CommandKeyword.java @@ -27,7 +27,7 @@ */ public enum CommandKeyword implements ProtocolKeyword { - ADDR, ADDSLOTS, AFTER, AGGREGATE, ALLCHANNELS, ALLCOMMANDS, ALLKEYS, ALPHA, AND, ASK, ASC, ASYNC, BEFORE, BLOCK, BUMPEPOCH, + ABSTTL, ADDR, ADDSLOTS, AFTER, AGGREGATE, ALLCHANNELS, ALLCOMMANDS, ALLKEYS, ALPHA, AND, ASK, ASC, ASYNC, BEFORE, BLOCK, BUMPEPOCH, BY, BYLEX, BYSCORE, CACHING, CAT, CHANNELS, COPY, COUNT, COUNTKEYSINSLOT, CONSUMERS, CREATE, DB, DELSLOTS, DELUSER, DESC, SOFT, HARD, ENCODING, diff --git a/src/test/java/io/lettuce/core/commands/KeyCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/KeyCommandIntegrationTests.java index dd694184be..12cc5bf2e7 100644 --- a/src/test/java/io/lettuce/core/commands/KeyCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/KeyCommandIntegrationTests.java @@ -49,6 +49,7 @@ /** * @author Will Glozer * @author Mark Paluch + * @author dengliming */ @ExtendWith(LettuceExtension.class) @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -328,6 +329,12 @@ void restoreReplace() { assertThat(redis.restore(key, bytes, RestoreArgs.Builder.ttl(Duration.ofSeconds(1)).replace())).isEqualTo("OK"); assertThat(redis.get(key)).isEqualTo(value); assertThat(redis.pttl(key)).isGreaterThan(0).isLessThanOrEqualTo(1000); + + redis.del(key); + assertThat(redis.restore(key, bytes, RestoreArgs.Builder.ttl(System.currentTimeMillis() + 3000).replace().absttl())) + .isEqualTo("OK"); + assertThat(redis.get(key)).isEqualTo(value); + assertThat(redis.pttl(key)).isGreaterThan(0).isLessThanOrEqualTo(3000); } @Test @@ -516,4 +523,5 @@ void setup100KeyValues(Set expect) { expect.add(key + i); } } + }