Skip to content

Commit

Permalink
Add support for KEEPTTL with SET #1234
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Feb 21, 2020
1 parent 6970214 commit c1a2d55
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/main/java/io/lettuce/core/SetArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SetArgs implements CompositeArgument {
private Long px;
private boolean nx = false;
private boolean xx = false;
private boolean keepttl = false;

/**
* Builder entry points for {@link SetArgs}.
Expand Down Expand Up @@ -86,6 +87,17 @@ public static SetArgs nx() {
public static SetArgs xx() {
return new SetArgs().xx();
}

/**
* Creates new {@link SetArgs} and enabling {@literal KEEPTTL}.
*
* @return new {@link SetArgs} with {@literal KEEPTTL} enabled.
* @see SetArgs#keepttl()
* @since 5.3
*/
public static SetArgs keepttl() {
return new SetArgs().keepttl();
}
}

/**
Expand Down Expand Up @@ -123,6 +135,18 @@ public SetArgs nx() {
return this;
}

/**
* Set the value and retain the existing TTL.
*
* @return {@code this} {@link SetArgs}.
* @since 5.3
*/
public SetArgs keepttl() {

this.keepttl = true;
return this;
}

/**
* Only set the key if it already exists.
*
Expand Down Expand Up @@ -151,5 +175,9 @@ public <K, V> void build(CommandArgs<K, V> args) {
if (xx) {
args.add("XX");
}

if (keepttl) {
args.add("KEEPTTL");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
package io.lettuce.core.commands;

import static io.lettuce.core.SetArgs.Builder.ex;
import static io.lettuce.core.SetArgs.Builder.nx;
import static io.lettuce.core.SetArgs.Builder.px;
import static io.lettuce.core.SetArgs.Builder.xx;
import static io.lettuce.core.SetArgs.Builder.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand All @@ -39,6 +36,7 @@
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.test.KeyValueStreamingAdapter;
import io.lettuce.test.LettuceExtension;
import io.lettuce.test.condition.EnabledOnCommand;

/**
* @author Will Glozer
Expand Down Expand Up @@ -170,6 +168,17 @@ void set() {
assertThat(redis.ttl(key) >= 19).isTrue();
}

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

redis.set(key, value, ex(10));

assertThat(redis.set(key, "value2", keepttl())).isEqualTo("OK");
assertThat(redis.get(key)).isEqualTo("value2");
assertThat(redis.ttl(key) >= 1).isTrue();
}

@Test
void setNegativeEX() {
assertThatThrownBy(() -> redis.set(key, value, ex(-10))).isInstanceOf(RedisException. class);
Expand Down

0 comments on commit c1a2d55

Please sign in to comment.