Feature - added RBucket.getAndExpire and getAndClearExpire() methods #4138

pull/4162/head
Nikita Koksharov 3 years ago
parent 823065bdc1
commit dcfff21ff9

@ -26,6 +26,8 @@ import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.misc.CompletableFutureWrapper;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@ -98,6 +100,36 @@ public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.GETSET, getRawName(), encode(newValue));
}
@Override
public V getAndExpire(Instant time) {
return get(getAndExpireAsync(time));
}
@Override
public RFuture<V> getAndExpireAsync(Instant time) {
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.GETEX, getRawName(), "PXAT", time.toEpochMilli());
}
@Override
public V getAndExpire(Duration duration) {
return get(getAndExpireAsync(duration));
}
@Override
public RFuture<V> getAndExpireAsync(Duration duration) {
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.GETEX, getRawName(), "PX", duration.toMillis());
}
@Override
public V getAndClearExpire() {
return get(getAndClearExpireAsync());
}
@Override
public RFuture<V> getAndClearExpireAsync() {
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.GETEX, getRawName(), "PERSIST");
}
@Override
public V get() {
return get(getAsync());

@ -15,6 +15,8 @@
*/
package org.redisson.api;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
/**
@ -116,7 +118,36 @@ public interface RBucket<V> extends RExpirable, RBucketAsync<V> {
* @return previous value
*/
V getAndSet(V value, long timeToLive, TimeUnit timeUnit);
/**
* Retrieves current element in the holder and sets an expiration duration for it.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param duration of object time to live interval
* @return element
*/
V getAndExpire(Duration duration);
/**
* Retrieves current element in the holder and sets an expiration date for it.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param time of exact object expiration moment
* @return element
*/
V getAndExpire(Instant time);
/**
* Retrieves current element in the holder and clears expiration date set before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @return element
*/
V getAndClearExpire();
/**
* Stores element into the holder.
*

@ -15,6 +15,8 @@
*/
package org.redisson.api;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
/**
@ -116,7 +118,36 @@ public interface RBucketAsync<V> extends RExpirableAsync {
* @return previous value
*/
RFuture<V> getAndSetAsync(V value, long timeToLive, TimeUnit timeUnit);
/**
* Retrieves current element in the holder and sets an expiration duration for it.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param duration of object time to live interval
* @return element
*/
RFuture<V> getAndExpireAsync(Duration duration);
/**
* Retrieves current element in the holder and sets an expiration date for it.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param time of exact object expiration moment
* @return element
*/
RFuture<V> getAndExpireAsync(Instant time);
/**
* Retrieves current element in the holder and clears expiration date set before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @return element
*/
RFuture<V> getAndClearExpireAsync();
/**
* Stores element into the holder.
*

@ -17,6 +17,8 @@ package org.redisson.api;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
@ -105,7 +107,36 @@ public interface RBucketReactive<V> extends RExpirableReactive {
* @return previous value
*/
Mono<V> getAndSet(V value, long timeToLive, TimeUnit timeUnit);
/**
* Retrieves current element in the holder and sets an expiration duration for it.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param duration of object time to live interval
* @return element
*/
Mono<V> getAndExpire(Duration duration);
/**
* Retrieves current element in the holder and sets an expiration date for it.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param time of exact object expiration moment
* @return element
*/
Mono<V> getAndExpire(Instant time);
/**
* Retrieves current element in the holder and clears expiration date set before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @return element
*/
Mono<V> getAndClearExpire();
/**
* Retrieves element stored in the holder.
*

@ -19,6 +19,8 @@ import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
@ -108,6 +110,35 @@ public interface RBucketRx<V> extends RExpirableRx {
*/
Maybe<V> getAndSet(V value, long timeToLive, TimeUnit timeUnit);
/**
* Retrieves current element in the holder and sets an expiration duration for it.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param duration of object time to live interval
* @return element
*/
Maybe<V> getAndExpire(Duration duration);
/**
* Retrieves current element in the holder and sets an expiration date for it.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @param time of exact object expiration moment
* @return element
*/
Maybe<V> getAndExpire(Instant time);
/**
* Retrieves current element in the holder and clears expiration date set before.
* <p>
* Requires <b>Redis 6.2.0 and higher.</b>
*
* @return element
*/
Maybe<V> getAndClearExpire();
/**
* Retrieves element stored in the holder.
*

@ -338,6 +338,7 @@ public interface RedisCommands {
RedisStrictCommand<Void> RESTORE = new RedisStrictCommand<Void>("RESTORE", new VoidReplayConvertor());
RedisCommand<Object> GET = new RedisCommand<Object>("GET");
RedisCommand<Object> GETEX = new RedisCommand<Object>("GETEX");
RedisCommand<Object> GETRANGE = new RedisCommand<Object>("GETRANGE");
RedisCommand<Long> SETRANGE = new RedisCommand<Long>("SETRANGE");
RedisStrictCommand<Long> GET_LONG = new RedisStrictCommand<Long>("GET", new LongReplayConvertor());

@ -1,14 +1,5 @@
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
@ -22,8 +13,44 @@ import org.redisson.api.RedissonClient;
import org.redisson.api.listener.SetObjectListener;
import org.redisson.config.Config;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
public class RedissonBucketTest extends BaseTest {
@Test
public void testGetAndClearExpire() {
RBucket<Integer> al = redisson.getBucket("test");
al.set(1, 1, TimeUnit.SECONDS);
assertThat(al.getAndClearExpire()).isEqualTo(1);
assertThat(al.remainTimeToLive()).isEqualTo(-1);
}
@Test
public void testGetAndExpire() throws InterruptedException {
RBucket<Integer> al = redisson.getBucket("test");
al.set(1);
assertThat(al.getAndExpire(Duration.ofSeconds(1))).isEqualTo(1);
Thread.sleep(500);
assertThat(al.get()).isEqualTo(1);
Thread.sleep(600);
assertThat(al.get()).isNull();
al.set(2);
assertThat(al.getAndExpire(Instant.now().plusSeconds(1))).isEqualTo(2);
Thread.sleep(500);
assertThat(al.get()).isEqualTo(2);
Thread.sleep(600);
assertThat(al.get()).isNull();
}
@Test
public void testExpireTime() {
RBucket<Integer> al = redisson.getBucket("test");

Loading…
Cancel
Save