Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET parameter to SET command #1461

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,16 @@ public RedisFuture<V> getset(K key, V value) {
return dispatch(commandBuilder.getset(key, value));
}

@Override
public RedisFuture<V> setget(K key, V value) {
return dispatch(commandBuilder.setget(key, value));
}

@Override
public RedisFuture<V> setget(K key, V value, SetArgs setArgs) {
return dispatch(commandBuilder.setget(key, value, setArgs));
}

@Override
public RedisFuture<Long> hdel(K key, K... fields) {
return dispatch(commandBuilder.hdel(key, fields));
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,16 @@ public Mono<V> getset(K key, V value) {
return createMono(() -> commandBuilder.getset(key, value));
}

@Override
public Mono<V> setget(K key, V value) {
return createMono(() -> commandBuilder.setget(key, value));
}

@Override
public Mono<V> setget(K key, V value, SetArgs setArgs) {
return createMono(() -> commandBuilder.setget(key, value, setArgs));
}

@Override
public Mono<Long> hdel(K key, K... fields) {
return createMono(() -> commandBuilder.hdel(key, fields));
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,18 @@ Command<K, V, String> set(K key, V value, SetArgs setArgs) {
return createCommand(SET, new StatusOutput<>(codec), args);
}

Command<K, V, V> setget(K key, V value) {
return setget(key, value, new SetArgs());
}

Command<K, V, V> setget(K key, V value, SetArgs setArgs) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key).addValue(value);
setArgs.get().build(args);
return createCommand(SET, new ValueOutput<>(codec), args);
}

Command<K, V, Long> setbit(K key, long offset, int value) {
notNullKey(key);

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/io/lettuce/core/SetArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @author Will Glozer
* @author Vincent Rischmann
* @author Mark Paluch
* @author Andrey Shlykov
*/
public class SetArgs implements CompositeArgument {

Expand All @@ -39,6 +40,8 @@ public class SetArgs implements CompositeArgument {

private boolean keepttl = false;

private boolean get = false;
sokomishalov marked this conversation as resolved.
Show resolved Hide resolved

/**
* Builder entry points for {@link SetArgs}.
*/
Expand Down Expand Up @@ -103,6 +106,17 @@ public static SetArgs keepttl() {
return new SetArgs().keepttl();
}

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

}

/**
Expand Down Expand Up @@ -163,6 +177,17 @@ public SetArgs xx() {
return this;
}

/**
* Return the old value stored at key, or nil when key did not exist.
*
* @return {@code this} {@link SetArgs}.
*/
public SetArgs get() {

this.get = true;
return this;
}

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

if (ex != null) {
Expand All @@ -184,6 +209,10 @@ public <K, V> void build(CommandArgs<K, V> args) {
if (keepttl) {
args.add("KEEPTTL");
}

if (get) {
args.add("GET");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ public interface RedisStringAsyncCommands<K, V> {
*/
RedisFuture<String> set(K key, V value, SetArgs setArgs);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
RedisFuture<V> setget(K key, V value);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
* @param setArgs the command arguments
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
RedisFuture<V> setget(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,29 @@ public interface RedisStringReactiveCommands<K, V> {
*/
Mono<String> set(K key, V value, SetArgs setArgs);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
Mono<V> setget(K key, V value);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
* @param setArgs the command arguments
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
Mono<V> setget(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
*
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/io/lettuce/core/api/sync/RedisStringCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ public interface RedisStringCommands<K, V> {
*/
String set(K key, V value, SetArgs setArgs);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
V setget(K key, V value);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
* @param setArgs the command arguments
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
V setget(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ public interface NodeSelectionStringAsyncCommands<K, V> {
*/
AsyncExecutions<String> set(K key, V value, SetArgs setArgs);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
AsyncExecutions<V> setget(K key, V value);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
* @param setArgs the command arguments
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
AsyncExecutions<V> setget(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ public interface NodeSelectionStringCommands<K, V> {
*/
Executions<String> set(K key, V value, SetArgs setArgs);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
Executions<V> setget(K key, V value);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
* @param setArgs the command arguments
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
Executions<V> setget(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ interface RedisStringCoroutinesCommands<K : Any, V : Any> {
* @param keys the key.
* @return List<V> array-reply list of values at the specified keys.
*/
fun mget(vararg keys: K): Flow<KeyValue<K, V>>
fun mget(vararg keys: K): Flow<KeyValue<K,V>>

/**
* Set multiple keys to multiple values.
Expand Down Expand Up @@ -296,6 +296,27 @@ interface RedisStringCoroutinesCommands<K : Any, V : Any> {
*/
suspend fun set(key: K, value: V, setArgs: SetArgs): String?

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
* @return V bulk-string-reply the old value stored at `key`, or `null` when `key` did not exist.
* @since 6.2
*/
suspend fun setget(key: K, value: V): V?

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
* @param setArgs the command arguments
* @return V bulk-string-reply the old value stored at `key`, or `null` when `key` did not exist.
* @since 6.2
*/
suspend fun setget(key: K, value: V, setArgs: SetArgs): V?

/**
* Sets or clears the bit at offset in the string value stored at key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ internal class RedisStringCoroutinesCommandsImpl<K : Any, V : Any>(internal val

override suspend fun set(key: K, value: V, setArgs: SetArgs): String? = ops.set(key, value, setArgs).awaitFirstOrNull()

override suspend fun setget(key: K, value: V): V? = ops.setget(key, value).awaitFirstOrNull()

override suspend fun setget(key: K, value: V, setArgs: SetArgs): V? = ops.setget(key, value, setArgs).awaitFirstOrNull()

override suspend fun setbit(key: K, offset: Long, value: Int): Long? = ops.setbit(key, offset, value).awaitFirstOrNull()

override suspend fun setex(key: K, seconds: Long, value: V): String? = ops.setex(key, seconds, value).awaitFirstOrNull()
Expand Down
23 changes: 23 additions & 0 deletions src/main/templates/io/lettuce/core/api/RedisStringCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,29 @@ public interface RedisStringCommands<K, V> {
*/
String set(K key, V value, SetArgs setArgs);

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
V setget(K key, V value);
sokomishalov marked this conversation as resolved.
Show resolved Hide resolved

/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
* @param setArgs the command arguments
*
* @return V bulk-string-reply the old value stored at {@code key}, or {@code null} when {@code key} did not exist.
* @since 6.2
*/
V setget(K key, V value, SetArgs setArgs);

/**
* Sets or clears the bit at offset in the string value stored at key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @author Will Glozer
* @author Mark Paluch
* @author dengliming
* @author Andrey Shlykov
*/
@ExtendWith(LettuceExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -191,6 +192,14 @@ void setNegativePX() {
assertThatThrownBy(() -> redis.set(key, value, px(-1000))).isInstanceOf(RedisException. class);
}

@Test
@EnabledOnCommand("ZMSCORE") // Redis 6.2
void setget() {
assertThat(redis.setget(key, value)).isNull();
assertThat(redis.setget(key, "value2")).isEqualTo(value);
assertThat(redis.get(key)).isEqualTo("value2");
}

@Test
void setbit() {
assertThat(redis.setbit(key, 0, 1)).isEqualTo(0);
Expand Down