Add support for GETEX and GETDEL through getAndExpire(…), getAndPersist(…) and getAndDelete(…) methods.
We now support Redis 6.2 GETEX and GETDEL commands through the Template API and on the connection level for all drivers. Closes: #2050 Original Pull Request: #2086
This commit is contained in:
committed by
Christoph Strobl
parent
72055de59f
commit
a53193946f
@@ -410,6 +410,46 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
|
||||
return convertAndReturn(delegate.get(key), Converters.identityConverter());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getDel(byte[])
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public byte[] getDel(byte[] key) {
|
||||
return convertAndReturn(delegate.getDel(key), Converters.identityConverter());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getDel(byte[])
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDel(String key) {
|
||||
return convertAndReturn(delegate.getDel(serialize(key)), bytesToString);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#get(byte[], org.springframework.data.redis.core.types.Expiration)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public byte[] getEx(byte[] key, Expiration expiration) {
|
||||
return convertAndReturn(delegate.getEx(key, expiration), Converters.identityConverter());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#get(byte[], org.springframework.data.redis.core.types.Expiration)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public String getEx(String key, Expiration expiration) {
|
||||
return convertAndReturn(delegate.getEx(serialize(key), expiration), bytesToString);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getBit(byte[], long)
|
||||
|
||||
@@ -276,6 +276,20 @@ public interface DefaultedRedisConnection extends RedisConnection {
|
||||
return stringCommands().get(key);
|
||||
}
|
||||
|
||||
/** @deprecated in favor of {@link RedisConnection#stringCommands()}}. */
|
||||
@Override
|
||||
@Deprecated
|
||||
default byte[] getEx(byte[] key, Expiration expiration) {
|
||||
return stringCommands().getEx(key, expiration);
|
||||
}
|
||||
|
||||
/** @deprecated in favor of {@link RedisConnection#stringCommands()}}. */
|
||||
@Override
|
||||
@Deprecated
|
||||
default byte[] getDel(byte[] key) {
|
||||
return stringCommands().getDel(key);
|
||||
}
|
||||
|
||||
/** @deprecated in favor of {@link RedisConnection#stringCommands()}}. */
|
||||
@Override
|
||||
@Deprecated
|
||||
|
||||
@@ -218,6 +218,109 @@ public interface ReactiveStringCommands {
|
||||
*/
|
||||
Flux<ByteBufferResponse<KeyCommand>> get(Publisher<KeyCommand> keys);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and delete the key.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@link Mono#empty()} in case {@literal key} does not exist.
|
||||
* @see <a href="https://redis.io/commands/getdel">Redis Documentation: GETDEL</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
default Mono<ByteBuffer> getDel(ByteBuffer key) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
|
||||
return getDel(Mono.just(new KeyCommand(key))).next().filter(CommandResponse::isPresent)
|
||||
.map(CommandResponse::getOutput);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and delete the key.
|
||||
*
|
||||
* @param commands must not be {@literal null}.
|
||||
* @return {@link Flux} of {@link ByteBufferResponse} holding the {@literal key} to get along with the value
|
||||
* retrieved.
|
||||
* @see <a href="https://redis.io/commands/getdel">Redis Documentation: GETDEL</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
Flux<ByteBufferResponse<KeyCommand>> getDel(Publisher<KeyCommand> commands);
|
||||
|
||||
/**
|
||||
* {@link Command} for {@code GETEX}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 2.6
|
||||
*/
|
||||
class GetExCommand extends KeyCommand {
|
||||
|
||||
private final Expiration expiration;
|
||||
|
||||
private GetExCommand(@Nullable ByteBuffer key, Expiration expiration) {
|
||||
|
||||
super(key);
|
||||
|
||||
Assert.notNull(expiration, "Expiration must not be null!");
|
||||
this.expiration = expiration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link GetExCommand} given a {@code key}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return a new {@link GetExCommand} for {@code key}.
|
||||
*/
|
||||
public static GetExCommand key(ByteBuffer key) {
|
||||
return new GetExCommand(key, Expiration.persistent());
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies {@link Expiration}. Constructs a new command instance with all previously configured properties.
|
||||
*
|
||||
* @param options must not be {@literal null}.
|
||||
* @return a new {@link GetExCommand} with {@link Expiration} applied.
|
||||
*/
|
||||
public GetExCommand withExpiration(Expiration expiration) {
|
||||
return new GetExCommand(getKey(), expiration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link Expiration} to apply.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
public Expiration getExpiration() {
|
||||
return expiration;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and expire the key by applying {@link Expiration}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param expiration must not be {@literal null}.
|
||||
* @return {@link Mono#empty()} in case {@literal key} does not exist.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
default Mono<ByteBuffer> getEx(ByteBuffer key, Expiration expiration) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
|
||||
return getEx(Mono.just(GetExCommand.key(key).withExpiration(expiration))).next().filter(CommandResponse::isPresent)
|
||||
.map(CommandResponse::getOutput);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and expire the key by applying {@link Expiration}.
|
||||
*
|
||||
* @param commands must not be {@literal null}.
|
||||
* @return {@link Flux} of {@link ByteBufferResponse} holding the {@literal key} to get along with the value
|
||||
* retrieved.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
Flux<ByteBufferResponse<GetExCommand>> getEx(Publisher<GetExCommand> commands);
|
||||
|
||||
/**
|
||||
* Set {@literal value} for {@literal key} and return the existing value.
|
||||
*
|
||||
|
||||
@@ -45,6 +45,30 @@ public interface RedisStringCommands {
|
||||
@Nullable
|
||||
byte[] get(byte[] key);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and delete the key.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getdel">Redis Documentation: GETDEL</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
byte[] getDel(byte[] key);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and expire the key by applying {@link Expiration}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param expiration must not be {@literal null}.
|
||||
* @param unit must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
byte[] getEx(byte[] key, Expiration expiration);
|
||||
|
||||
/**
|
||||
* Set {@code value} of {@code key} and return its old value.
|
||||
*
|
||||
@@ -403,4 +427,5 @@ public interface RedisStringCommands {
|
||||
return SET_IF_ABSENT;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -397,6 +397,29 @@ public interface StringRedisConnection extends RedisConnection {
|
||||
*/
|
||||
String get(String key);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and delete the key.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getdel">Redis Documentation: GETDEL</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
String getDel(String key);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and expire the key by applying {@link Expiration}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param expiration must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
String getEx(String key, Expiration expiration);
|
||||
|
||||
/**
|
||||
* Set {@code value} of {@code key} and return its old value.
|
||||
*
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.data.redis.connection.jedis.JedisClusterConnection.Je
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConverters;
|
||||
import org.springframework.data.redis.core.types.Expiration;
|
||||
import org.springframework.data.redis.util.ByteUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -68,6 +69,41 @@ class JedisClusterStringCommands implements RedisStringCommands {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getDel(byte[])
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public byte[] getDel(byte[] key) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
|
||||
try {
|
||||
return connection.getCluster().getDel(key);
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getEx(byte[], org.springframework.data.redis.core.types.Expiration)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public byte[] getEx(byte[] key, Expiration expiration) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
Assert.notNull(expiration, "Expiration must not be null!");
|
||||
|
||||
try {
|
||||
return connection.getCluster().getEx(key, JedisConverters.toGetExParams(expiration));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getSet(byte[], byte[])
|
||||
|
||||
@@ -23,6 +23,7 @@ import redis.clients.jedis.ListPosition;
|
||||
import redis.clients.jedis.ScanParams;
|
||||
import redis.clients.jedis.SortingParams;
|
||||
import redis.clients.jedis.params.GeoRadiusParam;
|
||||
import redis.clients.jedis.params.GetExParams;
|
||||
import redis.clients.jedis.params.SetParams;
|
||||
import redis.clients.jedis.params.ZAddParams;
|
||||
import redis.clients.jedis.util.SafeEncoder;
|
||||
@@ -441,6 +442,34 @@ public abstract class JedisConverters extends Converters {
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a given {@link Expiration} to the according {@code GETEX} command argument.
|
||||
* <dl>
|
||||
* <dt>{@link TimeUnit#MILLISECONDS}</dt>
|
||||
* <dd>{@code PX}</dd>
|
||||
* <dt>{@link TimeUnit#SECONDS}</dt>
|
||||
* <dd>{@code EX}</dd>
|
||||
* </dl>
|
||||
*
|
||||
* @param expiration must not be {@literal null}.
|
||||
* @return
|
||||
* @since 2.6
|
||||
*/
|
||||
static GetExParams toGetExParams(Expiration expiration) {
|
||||
|
||||
GetExParams params = new GetExParams();
|
||||
|
||||
if (expiration.isPersistent()) {
|
||||
return params.persist();
|
||||
}
|
||||
|
||||
if (expiration.getTimeUnit() == TimeUnit.MILLISECONDS) {
|
||||
return params.px(expiration.getExpirationTime());
|
||||
}
|
||||
|
||||
return params.ex((int) expiration.getExpirationTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a given {@link SetOption} to the according {@code SET} command argument.<br />
|
||||
* <dl>
|
||||
|
||||
@@ -57,6 +57,34 @@ class JedisStringCommands implements RedisStringCommands {
|
||||
return connection.invoke().just(BinaryJedis::get, MultiKeyPipelineBase::get, key);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getDel(byte[])
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public byte[] getDel(byte[] key) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
|
||||
return connection.invoke().just(BinaryJedis::getDel, MultiKeyPipelineBase::getDel, key);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getEx(byte[], org.springframework.data.redis.core.types.Expiration)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public byte[] getEx(byte[] key, Expiration expiration) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
Assert.notNull(expiration, "Expiration must not be null!");
|
||||
|
||||
return connection.invoke().just(BinaryJedis::getEx, MultiKeyPipelineBase::getEx, key,
|
||||
JedisConverters.toGetExParams(expiration));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getSet(byte[], byte[])
|
||||
|
||||
@@ -725,6 +725,37 @@ public abstract class LettuceConverters extends Converters {
|
||||
return args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert {@link Expiration} to {@link GetExArgs}.
|
||||
*
|
||||
* @param expiration
|
||||
* @return
|
||||
* @since 2.6
|
||||
*/
|
||||
static GetExArgs toGetExArgs(Expiration expiration) {
|
||||
|
||||
GetExArgs args = new GetExArgs();
|
||||
|
||||
if (expiration != null) {
|
||||
|
||||
if (expiration.isPersistent()) {
|
||||
args.persist();
|
||||
} else if (!expiration.isPersistent()) {
|
||||
|
||||
switch (expiration.getTimeUnit()) {
|
||||
case SECONDS:
|
||||
args.ex(expiration.getExpirationTime());
|
||||
break;
|
||||
default:
|
||||
args.px(expiration.getConverted(TimeUnit.MILLISECONDS));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
static Converter<List<byte[]>, Long> toTimeConverter(TimeUnit timeUnit) {
|
||||
|
||||
return source -> {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.redis.connection.lettuce;
|
||||
|
||||
import io.lettuce.core.BitFieldArgs;
|
||||
import io.lettuce.core.GetExArgs;
|
||||
import io.lettuce.core.SetArgs;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -25,6 +26,7 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import org.springframework.data.domain.Range;
|
||||
import org.springframework.data.redis.connection.ReactiveRedisConnection.AbsentByteBufferResponse;
|
||||
import org.springframework.data.redis.connection.ReactiveRedisConnection.BooleanResponse;
|
||||
@@ -140,6 +142,40 @@ class LettuceReactiveStringCommands implements ReactiveStringCommands {
|
||||
}));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.ReactiveRedisConnection.ReactiveStringCommands#getDel(org.reactivestreams.Publisher)
|
||||
*/
|
||||
@Override
|
||||
public Flux<ByteBufferResponse<KeyCommand>> getDel(Publisher<KeyCommand> commands) {
|
||||
|
||||
return connection.execute(cmd -> Flux.from(commands).concatMap((command) -> {
|
||||
|
||||
Assert.notNull(command.getKey(), "Key must not be null!");
|
||||
|
||||
return cmd.getdel(command.getKey()).map((value) -> new ByteBufferResponse<>(command, value))
|
||||
.defaultIfEmpty(new AbsentByteBufferResponse<>(command));
|
||||
}));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.ReactiveRedisConnection.ReactiveStringCommands#getDel(org.reactivestreams.Publisher)
|
||||
*/
|
||||
@Override
|
||||
public Flux<ByteBufferResponse<GetExCommand>> getEx(Publisher<GetExCommand> commands) {
|
||||
|
||||
return connection.execute(cmd -> Flux.from(commands).concatMap((command) -> {
|
||||
|
||||
Assert.notNull(command.getKey(), "Key must not be null!");
|
||||
|
||||
GetExArgs args = LettuceConverters.toGetExArgs(command.getExpiration());
|
||||
|
||||
return cmd.getex(command.getKey(), args).map((value) -> new ByteBufferResponse<>(command, value))
|
||||
.defaultIfEmpty(new AbsentByteBufferResponse<>(command));
|
||||
}));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.ReactiveRedisConnection.ReactiveStringCommands#setNX(org.reactivestreams.Publisher)
|
||||
|
||||
@@ -55,6 +55,33 @@ class LettuceStringCommands implements RedisStringCommands {
|
||||
return connection.invoke().just(RedisStringAsyncCommands::get, key);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getDel(byte[])
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public byte[] getDel(byte[] key) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
|
||||
return connection.invoke().just(RedisStringAsyncCommands::getdel, key);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getEx(byte[], org.springframework.data.redis.core.types.Expiration)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public byte[] getEx(byte[] key, Expiration expiration) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
Assert.notNull(expiration, "Expiration must not be null!");
|
||||
|
||||
return connection.invoke().just(RedisStringAsyncCommands::getex, key, LettuceConverters.toGetExArgs(expiration));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisStringCommands#getSet(byte[], byte[])
|
||||
|
||||
@@ -93,7 +93,7 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
|
||||
Boolean setIfAbsent(V value, long timeout, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* Set bound key to hold the string {@code value} and expiration {@code timeout} if {@code key} is absent.
|
||||
* Set bound key to hold the string {@code value} and expiration {@code timeout} if the bound key is absent.
|
||||
*
|
||||
* @param value must not be {@literal null}.
|
||||
* @param timeout must not be {@literal null}.
|
||||
@@ -115,7 +115,7 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bound key to hold the string {@code value} if {@code key} is present.
|
||||
* Set the bound key to hold the string {@code value} if the bound key is present.
|
||||
*
|
||||
* @param value must not be {@literal null}.
|
||||
* @return command result indicating if the key has been set.
|
||||
@@ -127,7 +127,7 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
|
||||
Boolean setIfPresent(V value);
|
||||
|
||||
/**
|
||||
* Set the bound key to hold the string {@code value} and expiration {@code timeout} if {@code key} is present.
|
||||
* Set the bound key to hold the string {@code value} and expiration {@code timeout} if the bound key is present.
|
||||
*
|
||||
* @param value must not be {@literal null}.
|
||||
* @param timeout the key expiration timeout.
|
||||
@@ -141,7 +141,7 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
|
||||
Boolean setIfPresent(V value, long timeout, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* Set the bound key to hold the string {@code value} and expiration {@code timeout} if {@code key} is present.
|
||||
* Set the bound key to hold the string {@code value} and expiration {@code timeout} if the bound key is present.
|
||||
*
|
||||
* @param value must not be {@literal null}.
|
||||
* @param timeout must not be {@literal null}.
|
||||
@@ -165,12 +165,57 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
|
||||
/**
|
||||
* Get the value of the bound key.
|
||||
*
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/get">Redis Documentation: GET</a>
|
||||
*/
|
||||
@Nullable
|
||||
V get();
|
||||
|
||||
/**
|
||||
* Return the value at the bound key and delete the key.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getdel">Redis Documentation: GETDEL</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
V getAndDelete();
|
||||
|
||||
/**
|
||||
* Return the value at the bound key and expire the key by applying {@code timeout}.
|
||||
*
|
||||
* @param timeout
|
||||
* @param unit must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
V getAndExpire(long timeout, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* Return the value at the bound key and expire the key by applying {@code timeout}.
|
||||
*
|
||||
* @param timeout must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
V getAndExpire(Duration timeout);
|
||||
|
||||
/**
|
||||
* Return the value at the bound key and persist the key. This operation removes any TTL that is associated with the
|
||||
* bound key.
|
||||
*
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
V getAndPersist();
|
||||
|
||||
/**
|
||||
* Set {@code value} of the bound key and return its old value.
|
||||
*
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.data.redis.connection.DataType;
|
||||
@@ -50,6 +51,46 @@ class DefaultBoundValueOperations<K, V> extends DefaultBoundKeyOperations<K> imp
|
||||
return ops.get(getKey());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#get()
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public V getAndDelete() {
|
||||
return ops.getAndDelete(getKey());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#get(long, java.util.concurrent.TimeUnit)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public V getAndExpire(long timeout, TimeUnit unit) {
|
||||
return ops.getAndExpire(getKey(), timeout, unit);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#get(java.time.Duration)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public V getAndExpire(Duration timeout) {
|
||||
return ops.getAndExpire(getKey(), timeout);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#getAndPersist()
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public V getAndPersist() {
|
||||
return ops.getAndPersist(getKey());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#getAndSet(java.lang.Object)
|
||||
|
||||
@@ -176,6 +176,43 @@ class DefaultReactiveValueOperations<K, V> implements ReactiveValueOperations<K,
|
||||
.map(this::readValue));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#getAndDelete(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Mono<V> getAndDelete(K key) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
|
||||
return createMono(connection -> connection.getDel(rawKey(key)) //
|
||||
.map(this::readValue));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#getAndExpire(java.lang.Object, java.time.Duration)
|
||||
*/
|
||||
@Override
|
||||
public Mono<V> getAndExpire(K key, Duration timeout) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
Assert.notNull(timeout, "Timeout must not be null!");
|
||||
|
||||
return createMono(connection -> connection.getEx(rawKey(key), Expiration.from(timeout)) //
|
||||
.map(this::readValue));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#getAndPersist(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Mono<V> getAndPersist(K key) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
|
||||
return createMono(connection -> connection.getEx(rawKey(key), Expiration.persistent()) //
|
||||
.map(this::readValue));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#getAndSet(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -59,6 +60,74 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
|
||||
}, true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#getAndDelete(java.lang.Object)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public V getAndDelete(K key) {
|
||||
|
||||
return execute(new ValueDeserializingRedisCallback(key) {
|
||||
|
||||
@Override
|
||||
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
|
||||
return connection.getDel(rawKey);
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#getAndPersist(java.lang.Object, long, java.util.concurrent.TimeUnit)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public V getAndExpire(K key, long timeout, TimeUnit unit) {
|
||||
|
||||
return execute(new ValueDeserializingRedisCallback(key) {
|
||||
|
||||
@Override
|
||||
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
|
||||
return connection.getEx(rawKey, Expiration.from(timeout, unit));
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#getAndPersist(java.lang.Object, java.time.Duration)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public V getAndExpire(K key, Duration timeout) {
|
||||
|
||||
return execute(new ValueDeserializingRedisCallback(key) {
|
||||
|
||||
@Override
|
||||
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
|
||||
return connection.getEx(rawKey, Expiration.from(timeout));
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#getAndPersist(java.lang.Object)
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public V getAndPersist(K key) {
|
||||
|
||||
return execute(new ValueDeserializingRedisCallback(key) {
|
||||
|
||||
@Override
|
||||
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
|
||||
return connection.getEx(rawKey, Expiration.persistent());
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#getAndSet(java.lang.Object, java.lang.Object)
|
||||
|
||||
@@ -117,6 +117,35 @@ public interface ReactiveValueOperations<K, V> {
|
||||
*/
|
||||
Mono<V> get(Object key);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and delete the key.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @see <a href="https://redis.io/commands/getdel">Redis Documentation: GETDEL</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
Mono<V> getAndDelete(K key);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and expire the key by applying {@code timeout}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param timeout must not be {@literal null}.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
Mono<V> getAndExpire(K key, Duration timeout);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and persist the key. This operation removes any TTL that is associated with
|
||||
* {@code key}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
Mono<V> getAndPersist(K key);
|
||||
|
||||
/**
|
||||
* Set {@code value} of {@code key} and return its old value.
|
||||
*
|
||||
|
||||
@@ -198,17 +198,65 @@ public interface ValueOperations<K, V> {
|
||||
* Get the value of {@code key}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/get">Redis Documentation: GET</a>
|
||||
*/
|
||||
@Nullable
|
||||
V get(Object key);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and delete the key.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getdel">Redis Documentation: GETDEL</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
V getAndDelete(K key);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and expire the key by applying {@code timeout}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param timeout
|
||||
* @param unit must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
V getAndExpire(K key, long timeout, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and expire the key by applying {@code timeout}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param timeout must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
V getAndExpire(K key, Duration timeout);
|
||||
|
||||
/**
|
||||
* Return the value at {@code key} and persist the key. This operation removes any TTL that is associated with
|
||||
* {@code key}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getex">Redis Documentation: GETEX</a>
|
||||
* @since 2.6
|
||||
*/
|
||||
@Nullable
|
||||
V getAndPersist(K key);
|
||||
|
||||
/**
|
||||
* Set {@code value} of {@code key} and return its old value.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @return {@literal null} when key does not exist or used in pipeline / transaction.
|
||||
* @see <a href="https://redis.io/commands/getset">Redis Documentation: GETSET</a>
|
||||
*/
|
||||
@Nullable
|
||||
|
||||
Reference in New Issue
Block a user