Skip to content

Add support for GETEX and GETDEL through getAndExpire(…), getAndPersist(…) and getAndDelete(…) methods #2086

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.6.0-SNAPSHOT</version>
<version>2.6.0-2050-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
2 changes: 1 addition & 1 deletion src/main/asciidoc/new-features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This section briefly covers items that are new and noteworthy in the latest rele
== New in Spring Data Redis 2.6

* Support for `SubscriptionListener` when using `MessageListener` for subscription confirmation callbacks. `ReactiveRedisMessageListenerContainer` and `ReactiveRedisOperations` provide `receiveLater(…)` and `listenToLater(…)` methods to await until Redis acknowledges the subscription.
* Support Redis 6.2 commands (`LPOP`/`RPOP` with `count`, `COPY`).
* Support Redis 6.2 commands (`LPOP`/`RPOP` with `count`, `COPY`, `GETEX`, `GETDEL`).

[[new-in-2.5.0]]
== New in Spring Data Redis 2.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,46 @@ public byte[] get(byte[] key) {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,20 @@ default byte[] get(byte[] key) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,109 @@ default Mono<ByteBuffer> get(ByteBuffer key) {
*/
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.data.domain.Range;
import org.springframework.data.redis.core.types.Expiration;
Expand Down Expand Up @@ -45,6 +46,33 @@ enum BitOperation {
@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}.
* <p />
* Use {@link Expiration#seconds(long)} for {@code EX}. <br />
* Use {@link Expiration#milliseconds(long)} for {@code PX}. <br />
* Use {@link Expiration#unixTimestamp(long, TimeUnit)} for {@code EXAT | PXAT}. <br />
*
* @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
byte[] getEx(byte[] key, Expiration expiration);

/**
* Set {@code value} of {@code key} and return its old value.
*
Expand Down Expand Up @@ -403,4 +431,5 @@ public static SetOption ifAbsent() {
return SET_IF_ABSENT;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,29 @@ interface StringTuple extends Tuple {
*/
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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;

/**
Expand Down Expand Up @@ -68,6 +69,41 @@ public byte[] get(byte[] 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!");

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[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -430,15 +431,48 @@ public static SetParams toSetCommandExPxArgument(Expiration expiration, SetParam
return paramsToUse.keepttl();
}

if (!expiration.isPersistent()) {
if (expiration.getTimeUnit() == TimeUnit.MILLISECONDS) {
return paramsToUse.px(expiration.getExpirationTime());
}
if (expiration.isPersistent()) {
return paramsToUse;
}

if (expiration.getTimeUnit() == TimeUnit.MILLISECONDS) {
return expiration.isUnixTimestamp() ? paramsToUse.pxAt(expiration.getExpirationTime()) : paramsToUse.px(expiration.getExpirationTime());
}

return expiration.isUnixTimestamp() ? paramsToUse.exAt(expiration.getConverted(TimeUnit.SECONDS)) : paramsToUse.ex(expiration.getConverted(TimeUnit.SECONDS));
}

/**
* Converts a given {@link Expiration} to the according {@code GETEX} command argument depending on
* {@link Expiration#isUnixTimestamp()}.
* <dl>
* <dt>{@link TimeUnit#MILLISECONDS}</dt>
* <dd>{@code PX|PXAT}</dd>
* <dt>{@link TimeUnit#SECONDS}</dt>
* <dd>{@code EX|EXAT}</dd>
* </dl>
*
* @param expiration must not be {@literal null}.
* @return
* @since 2.6
*/
static GetExParams toGetExParams(Expiration expiration) {

GetExParams params = new GetExParams();

return paramsToUse.ex((int) expiration.getExpirationTime());
if (expiration.isPersistent()) {
return params.persist();
}

if (expiration.getTimeUnit() == TimeUnit.MILLISECONDS) {
if (expiration.isUnixTimestamp()) {
return params.pxAt(expiration.getExpirationTime());
}
return params.px(expiration.getExpirationTime());
}

return params;
return expiration.isUnixTimestamp() ? params.exAt(expiration.getConverted(TimeUnit.SECONDS))
: params.ex(expiration.getConverted(TimeUnit.SECONDS));
}

/**
Expand Down
Loading