Skip to content

Commit

Permalink
Add support for PXAT/EXAT arguments to SET command #1607
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Feb 3, 2021
1 parent 638adff commit 8a31910
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 5 deletions.
179 changes: 174 additions & 5 deletions src/main/java/io/lettuce/core/SetArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package io.lettuce.core;

import java.time.Instant;
import java.util.Date;

import io.lettuce.core.internal.LettuceAssert;
import io.lettuce.core.protocol.CommandArgs;

/**
Expand All @@ -31,8 +35,12 @@ public class SetArgs implements CompositeArgument {

private Long ex;

private Long exAt;

private Long px;

private Long pxAt;

private boolean nx = false;

private boolean xx = false;
Expand All @@ -51,7 +59,7 @@ private Builder() {
}

/**
* Creates new {@link SetArgs} and enabling {@literal EX}.
* Creates new {@link SetArgs} and enable {@literal EX}.
*
* @param timeout expire time in seconds.
* @return new {@link SetArgs} with {@literal EX} enabled.
Expand All @@ -62,7 +70,42 @@ public static SetArgs ex(long timeout) {
}

/**
* Creates new {@link SetArgs} and enabling {@literal PX}.
* Creates new {@link SetArgs} and enable {@literal EXAT}.
*
* @param timestamp the timestamp type: posix time in seconds.
* @return new {@link SetArgs} with {@literal EXAT} enabled.
* @see SetArgs#exAt(long)
*/
public static SetArgs exAt(long timestamp) {
return new SetArgs().exAt(timestamp);
}

/**
* Creates new {@link SetArgs} and enable {@literal EXAT}.
*
* @param timestamp the timestamp type: posix time in seconds.
* @return new {@link SetArgs} with {@literal EXAT} enabled.
* @see SetArgs#exAt(Date)
* @since 6.1
*/
public static SetArgs exAt(Date timestamp) {
return new SetArgs().exAt(timestamp);
}

/**
* Creates new {@link SetArgs} and enable {@literal EXAT}.
*
* @param timestamp the timestamp type: posix time in seconds.
* @return new {@link SetArgs} with {@literal EXAT} enabled.
* @see SetArgs#exAt(Instant)
* @since 6.1
*/
public static SetArgs exAt(Instant timestamp) {
return new SetArgs().exAt(timestamp);
}

/**
* Creates new {@link SetArgs} and enable {@literal PX}.
*
* @param timeout expire time in milliseconds.
* @return new {@link SetArgs} with {@literal PX} enabled.
Expand All @@ -73,7 +116,42 @@ public static SetArgs px(long timeout) {
}

/**
* Creates new {@link SetArgs} and enabling {@literal NX}.
* Creates new {@link SetArgs} and enable {@literal PXAT}.
*
* @param timestamp the timestamp type: posix time.
* @return new {@link SetArgs} with {@literal PXAT} enabled.
* @see SetArgs#pxAt(long)
*/
public static SetArgs pxAt(long timestamp) {
return new SetArgs().pxAt(timestamp);
}

/**
* Creates new {@link SetArgs} and enable {@literal PXAT}.
*
* @param timestamp the timestamp type: posix time.
* @return new {@link SetArgs} with {@literal PXAT} enabled.
* @see SetArgs#pxAt(Date)
* @since 6.1
*/
public static SetArgs pxAt(Date timestamp) {
return new SetArgs().pxAt(timestamp);
}

/**
* Creates new {@link SetArgs} and enable {@literal PXAT}.
*
* @param timestamp the timestamp type: posix time.
* @return new {@link SetArgs} with {@literal PXAT} enabled.
* @see SetArgs#pxAt(Instant)
* @since 6.1
*/
public static SetArgs pxAt(Instant timestamp) {
return new SetArgs().pxAt(timestamp);
}

