From 71e98415a4eba0c2a7b768137dc752f114854fe7 Mon Sep 17 00:00:00 2001 From: dengliming Date: Tue, 9 Mar 2021 21:09:16 +0800 Subject: [PATCH 1/2] Add support PXAT/EXAT arguments to SET command --- .../redis/clients/jedis/params/SetParams.java | 32 ++++++++++++++++++- .../commands/BinaryValuesCommandsTest.java | 18 ++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/redis/clients/jedis/params/SetParams.java b/src/main/java/redis/clients/jedis/params/SetParams.java index e7ebdb1adb..4fb9b316d9 100644 --- a/src/main/java/redis/clients/jedis/params/SetParams.java +++ b/src/main/java/redis/clients/jedis/params/SetParams.java @@ -12,6 +12,8 @@ public class SetParams extends Params { private static final String NX = "nx"; private static final String PX = "px"; private static final String EX = "ex"; + private static final String EXAT = "exat"; + private static final String PXAT = "pxat"; private static final String KEEPTTL = "keepttl"; private static final String GET = "get"; @@ -71,6 +73,26 @@ public SetParams xx() { return this; } + /** + * Set the specified Unix time at which the key will expire, in seconds. + * @param seconds + * @return SetParams + */ + public SetParams exat(long seconds) { + addParam(EXAT, seconds); + return this; + } + + /** + * Set the specified Unix time at which the key will expire, in milliseconds. + * @param milliseconds + * @return SetParams + */ + public SetParams pxat(long milliseconds) { + addParam(PXAT, milliseconds); + return this; + } + /** * Retain the time to live associated with the key. * @return SetParams @@ -79,7 +101,7 @@ public SetParams keepttl() { addParam(KEEPTTL); return this; } - + /** * Return the old value stored at key, or nil when key did not exist. * @return SetParams @@ -108,6 +130,14 @@ public byte[][] getByteParams(byte[]... args) { byteParams.add(SafeEncoder.encode(PX)); byteParams.add(Protocol.toByteArray((long) getParam(PX))); } + if (contains(EXAT)) { + byteParams.add(SafeEncoder.encode(EXAT)); + byteParams.add(Protocol.toByteArray((long) getParam(EXAT))); + } + if (contains(PXAT)) { + byteParams.add(SafeEncoder.encode(PXAT)); + byteParams.add(Protocol.toByteArray((long) getParam(PXAT))); + } if (contains(KEEPTTL)) { byteParams.add(SafeEncoder.encode(KEEPTTL)); } diff --git a/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java index 2f03ca1719..e67a5618b7 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java @@ -131,6 +131,22 @@ public void setAndKeepttl() { assertTrue(ttl < 0); } + @Test + public void setAndPxat() { + String status = jedis.set(bfoo, binaryValue, setParams().nx().pxat(System.currentTimeMillis() + expireMillis)); + assertTrue(Keyword.OK.name().equalsIgnoreCase(status)); + long ttl = jedis.ttl(bfoo); + assertTrue(ttl > 0 && ttl <= expireSeconds); + } + + @Test + public void setAndExat() { + String status = jedis.set(bfoo, binaryValue, setParams().nx().exat(System.currentTimeMillis() / 1000 + expireSeconds)); + assertTrue(Keyword.OK.name().equalsIgnoreCase(status)); + long ttl = jedis.ttl(bfoo); + assertTrue(ttl > 0 && ttl <= expireSeconds); + } + @Test public void getSet() { byte[] value = jedis.getSet(bfoo, binaryValue); @@ -338,4 +354,4 @@ public void sendBlockingCommandTest() { assertNull(jedis.sendBlockingCommand(BLPOP, bfoo, Protocol.toByteArray(1L))); } -} \ No newline at end of file +} From b927d1c168d8465f1109b76a8043fcbcb905fa75 Mon Sep 17 00:00:00 2001 From: dengliming Date: Tue, 9 Mar 2021 21:27:29 +0800 Subject: [PATCH 2/2] Fix review --- src/main/java/redis/clients/jedis/params/SetParams.java | 4 ++-- .../jedis/tests/commands/BinaryValuesCommandsTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/redis/clients/jedis/params/SetParams.java b/src/main/java/redis/clients/jedis/params/SetParams.java index 4fb9b316d9..4b4aab1408 100644 --- a/src/main/java/redis/clients/jedis/params/SetParams.java +++ b/src/main/java/redis/clients/jedis/params/SetParams.java @@ -78,7 +78,7 @@ public SetParams xx() { * @param seconds * @return SetParams */ - public SetParams exat(long seconds) { + public SetParams exAt(long seconds) { addParam(EXAT, seconds); return this; } @@ -88,7 +88,7 @@ public SetParams exat(long seconds) { * @param milliseconds * @return SetParams */ - public SetParams pxat(long milliseconds) { + public SetParams pxAt(long milliseconds) { addParam(PXAT, milliseconds); return this; } diff --git a/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java index e67a5618b7..734788635e 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/BinaryValuesCommandsTest.java @@ -133,7 +133,7 @@ public void setAndKeepttl() { @Test public void setAndPxat() { - String status = jedis.set(bfoo, binaryValue, setParams().nx().pxat(System.currentTimeMillis() + expireMillis)); + String status = jedis.set(bfoo, binaryValue, setParams().nx().pxAt(System.currentTimeMillis() + expireMillis)); assertTrue(Keyword.OK.name().equalsIgnoreCase(status)); long ttl = jedis.ttl(bfoo); assertTrue(ttl > 0 && ttl <= expireSeconds); @@ -141,7 +141,7 @@ public void setAndPxat() { @Test public void setAndExat() { - String status = jedis.set(bfoo, binaryValue, setParams().nx().exat(System.currentTimeMillis() / 1000 + expireSeconds)); + String status = jedis.set(bfoo, binaryValue, setParams().nx().exAt(System.currentTimeMillis() / 1000 + expireSeconds)); assertTrue(Keyword.OK.name().equalsIgnoreCase(status)); long ttl = jedis.ttl(bfoo); assertTrue(ttl > 0 && ttl <= expireSeconds);