Feature - setIfExists method added to RBucket, RBucketRx, RBucketReactive interfaces. #2817

pull/2853/head
Nikita Koksharov 5 years ago
parent dd445b4370
commit cbffaf33fa

@ -175,7 +175,7 @@ public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
if (value == null) {
throw new IllegalArgumentException("Value can't be null");
}
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SETPXNX, getName(), encode(value), "PX", timeUnit.toMillis(timeToLive), "NX");
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SET_BOOLEAN, getName(), encode(value), "PX", timeUnit.toMillis(timeToLive), "NX");
}
@Override
@ -188,6 +188,41 @@ public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
return get(trySetAsync(value));
}
@Override
public boolean setIfExists(V value) {
return get(setIfExistsAsync(value));
}
@Override
public RFuture<Boolean> setIfExistsAsync(V value) {
if (value == null) {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
"local currValue = redis.call('get', KEYS[1]); " +
"if currValue ~= false then " +
"redis.call('del', KEYS[1]); " +
"return 1;" +
"end;" +
"return 0; ",
Collections.singletonList(getName()));
}
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SET_BOOLEAN, getName(), encode(value), "XX");
}
@Override
public boolean setIfExists(V value, long timeToLive, TimeUnit timeUnit) {
return get(setIfExistsAsync(value, timeToLive, timeUnit));
}
@Override
public RFuture<Boolean> setIfExistsAsync(V value, long timeToLive, TimeUnit timeUnit) {
if (value == null) {
throw new IllegalArgumentException("Value can't be null");
}
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SET_BOOLEAN, getName(), encode(value), "PX", timeUnit.toMillis(timeToLive), "XX");
}
@Override
public RFuture<V> getAndSetAsync(V value, long timeToLive, TimeUnit timeUnit) {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_OBJECT,

@ -67,6 +67,26 @@ public interface RBucket<V> extends RExpirable, RBucketAsync<V> {
*/
boolean trySet(V value, long timeToLive, TimeUnit timeUnit);
/**
* Sets value only if it's already exists.
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element wasn't set
*/
boolean setIfExists(V value);
/**
* Sets value only if it's already exists.
*
* @param value - value to set
* @param timeToLive - time to live interval
* @param timeUnit - unit of time to live interval
* @return {@code true} if successful, or {@code false} if
* element wasn't set
*/
boolean setIfExists(V value, long timeToLive, TimeUnit timeUnit);
/**
* Atomically sets the value to the given updated value
* only if serialized state of the current value equals

@ -67,6 +67,26 @@ public interface RBucketAsync<V> extends RExpirableAsync {
*/
RFuture<Boolean> trySetAsync(V value, long timeToLive, TimeUnit timeUnit);
/**
* Sets value only if it's already exists.
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element wasn't set
*/
RFuture<Boolean> setIfExistsAsync(V value);
/**
* Sets value only if it's already exists.
*
* @param value - value to set
* @param timeToLive - time to live interval
* @param timeUnit - unit of time to live interval
* @return {@code true} if successful, or {@code false} if
* element wasn't set
*/
RFuture<Boolean> setIfExistsAsync(V value, long timeToLive, TimeUnit timeUnit);
/**
* Atomically sets the value to the given updated value
* only if serialized state of the current value equals

@ -15,10 +15,10 @@
*/
package org.redisson.api;
import java.util.concurrent.TimeUnit;
import reactor.core.publisher.Mono;
import java.util.concurrent.TimeUnit;
/**
* Reactive implementation of object holder. Max size of object is 512MB
@ -56,6 +56,26 @@ public interface RBucketReactive<V> extends RExpirableReactive {
*/
Mono<Boolean> trySet(V value, long timeToLive, TimeUnit timeUnit);
/**
* Sets value only if it's already exists.
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element wasn't set
*/
Mono<Boolean> setIfExists(V value);
/**
* Sets value only if it's already exists.
*
* @param value - value to set
* @param timeToLive - time to live interval
* @param timeUnit - unit of time to live interval
* @return {@code true} if successful, or {@code false} if
* element wasn't set
*/
Mono<Boolean> setIfExists(V value, long timeToLive, TimeUnit timeUnit);
/**
* Atomically sets the value to the given updated value
* only if serialized state of the current value equals

@ -15,12 +15,12 @@
*/
package org.redisson.api;
import java.util.concurrent.TimeUnit;
import io.reactivex.Completable;
import io.reactivex.Maybe;
import io.reactivex.Single;
import java.util.concurrent.TimeUnit;
/**
* Reactive implementation of object holder. Max size of object is 512MB
@ -58,6 +58,26 @@ public interface RBucketRx<V> extends RExpirableRx {
*/
Single<Boolean> trySet(V value, long timeToLive, TimeUnit timeUnit);
/**
* Sets value only if it's already exists.
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element wasn't set
*/
Single<Boolean> setIfExists(V value);
/**
* Sets value only if it's already exists.
*
* @param value - value to set
* @param timeToLive - time to live interval
* @param timeUnit - unit of time to live interval
* @return {@code true} if successful, or {@code false} if
* element wasn't set
*/
Single<Boolean> setIfExists(V value, long timeToLive, TimeUnit timeUnit);
/**
* Atomically sets the value to the given updated value
* only if serialized state of the current value equals

@ -299,7 +299,7 @@ public interface RedisCommands {
RedisCommand<Object> GETSET = new RedisCommand<Object>("GETSET");
RedisCommand<Void> SET = new RedisCommand<Void>("SET", new VoidReplayConvertor());
RedisCommand<Void> APPEND = new RedisCommand<Void>("APPEND", new VoidReplayConvertor());
RedisCommand<Boolean> SETPXNX = new RedisCommand<Boolean>("SET", new BooleanNotNullReplayConvertor());
RedisCommand<Boolean> SET_BOOLEAN = new RedisCommand<Boolean>("SET", new BooleanNotNullReplayConvertor());
RedisCommand<Boolean> SETNX = new RedisCommand<Boolean>("SETNX", new BooleanReplayConvertor());
RedisCommand<Void> PSETEX = new RedisCommand<Void>("PSETEX", new VoidReplayConvertor());

@ -216,6 +216,23 @@ public class RedissonBucketTest extends BaseTest {
assertThat(r1.isExists()).isFalse();
}
@Test
public void testSetIfExists() throws InterruptedException {
RBucket<String> r1 = redisson.getBucket("test1");
assertThat(r1.setIfExists("0")).isFalse();
assertThat(r1.isExists()).isFalse();
r1.set("1");
assertThat(r1.setIfExists("2")).isTrue();
assertThat(r1.get()).isEqualTo("2");
RBucket<String> r2 = redisson.getBucket("test2");
r2.set("1");
assertThat(r2.setIfExists("2", 1, TimeUnit.SECONDS)).isTrue();
assertThat(r2.get()).isEqualTo("2");
Thread.sleep(1000);
assertThat(r2.isExists()).isFalse();
}
@Test
public void testTrySet() {
RBucket<String> r1 = redisson.getBucket("testTrySet");

Loading…
Cancel
Save