refactoring

pull/4551/head
Nikita Koksharov 3 years ago
parent d01bedb68a
commit bacd89756d

@ -219,6 +219,33 @@ public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
return get(trySetAsync(value));
}
@Override
public boolean setIfAbsent(V value) {
return get(setIfAbsentAsync(value));
}
@Override
public boolean setIfAbsent(V value, Duration duration) {
return get(setIfAbsentAsync(value, duration));
}
@Override
public RFuture<Boolean> setIfAbsentAsync(V value) {
if (value == null) {
return commandExecutor.readAsync(getRawName(), codec, RedisCommands.NOT_EXISTS, getRawName());
}
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SETNX, getRawName(), encode(value));
}
@Override
public RFuture<Boolean> setIfAbsentAsync(V value, Duration duration) {
if (value == null) {
throw new IllegalArgumentException("Value can't be null");
}
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SET_BOOLEAN, getRawName(), encode(value), "PX", duration.toMillis(), "NX");
}
@Override
public boolean setIfExists(V value) {
return get(setIfExistsAsync(value));

@ -117,6 +117,33 @@ public class RedissonJsonBucket<V> extends RedissonExpirable implements RJsonBuc
Collections.singletonList(getRawName()));
}
@Override
public boolean setIfAbsent(V value) {
return get(setIfAbsentAsync(value));
}
@Override
public boolean setIfAbsent(V value, Duration duration) {
return get(setIfAbsentAsync(value, duration));
}
@Override
public RFuture<Boolean> setIfAbsentAsync(V value) {
return setIfAbsentAsync("$", value);
}
@Override
public RFuture<Boolean> setIfAbsentAsync(V value, Duration duration) {
return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN,
"local currValue = redis.call('json.set', KEYS[1], '$', ARGV[1], 'NX'); " +
"if currValue ~= false then " +
"redis.call('pexpire', KEYS[1], ARGV[2]); " +
"return 1;" +
"end;" +
"return 0; ",
Collections.singletonList(getRawName()), encode(value), duration.toMillis());
}
@Override
public boolean trySet(V value) {
return get(trySetAsync(value));
@ -127,6 +154,20 @@ public class RedissonJsonBucket<V> extends RedissonExpirable implements RJsonBuc
return trySetAsync("$", value);
}
@Override
public boolean setIfAbsent(String path, Object value) {
return get(setIfAbsentAsync(path, value));
}
@Override
public RFuture<Boolean> setIfAbsentAsync(String path, Object value) {
if (value == null) {
return commandExecutor.readAsync(getRawName(), codec, RedisCommands.NOT_EXISTS, getRawName());
}
return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.JSON_SET_BOOLEAN, getRawName(), path, encode(value), "NX");
}
@Override
public boolean trySet(String path, Object value) {
return get(trySetAsync(path, value));

@ -50,16 +50,17 @@ public interface RBucket<V> extends RExpirable, RBucketAsync<V> {
V getAndDelete();
/**
* Tries to set element atomically into empty holder.
*
* Use {@link #setIfAbsent(Object)} instead
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
@Deprecated
boolean trySet(V value);
/**
* Tries to set element atomically into empty holder with defined <code>timeToLive</code> interval.
* Use {@link #setIfAbsent(Object, Duration)} instead
*
* @param value - value to set
* @param timeToLive - time to live interval
@ -67,10 +68,30 @@ public interface RBucket<V> extends RExpirable, RBucketAsync<V> {
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
@Deprecated
boolean trySet(V value, long timeToLive, TimeUnit timeUnit);
/**
* Sets value only if it's already exists.
* Sets value only if object holder doesn't exist.
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
boolean setIfAbsent(V value);
/**
* Sets value with defined duration only if object holder doesn't exist.
*
* @param value value to set
* @param duration expiration duration
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
boolean setIfAbsent(V value, Duration duration);
/**
* Sets value only if object holder already exists.
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
@ -79,7 +100,7 @@ public interface RBucket<V> extends RExpirable, RBucketAsync<V> {
boolean setIfExists(V value);
/**
* Sets value only if it's already exists.
* Sets value only if object holder already exists.
*
* @param value - value to set
* @param timeToLive - time to live interval

@ -50,16 +50,17 @@ public interface RBucketAsync<V> extends RExpirableAsync {
RFuture<V> getAndDeleteAsync();
/**
* Tries to set element atomically into empty holder.
* Use {@link #setIfAbsentAsync(Object)} instead
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
@Deprecated
RFuture<Boolean> trySetAsync(V value);
/**
* Tries to set element atomically into empty holder with defined <code>timeToLive</code> interval.
* Use {@link #setIfAbsentAsync(Object, Duration)} instead
*
* @param value - value to set
* @param timeToLive - time to live interval
@ -67,8 +68,28 @@ public interface RBucketAsync<V> extends RExpirableAsync {
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
@Deprecated
RFuture<Boolean> trySetAsync(V value, long timeToLive, TimeUnit timeUnit);
/**
* Sets value only if object holder doesn't exist.
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
RFuture<Boolean> setIfAbsentAsync(V value);
/**
* Sets value with defined duration only if object holder doesn't exist.
*
* @param value value to set
* @param duration expiration duration
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
RFuture<Boolean> setIfAbsentAsync(V value, Duration duration);
/**
* Sets value only if it's already exists.
*

@ -37,18 +37,38 @@ public interface RBucketReactive<V> extends RExpirableReactive {
* @return object size
*/
Mono<Long> size();
/**
* Sets value only if object holder doesn't exist.
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
Mono<Boolean> setIfAbsent(V value);
/**
* Sets value with defined duration only if object holder doesn't exist.
*
* @param value value to set
* @param duration expiration duration
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
Mono<Boolean> setIfAbsent(V value, Duration duration);
/**
* Tries to set element atomically into empty holder.
* Use {@link #setIfAbsent(Object)} instead
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
@Deprecated
Mono<Boolean> trySet(V value);
/**
* Tries to set element atomically into empty holder with defined <code>timeToLive</code> interval.
* Use {@link #setIfAbsent(Object, Duration)} instead
*
* @param value - value to set
* @param timeToLive - time to live interval
@ -56,6 +76,7 @@ public interface RBucketReactive<V> extends RExpirableReactive {
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
@Deprecated
Mono<Boolean> trySet(V value, long timeToLive, TimeUnit timeUnit);
/**

@ -39,18 +39,38 @@ public interface RBucketRx<V> extends RExpirableRx {
* @return object size
*/
Single<Long> size();
/**
* Sets value only if object holder doesn't exist.
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
Single<Boolean> setIfAbsent(V value);
/**
* Sets value with defined duration only if object holder doesn't exist.
*
* @param value value to set
* @param duration expiration duration
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
Single<Boolean> setIfAbsent(V value, Duration duration);
/**
* Tries to set element atomically into empty holder.
* Use {@link #setIfAbsent(Object)} instead
*
* @param value - value to set
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
@Deprecated
Single<Boolean> trySet(V value);
/**
* Tries to set element atomically into empty holder with defined <code>timeToLive</code> interval.
* Use {@link #setIfAbsent(Object, Duration)} instead
*
* @param value - value to set
* @param timeToLive - time to live interval
@ -58,6 +78,7 @@ public interface RBucketRx<V> extends RExpirableRx {
* @return {@code true} if successful, or {@code false} if
* element was already set
*/
@Deprecated
Single<Boolean> trySet(V value, long timeToLive, TimeUnit timeUnit);
/**

@ -46,6 +46,17 @@ public interface RJsonBucket<V> extends RBucket<V>, RJsonBucketAsync<V> {
* @return {@code true} if successful, or {@code false} if
* value was already set
*/
boolean setIfAbsent(String path, Object value);
/**
* Use {@link #setIfAbsent(String, Object)} instead
*
* @param path JSON path
* @param value object
* @return {@code true} if successful, or {@code false} if
* value was already set
*/
@Deprecated
boolean trySet(String path, Object value);
/**

@ -46,6 +46,17 @@ public interface RJsonBucketAsync<V> extends RBucketAsync<V> {
* @return {@code true} if successful, or {@code false} if
* value was already set
*/
RFuture<Boolean> setIfAbsentAsync(String path, Object value);
/**
* Use {@link #setIfAbsentAsync(String, Object)} instead
*
* @param path JSON path
* @param value object
* @return {@code true} if successful, or {@code false} if
* value was already set
*/
@Deprecated
RFuture<Boolean> trySetAsync(String path, Object value);
/**

@ -181,7 +181,7 @@ public class RedissonBucketTest extends BaseTest {
Assumptions.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("4.0.0") > 0);
RBucket<Integer> al = redisson.getBucket("test");
al.set(1234);
assertThat(al.sizeInMemory()).isEqualTo(54);
assertThat(al.sizeInMemory()).isEqualTo(56);
}
@Test
@ -298,16 +298,16 @@ public class RedissonBucketTest extends BaseTest {
@Test
public void testTrySet() {
RBucket<String> r1 = redisson.getBucket("testTrySet");
assertThat(r1.trySet("3")).isTrue();
assertThat(r1.trySet("4")).isFalse();
assertThat(r1.setIfAbsent("3")).isTrue();
assertThat(r1.setIfAbsent("4")).isFalse();
assertThat(r1.get()).isEqualTo("3");
}
@Test
public void testTrySetTTL() throws InterruptedException {
RBucket<String> r1 = redisson.getBucket("testTrySetTTL");
assertThat(r1.trySet("3", 500, TimeUnit.MILLISECONDS)).isTrue();
assertThat(r1.trySet("4", 500, TimeUnit.MILLISECONDS)).isFalse();
assertThat(r1.setIfAbsent("3", Duration.ofMillis(500))).isTrue();
assertThat(r1.setIfAbsent("4", Duration.ofMillis(500))).isFalse();
assertThat(r1.get()).isEqualTo("3");
Thread.sleep(1000);

@ -408,8 +408,8 @@ public class RedissonJsonBucketTest extends BaseTest {
NestedType nt2 = new NestedType();
nt2.setValue(124);
nt2.setValues(Arrays.asList("t4", "t3"));
assertThat(al.trySet("$.type", nt2)).isFalse();
assertThat(al.trySet("type", nt2)).isFalse();
assertThat(al.setIfAbsent("$.type", nt2)).isFalse();
assertThat(al.setIfAbsent("type", nt2)).isFalse();
Integer n2 = al.get(new JacksonCodec<>(Integer.class), "type.value");
assertThat(n2).isEqualTo(123);

Loading…
Cancel
Save