Feature - add addIfAbsent(Map<V, Duration> objects) method to RSetCache,RSetCacheRx,RSetCacheReactive

Signed-off-by: lyrric <xx20510@163.com>
pull/6145/head
lyrric 6 months ago
parent 8128b209b4
commit 9211ef1923

@ -1273,6 +1273,11 @@ public class RedissonSetCache<V> extends RedissonExpirable implements RSetCache<
return get(addAllIfAbsentAsync(objects));
}
@Override
public boolean addIfAbsent(Map<V, Duration> objects) {
return get(addIfAbsentAsync(objects));
}
@Override
public int addAllIfExist(Map<V, Duration> objects) {
return get(addAllIfExistAsync(objects));
@ -1314,7 +1319,33 @@ public class RedissonSetCache<V> extends RedissonExpirable implements RSetCache<
"return result; ",
Arrays.asList(getRawName()), params.toArray());
}
@Override
public RFuture<Boolean> addIfAbsentAsync(Map<V, Duration> objects) {
List<Object> params = new ArrayList<>();
long currentTime = System.currentTimeMillis();
params.add(currentTime);
for (Map.Entry<V, Duration> entry : objects.entrySet()) {
long timeoutDate = currentTime + entry.getValue().toMillis();
if (entry.getValue().isZero()) {
timeoutDate = 92233720368547758L - currentTime;
}
params.add(timeoutDate);
encode(params, entry.getKey());
}
return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN,
"for i=2, #ARGV, 2 do " +
"local expireDateScore = redis.call('zscore', KEYS[1], ARGV[i+1]); " +
"if expireDateScore ~= false and tonumber(expireDateScore) > tonumber(ARGV[1]) then " +
"return 0; " +
"end; " +
"end; " +
"for i=2, #ARGV, 2 do " +
"redis.call('zadd', KEYS[1], ARGV[i], ARGV[i+1]); " +
"end; " +
"return 1; ",
Collections.singletonList(getRawName()), params.toArray());
}
@Override
public RFuture<Integer> addAllIfExistAsync(Map<V, Duration> objects) {
List<Object> params = new ArrayList<>();

@ -62,7 +62,7 @@ public interface RSetCache<V> extends RSet<V>, RExpirable, RSetCacheAsync<V>, RD
int size();
/**
* Use {@link #addIfAbsent(Duration, Object)} instead
* Use {@link #addIfAbsent(Map)} instead
*
* @param values - values to add
* @param ttl - time to live for value.
@ -136,6 +136,15 @@ public interface RSetCache<V> extends RSet<V>, RExpirable, RSetCacheAsync<V>, RD
* @return amount of added elements
*/
int addAllIfAbsent(Map<V, Duration> objects);
/**
* Adds elements to this set only if all of them haven't been added before.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return <code>true</code> if elements added and <code>false</code> if not.
*/
boolean addIfAbsent(Map<V, Duration> objects);
/**
* Adds elements to this set only if they already exist.

@ -52,7 +52,7 @@ public interface RSetCacheAsync<V> extends RSetAsync<V> {
RFuture<Integer> sizeAsync();
/**
* Use {@link #addIfAbsentAsync(Duration, Object)} instead
* Use {@link #addIfAbsentAsync(Map)} instead
*
* @param values - values to add
* @param ttl - time to live for value.
@ -126,6 +126,15 @@ public interface RSetCacheAsync<V> extends RSetAsync<V> {
* @return amount of added elements
*/
RFuture<Integer> addAllIfAbsentAsync(Map<V, Duration> objects);
/**
* Adds elements to this set only if all of them haven't been added before.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return amount of added elements
*/
RFuture<Boolean> addIfAbsentAsync(Map<V, Duration> objects);
/**
* Adds elements to this set only if they already exist.

@ -110,6 +110,8 @@ public interface RSetCacheReactive<V> extends RCollectionReactive<V>, RDestroyab
Mono<Boolean> tryAdd(V... values);
/**
* Use {@link #addIfAbsent(Map)} instead
*
* Tries to add elements only if none of them in set.
*
* @param values - values to add
@ -119,6 +121,7 @@ public interface RSetCacheReactive<V> extends RCollectionReactive<V>, RDestroyab
* @return <code>true</code> if elements successfully added,
* otherwise <code>false</code>.
*/
@Deprecated
Mono<Boolean> tryAdd(long ttl, TimeUnit unit, V... values);
/**
@ -132,6 +135,16 @@ public interface RSetCacheReactive<V> extends RCollectionReactive<V>, RDestroyab
*/
Mono<Boolean> addIfAbsent(Duration ttl, V object);
/**
* Adds elements to this set only if all of them haven't been added before.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return <code>true</code> if elements added and <code>false</code> if not.
*/
Mono<Boolean> addIfAbsent(Map<V, Duration> objects);
/**
* Adds element to this set only if it's already exists.
* <p>

@ -110,7 +110,7 @@ public interface RSetCacheRx<V> extends RCollectionRx<V>, RDestroyable {
Single<Boolean> tryAdd(V... values);
/**
* Use {@link #addIfAbsent(Duration, Object)} instead
* Use {@link #addIfAbsent(Map)} instead
*
* @param values - values to add
* @param ttl - time to live for value.
@ -121,6 +121,15 @@ public interface RSetCacheRx<V> extends RCollectionRx<V>, RDestroyable {
*/
@Deprecated
Single<Boolean> tryAdd(long ttl, TimeUnit unit, V... values);
/**
* Adds elements to this set only if all of them haven't been added before.
* <p>
* Requires <b>Redis 3.0.2 and higher.</b>
*
* @param objects map of elements to add
* @return <code>true</code> if elements added and <code>false</code> if not.
*/
Single<Boolean> addIfAbsent(Map<V, Duration> objects);
/**
* Adds element to this set only if has not been added before.

@ -3,11 +3,8 @@ package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
@ -321,4 +318,23 @@ public class RedissonSetCacheReactiveTest extends BaseReactiveTest {
}
@Test
public void testAddIfAbsentWithMapParam() throws InterruptedException {
sync(redisson.getKeys().flushall());
RSetCacheReactive<String> cache = redisson.getSetCache("cache");
Map<String, Duration> map = new HashMap<>();
map.put("key1", Duration.ofMinutes(1));
map.put("key2", Duration.ofMinutes(1));
assertThat(sync(cache.addIfAbsent(map))).isTrue();
map = new HashMap<>();
map.put("key1", Duration.ofMinutes(1));
assertThat(sync(cache.addIfAbsent(map))).isFalse();
map = new HashMap<>();
map.put("key3", Duration.ofSeconds(1));
assertThat(sync(cache.addIfAbsent(map))).isTrue();
Thread.sleep(1200);
assertThat(sync(cache.addIfAbsent(map))).isTrue();
sync((redisson.getKeys().flushall()));
}
}

@ -697,4 +697,22 @@ public class RedissonSetCacheTest extends RedisDockerTest {
}, NOTIFY_KEYSPACE_EVENTS, "Ez");
}
@Test
public void testAddIfAbsentWithMapParam() throws InterruptedException {
redisson.getKeys().flushall();
RSetCache<String> cache = redisson.getSetCache("cache");
Map<String, Duration> map = new HashMap<>();
map.put("key1", Duration.ofMinutes(1));
map.put("key2", Duration.ofMinutes(1));
assertThat(cache.addIfAbsent(map)).isTrue();
map = new HashMap<>();
map.put("key1", Duration.ofMinutes(1));
assertThat(cache.addIfAbsent(map)).isFalse();
map = new HashMap<>();
map.put("key3", Duration.ofSeconds(1));
assertThat(cache.addIfAbsent(map)).isTrue();
Thread.sleep(1200);
assertThat(cache.addIfAbsent(map)).isTrue();
redisson.getKeys().flushall();
}
}

@ -3,17 +3,15 @@ package org.redisson.rx;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.redisson.TestObject;
import org.redisson.api.RSetCacheReactive;
import org.redisson.api.RSetCacheRx;
public class RedissonSetCacheRxTest extends BaseRxTest {
@ -290,5 +288,23 @@ public class RedissonSetCacheRxTest extends BaseRxTest {
Assertions.assertEquals(0, sync(cache.size()).intValue());
}
@Test
public void testAddIfAbsentWithMapParam() throws InterruptedException {
sync(redisson.getKeys().flushall());
RSetCacheRx<String> cache = redisson.getSetCache("cache");
Map<String, Duration> map = new HashMap<>();
map.put("key1", Duration.ofMinutes(1));
map.put("key2", Duration.ofMinutes(1));
assertThat(sync(cache.addIfAbsent(map))).isTrue();
map = new HashMap<>();
map.put("key1", Duration.ofMinutes(1));
assertThat(sync(cache.addIfAbsent(map))).isFalse();
map = new HashMap<>();
map.put("key3", Duration.ofSeconds(1));
assertThat(sync(cache.addIfAbsent(map))).isTrue();
Thread.sleep(1200);
assertThat(sync(cache.addIfAbsent(map))).isTrue();
sync((redisson.getKeys().flushall()));
}
}

Loading…
Cancel
Save