From 81b06b19fefa01d5a3c1a25f091584bc4a35f5f2 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Tue, 18 Jul 2023 09:33:26 +0300 Subject: [PATCH] refactoring --- .../java/org/redisson/RedissonBucket.java | 43 +++++++++++++++ .../java/org/redisson/RedissonJsonBucket.java | 53 +++++++++++++++++++ .../main/java/org/redisson/api/RBucket.java | 37 +++++++++++-- .../java/org/redisson/api/RBucketAsync.java | 34 ++++++++++-- .../org/redisson/api/RBucketReactive.java | 37 +++++++++++-- .../main/java/org/redisson/api/RBucketRx.java | 37 +++++++++++-- .../java/org/redisson/RedissonBucketTest.java | 14 ++--- 7 files changed, 236 insertions(+), 19 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonBucket.java b/redisson/src/main/java/org/redisson/RedissonBucket.java index b20125255..20e4f0a58 100644 --- a/redisson/src/main/java/org/redisson/RedissonBucket.java +++ b/redisson/src/main/java/org/redisson/RedissonBucket.java @@ -192,6 +192,20 @@ public class RedissonBucket extends RedissonExpirable implements RBucket { return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.PSETEX, getRawName(), timeUnit.toMillis(timeToLive), encode(value)); } + @Override + public void set(V value, Duration duration) { + get(setAsync(value, duration)); + } + + @Override + public RFuture setAsync(V value, Duration duration) { + if (value == null) { + return commandExecutor.writeAsync(getRawName(), RedisCommands.DEL_VOID, getRawName()); + } + + return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.PSETEX, getRawName(), duration.toMillis(), encode(value)); + } + @Override public RFuture trySetAsync(V value) { if (value == null) { @@ -295,6 +309,35 @@ public class RedissonBucket extends RedissonExpirable implements RBucket { return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.SET_BOOLEAN, getRawName(), encode(value), "PX", timeUnit.toMillis(timeToLive), "XX"); } + @Override + public boolean setIfExists(V value, Duration duration) { + return get(setIfExistsAsync(value, duration)); + } + + @Override + public RFuture setIfExistsAsync(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(), "XX"); + } + + @Override + public V getAndSet(V value, Duration duration) { + return get(getAndSetAsync(value, duration)); + } + + @Override + public RFuture getAndSetAsync(V value, Duration duration) { + return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_OBJECT, + "local currValue = redis.call('get', KEYS[1]); " + + "redis.call('psetex', KEYS[1], ARGV[2], ARGV[1]); " + + "return currValue; ", + Collections.singletonList(getRawName()), + encode(value), duration.toMillis()); + } + @Override public RFuture getAndSetAsync(V value, long timeToLive, TimeUnit timeUnit) { return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_OBJECT, diff --git a/redisson/src/main/java/org/redisson/RedissonJsonBucket.java b/redisson/src/main/java/org/redisson/RedissonJsonBucket.java index dc34049ce..e89886e85 100644 --- a/redisson/src/main/java/org/redisson/RedissonJsonBucket.java +++ b/redisson/src/main/java/org/redisson/RedissonJsonBucket.java @@ -236,6 +236,23 @@ public class RedissonJsonBucket extends RedissonExpirable implements RJsonBuc Collections.singletonList(getRawName()), encode(value), timeUnit.toMillis(timeToLive)); } + @Override + public boolean setIfExists(V value, Duration duration) { + return get(setIfExistsAsync(value, duration)); + } + + @Override + public RFuture setIfExistsAsync(V value, Duration duration) { + return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN, + "local currValue = redis.call('json.set', KEYS[1], '$', ARGV[1], 'XX'); " + + "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 compareAndSet(V expect, V update) { return get(compareAndSetAsync(expect, update)); @@ -367,6 +384,29 @@ public class RedissonJsonBucket extends RedissonExpirable implements RJsonBuc Collections.singletonList(getRawName()), encode(value), timeUnit.toMillis(timeToLive)); } + @Override + public V getAndSet(V value, Duration duration) { + return get(getAndSetAsync(value, duration)); + } + + @Override + public RFuture getAndSetAsync(V value, Duration duration) { + if (value == null) { + return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_OBJECT, + "local v = redis.call('json.get', KEYS[1]); " + + "redis.call('json.del', KEYS[1]); " + + "return v", + Collections.singletonList(getRawName())); + } + + return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_OBJECT, + "local currValue = redis.call('json.get', KEYS[1]); " + + "redis.call('json.set', KEYS[1], '$', ARGV[1]); " + + "redis.call('pexpire', KEYS[1], ARGV[2]); " + + "return currValue; ", + Collections.singletonList(getRawName()), encode(value), duration.toMillis()); + } + @Override public V getAndExpire(Duration duration) { return get(getAndExpireAsync(duration)); @@ -442,6 +482,19 @@ public class RedissonJsonBucket extends RedissonExpirable implements RJsonBuc Collections.singletonList(getRawName()), encode(value), timeUnit.toMillis(timeToLive)); } + @Override + public void set(V value, Duration duration) { + get(setAsync(value, duration)); + } + + @Override + public RFuture setAsync(V value, Duration duration) { + return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_VOID, + "redis.call('json.set', KEYS[1], '$', ARGV[1]); " + + "redis.call('pexpire', KEYS[1], ARGV[2]); ", + Collections.singletonList(getRawName()), encode(value), duration.toMillis()); + } + @Override public void setAndKeepTTL(V value) { get(setAndKeepTTLAsync(value)); diff --git a/redisson/src/main/java/org/redisson/api/RBucket.java b/redisson/src/main/java/org/redisson/api/RBucket.java index f59f68389..70d503ee6 100644 --- a/redisson/src/main/java/org/redisson/api/RBucket.java +++ b/redisson/src/main/java/org/redisson/api/RBucket.java @@ -100,7 +100,7 @@ public interface RBucket extends RExpirable, RBucketAsync { boolean setIfExists(V value); /** - * Sets value only if object holder already exists. + * Use {@link #setIfExists(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -108,8 +108,19 @@ public interface RBucket extends RExpirable, RBucketAsync { * @return {@code true} if successful, or {@code false} if * element wasn't set */ + @Deprecated boolean setIfExists(V value, long timeToLive, TimeUnit timeUnit); + /** + * Sets value with expiration duration only if object holder already exists. + * + * @param value value to set + * @param duration expiration duration + * @return {@code true} if successful, or {@code false} if + * element wasn't set + */ + boolean setIfExists(V value, Duration duration); + /** * Atomically sets the value to the given updated value * only if serialized state of the current value equals @@ -131,15 +142,26 @@ public interface RBucket extends RExpirable, RBucketAsync { V getAndSet(V newValue); /** - * Retrieves current element in the holder and replaces it with newValue with defined timeToLive interval. + * Use {@link #getAndSet(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval * @param timeUnit - unit of time to live interval * @return previous value */ + @Deprecated V getAndSet(V value, long timeToLive, TimeUnit timeUnit); + /** + * Retrieves current element in the holder and replaces it + * with value with defined expiration duration. + * + * @param value value to set + * @param duration expiration duration + * @return previous value + */ + V getAndSet(V value, Duration duration); + /** * Retrieves current element in the holder and sets an expiration duration for it. *

@@ -177,14 +199,23 @@ public interface RBucket extends RExpirable, RBucketAsync { void set(V value); /** - * Stores element into the holder with defined timeToLive interval. + * Use {@link #set(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval * @param timeUnit - unit of time to live interval */ + @Deprecated void set(V value, long timeToLive, TimeUnit timeUnit); + /** + * Stores value into the holder with defined expiration duration. + * + * @param value value to set + * @param duration expiration duration + */ + void set(V value, Duration duration); + /** * Set value and keep existing TTL. *

diff --git a/redisson/src/main/java/org/redisson/api/RBucketAsync.java b/redisson/src/main/java/org/redisson/api/RBucketAsync.java index 45d97ab56..8e3c186e9 100644 --- a/redisson/src/main/java/org/redisson/api/RBucketAsync.java +++ b/redisson/src/main/java/org/redisson/api/RBucketAsync.java @@ -100,7 +100,7 @@ public interface RBucketAsync extends RExpirableAsync { RFuture setIfExistsAsync(V value); /** - * Sets value only if it's already exists. + * Use {@link #setIfExistsAsync(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -110,6 +110,16 @@ public interface RBucketAsync extends RExpirableAsync { */ RFuture setIfExistsAsync(V value, long timeToLive, TimeUnit timeUnit); + /** + * Sets value with expiration duration only if object holder already exists. + * + * @param value value to set + * @param duration expiration duration + * @return {@code true} if successful, or {@code false} if + * element wasn't set + */ + RFuture setIfExistsAsync(V value, Duration duration); + /** * Atomically sets the value to the given updated value * only if serialized state of the current value equals @@ -131,7 +141,7 @@ public interface RBucketAsync extends RExpirableAsync { RFuture getAndSetAsync(V newValue); /** - * Retrieves current element in the holder and replaces it with newValue with defined timeToLive interval. + * Use {@link #getAndSetAsync(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -140,6 +150,16 @@ public interface RBucketAsync extends RExpirableAsync { */ RFuture getAndSetAsync(V value, long timeToLive, TimeUnit timeUnit); + /** + * Retrieves current element in the holder and replaces it + * with value with defined expiration duration. + * + * @param value value to set + * @param duration expiration duration + * @return previous value + */ + RFuture getAndSetAsync(V value, Duration duration); + /** * Retrieves current element in the holder and sets an expiration duration for it. *

@@ -178,7 +198,7 @@ public interface RBucketAsync extends RExpirableAsync { RFuture setAsync(V value); /** - * Stores element into the holder with defined timeToLive interval. + * Use {@link #setAsync(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -187,6 +207,14 @@ public interface RBucketAsync extends RExpirableAsync { */ RFuture setAsync(V value, long timeToLive, TimeUnit timeUnit); + /** + * Stores value into the holder with defined expiration duration. + * + * @param value value to set + * @param duration expiration duration + */ + RFuture setAsync(V value, Duration duration); + /** * Set value and keep existing TTL. *

diff --git a/redisson/src/main/java/org/redisson/api/RBucketReactive.java b/redisson/src/main/java/org/redisson/api/RBucketReactive.java index bcc8f3ef0..a612898b6 100644 --- a/redisson/src/main/java/org/redisson/api/RBucketReactive.java +++ b/redisson/src/main/java/org/redisson/api/RBucketReactive.java @@ -89,7 +89,7 @@ public interface RBucketReactive extends RExpirableReactive { Mono setIfExists(V value); /** - * Sets value only if it's already exists. + * Use {@link #setIfExists(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -97,8 +97,19 @@ public interface RBucketReactive extends RExpirableReactive { * @return {@code true} if successful, or {@code false} if * element wasn't set */ + @Deprecated Mono setIfExists(V value, long timeToLive, TimeUnit timeUnit); + /** + * Sets value with expiration duration only if object holder already exists. + * + * @param value value to set + * @param duration expiration duration + * @return {@code true} if successful, or {@code false} if + * element wasn't set + */ + Mono setIfExists(V value, Duration duration); + /** * Atomically sets the value to the given updated value * only if serialized state of the current value equals @@ -120,15 +131,26 @@ public interface RBucketReactive extends RExpirableReactive { Mono getAndSet(V newValue); /** - * Retrieves current element in the holder and replaces it with newValue with defined timeToLive interval. + * Use {@link #getAndSet(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval * @param timeUnit - unit of time to live interval * @return previous value */ + @Deprecated Mono getAndSet(V value, long timeToLive, TimeUnit timeUnit); + /** + * Retrieves current element in the holder and replaces it + * with value with defined expiration duration. + * + * @param value value to set + * @param duration expiration duration + * @return previous value + */ + Mono getAndSet(V value, Duration duration); + /** * Retrieves current element in the holder and sets an expiration duration for it. *

@@ -181,15 +203,24 @@ public interface RBucketReactive extends RExpirableReactive { Mono set(V value); /** - * Stores element into the holder with defined timeToLive interval. + * Use {@link #set(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval * @param timeUnit - unit of time to live interval * @return void */ + @Deprecated Mono set(V value, long timeToLive, TimeUnit timeUnit); + /** + * Stores value into the holder with defined expiration duration. + * + * @param value value to set + * @param duration expiration duration + */ + Mono set(V value, Duration duration); + /** * Set value and keep existing TTL. *

diff --git a/redisson/src/main/java/org/redisson/api/RBucketRx.java b/redisson/src/main/java/org/redisson/api/RBucketRx.java index d7dc87ddb..c48fe4a9a 100644 --- a/redisson/src/main/java/org/redisson/api/RBucketRx.java +++ b/redisson/src/main/java/org/redisson/api/RBucketRx.java @@ -91,7 +91,7 @@ public interface RBucketRx extends RExpirableRx { Single setIfExists(V value); /** - * Sets value only if it's already exists. + * Use {@link #setIfExists(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval @@ -99,8 +99,19 @@ public interface RBucketRx extends RExpirableRx { * @return {@code true} if successful, or {@code false} if * element wasn't set */ + @Deprecated Single setIfExists(V value, long timeToLive, TimeUnit timeUnit); + /** + * Sets value with expiration duration only if object holder already exists. + * + * @param value value to set + * @param duration expiration duration + * @return {@code true} if successful, or {@code false} if + * element wasn't set + */ + Single setIfExists(V value, Duration duration); + /** * Atomically sets the value to the given updated value * only if serialized state of the current value equals @@ -122,15 +133,26 @@ public interface RBucketRx extends RExpirableRx { Maybe getAndSet(V newValue); /** - * Retrieves current element in the holder and replaces it with newValue with defined timeToLive interval. + * Use {@link #getAndSet(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval * @param timeUnit - unit of time to live interval * @return previous value */ + @Deprecated Maybe getAndSet(V value, long timeToLive, TimeUnit timeUnit); + /** + * Retrieves current element in the holder and replaces it + * with value with defined expiration duration. + * + * @param value value to set + * @param duration expiration duration + * @return previous value + */ + Maybe getAndSet(V value, Duration duration); + /** * Retrieves current element in the holder and sets an expiration duration for it. *

@@ -183,15 +205,24 @@ public interface RBucketRx extends RExpirableRx { Completable set(V value); /** - * Stores element into the holder with defined timeToLive interval. + * Use {@link #set(Object, Duration)} instead * * @param value - value to set * @param timeToLive - time to live interval * @param timeUnit - unit of time to live interval * @return void */ + @Deprecated Completable set(V value, long timeToLive, TimeUnit timeUnit); + /** + * Stores value into the holder with defined expiration duration. + * + * @param value value to set + * @param duration expiration duration + */ + Completable set(V value, Duration duration); + /** * Set value and keep existing TTL. *

diff --git a/redisson/src/test/java/org/redisson/RedissonBucketTest.java b/redisson/src/test/java/org/redisson/RedissonBucketTest.java index dbe00d171..bc0824e18 100755 --- a/redisson/src/test/java/org/redisson/RedissonBucketTest.java +++ b/redisson/src/test/java/org/redisson/RedissonBucketTest.java @@ -30,7 +30,7 @@ public class RedissonBucketTest extends BaseTest { Assumptions.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("6.2.0") > 0); RBucket al = redisson.getBucket("test"); - al.set(1, 1, TimeUnit.SECONDS); + al.set(1, Duration.ofSeconds(1)); assertThat(al.getAndClearExpire()).isEqualTo(1); assertThat(al.remainTimeToLive()).isEqualTo(-1); } @@ -72,7 +72,7 @@ public class RedissonBucketTest extends BaseTest { Assumptions.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("6.0.0") > 0); RBucket al = redisson.getBucket("test"); - al.set(1234, 10, TimeUnit.SECONDS); + al.set(1234, Duration.ofSeconds(10)); al.setAndKeepTTL(222); assertThat(al.remainTimeToLive()).isGreaterThan(9900); assertThat(al.get()).isEqualTo(222); @@ -167,7 +167,7 @@ public class RedissonBucketTest extends BaseTest { RedissonClient redisson = Redisson.create(config); RBucket al = redisson.getBucket("test"); - al.set(1, 3, TimeUnit.SECONDS); + al.set(1, Duration.ofSeconds(3)); CountDownLatch latch = new CountDownLatch(1); al.addListener(new ExpiredObjectListener() { @Override @@ -264,7 +264,7 @@ public class RedissonBucketTest extends BaseTest { public void testGetAndSetTTL() throws InterruptedException { RBucket r1 = redisson.getBucket("getAndSetTTL"); r1.set("value1"); - assertThat(r1.getAndSet("value2", 500, TimeUnit.MILLISECONDS)).isEqualTo("value1"); + assertThat(r1.getAndSet("value2", Duration.ofMillis(500))).isEqualTo("value1"); assertThat(r1.get()).isEqualTo("value2"); Thread.sleep(1000); @@ -295,7 +295,7 @@ public class RedissonBucketTest extends BaseTest { RBucket r2 = redisson.getBucket("test2"); r2.set("1"); - assertThat(r2.setIfExists("2", 1, TimeUnit.SECONDS)).isTrue(); + assertThat(r2.setIfExists("2", Duration.ofSeconds(1))).isTrue(); assertThat(r2.get()).isEqualTo("2"); Thread.sleep(1000); assertThat(r2.isExists()).isFalse(); @@ -324,7 +324,7 @@ public class RedissonBucketTest extends BaseTest { @Test public void testExpire() throws InterruptedException { RBucket bucket = redisson.getBucket("test1"); - bucket.set("someValue", 1, TimeUnit.SECONDS); + bucket.set("someValue", Duration.ofSeconds(1)); Thread.sleep(1100); @@ -425,7 +425,7 @@ public class RedissonBucketTest extends BaseTest { Assertions.assertEquals(value, bucket.get()); bucket.set(null); - bucket.set(null, 1, TimeUnit.DAYS); + bucket.set(null, Duration.ofDays(1)); assertThat(bucket.isExists()).isFalse(); }