Skip to content

Commit

Permalink
Add support for GET parameter with SET command #1442
Browse files Browse the repository at this point in the history
Original pull request: #1461.
  • Loading branch information
anshlykov authored and mp911de committed Jan 14, 2021
1 parent 851d5c7 commit 19e439f
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 0 deletions.
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
14 changes: 14 additions & 0 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,20 @@ 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.build(args);
args.add("GET");

return createCommand(SET, new ValueOutput<>(codec), args);
}

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

Expand Down
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 @@ -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);

/**
* 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

0 comments on commit 19e439f

Please sign in to comment.