/**
* Creates new {@link SetArgs} and enable {@literal NX}.
*
* @return new {@link SetArgs} with {@literal NX} enabled.
* @see SetArgs#nx()
Expand All @@ -83,7 +161,7 @@ public static SetArgs nx() {
}

/**
* Creates new {@link SetArgs} and enabling {@literal XX}.
* Creates new {@link SetArgs} and enable {@literal XX}.
*
* @return new {@link SetArgs} with {@literal XX} enabled.
* @see SetArgs#xx()
Expand All @@ -93,7 +171,7 @@ public static SetArgs xx() {
}

/**
* Creates new {@link SetArgs} and enabling {@literal KEEPTTL}.
* Creates new {@link SetArgs} and enable {@literal KEEPTTL}.
*
* @return new {@link SetArgs} with {@literal KEEPTTL} enabled.
* @see SetArgs#keepttl()
Expand All @@ -117,6 +195,47 @@ public SetArgs ex(long timeout) {
return this;
}

/**
* Set the specified expire at time using a posix {@code timestamp}.
*
* @param timestamp the timestamp type: posix time in seconds.
* @return {@code this} {@link SetArgs}.
* @since 6.1
*/
public SetArgs exAt(long timestamp) {

this.exAt = timestamp;
return this;
}

/**
* Set the specified expire at time using a posix {@code timestamp}.
*
* @param timestamp the timestamp type: posix time in seconds.
* @return {@code this} {@link SetArgs}.
* @since 6.1
*/
public SetArgs exAt(Date timestamp) {

LettuceAssert.notNull(timestamp, "Timestamp must not be null");

return exAt(timestamp.getTime() / 1000);
}

/**
* Set the specified expire at time using a posix {@code timestamp}.
*
* @param timestamp the timestamp type: posix time in seconds.
* @return {@code this} {@link SetArgs}.
* @since 6.1
*/
public SetArgs exAt(Instant timestamp) {

LettuceAssert.notNull(timestamp, "Timestamp must not be null");

return exAt(timestamp.toEpochMilli() / 1000);
}

/**
* Set the specified expire time, in milliseconds.
*
Expand All @@ -129,6 +248,47 @@ public SetArgs px(long timeout) {
return this;
}

/**
* Set the specified expire at time using a posix {@code timestamp}.
*
* @param timestamp the timestamp type: posix time in milliseconds.
* @return {@code this} {@link SetArgs}.
* @since 6.1
*/
public SetArgs pxAt(long timestamp) {

this.pxAt = timestamp;
return this;
}

/**
* Set the specified expire at time using a posix {@code timestamp}.
*
* @param timestamp the timestamp type: posix time in milliseconds.
* @return {@code this} {@link SetArgs}.
* @since 6.1
*/
public SetArgs pxAt(Date timestamp) {

LettuceAssert.notNull(timestamp, "Timestamp must not be null");

return pxAt(timestamp.getTime());
}

/**
* Set the specified expire at time using a posix {@code timestamp}.
*
* @param timestamp the timestamp type: posix time in milliseconds.
* @return {@code this} {@link SetArgs}.
* @since 6.1
*/
public SetArgs pxAt(Instant timestamp) {

LettuceAssert.notNull(timestamp, "Timestamp must not be null");

return pxAt(timestamp.toEpochMilli());
}

/**
* Only set the key if it does not already exist.
*
Expand Down Expand Up @@ -163,16 +323,25 @@ public SetArgs xx() {
return this;
}

@Override
public <K, V> void build(CommandArgs<K, V> args) {

if (ex != null) {
args.add("EX").add(ex);
}

if (exAt != null) {
args.add("EXAT").add(exAt);
}

if (px != null) {
args.add("PX").add(px);
}

if (pxAt != null) {
args.add("PXAT").add(pxAt);
}

if (nx) {
args.add("NX");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static io.lettuce.core.StringMatchResult.*;
import static org.assertj.core.api.Assertions.*;

import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -175,6 +176,17 @@ void set() {
assertThat(redis.ttl(key) >= 19).isTrue();
}

@Test
@EnabledOnCommand("ZMSCORE") // Redis 6.2
void setExAt() {

assertThat(redis.set(key, value, exAt(Instant.now().plusSeconds(60)))).isEqualTo("OK");
assertThat(redis.ttl(key)).isBetween(50L, 61L);

assertThat(redis.set(key, value, pxAt(Instant.now().plusSeconds(60)))).isEqualTo("OK");
assertThat(redis.ttl(key)).isBetween(50L, 61L);
}

@Test
@EnabledOnCommand("ACL") // Redis 6.0 guard
void setKeepTTL() {
Expand Down

0 comments on commit 8a31910

Please sign in to comment.