Merge pull request #6177 from lyrric/master

Feature - add computeIfAbsent with TTL method to RMapCache #6136
pull/6179/head
Nikita Koksharov 6 months ago committed by GitHub
commit 86473b9cde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -128,6 +128,35 @@ public class RedissonMapCache<K, V> extends RedissonMap<K, V> implements RMapCac
Collections.singletonList(optionsName), maxSize, mode);
}
@Override
public V computeIfAbsent(K key, Duration ttl, Function<? super K, ? extends V> mappingFunction) {
checkNotBatch();
checkKey(key);
Objects.requireNonNull(mappingFunction);
V value = get(key);
if (value != null) {
return value;
}
RLock lock = getLock(key);
lock.lock();
try {
value = get(key);
if (value == null) {
V newValue = mappingFunction.apply(key);
if (newValue != null) {
fastPut(key, newValue, ttl.toMillis(), TimeUnit.MILLISECONDS);
return newValue;
}
return null;
}
return value;
} finally {
lock.unlock();
}
}
@Override
public void setMaxSize(int maxSize) {
get(setMaxSizeAsync(maxSize));

@ -23,6 +23,7 @@ import java.time.Duration;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
/**
* <p>Map-based cache with ability to set TTL for each entry via
@ -81,7 +82,20 @@ public interface RMapCache<K, V> extends RMap<K, V>, RMapCacheAsync<K, V> {
* @return <code>true</code> if max size has been successfully set, otherwise <code>false</code>.
*/
boolean trySetMaxSize(int maxSize, EvictionMode mode);
/**
* If the specified key is not already associated
* with a value, attempts to compute its value using the given mapping function and enters it into this map .
* <p>
* Stores value mapped by key with specified time to live.
* Entry expires after specified time to live.
*
* @param key - map key
* @param ttl - time to live for key\value entry.
* If <code>0</code> then stores infinitely.
* @param mappingFunction the mapping function to compute a value
* @return current associated value
*/
V computeIfAbsent(K key, Duration ttl, Function<? super K, ? extends V> mappingFunction);
/**
* If the specified key is not already associated
* with a value, associate it with the given value.

@ -24,6 +24,7 @@ import java.time.Duration;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
/**
* <p>Map-based cache with ability to set TTL for each entry via
@ -126,7 +127,20 @@ public interface RMapCacheReactive<K, V> extends RMapReactive<K, V>, RDestroyabl
* @return previous associated value
*/
Mono<V> putIfAbsent(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit);
/**
* If the specified key is not already associated
* with a value, attempts to compute its value using the given mapping function and enters it into this map .
* <p>
* Stores value mapped by key with specified time to live.
* Entry expires after specified time to live.
*
* @param key - map key
* @param ttl - time to live for key\value entry.
* If <code>0</code> then stores infinitely.
* @param mappingFunction the mapping function to compute a value
* @return current associated value
*/
Mono<V> computeIfAbsentAsync(K key, Duration ttl, Function<? super K, ? extends V> mappingFunction);
/**
* Stores value mapped by key with specified time to live.
* Entry expires after specified time to live.

@ -26,6 +26,7 @@ import java.time.Duration;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
/**
* <p>Map-based cache with ability to set TTL for each entry via
@ -128,7 +129,20 @@ public interface RMapCacheRx<K, V> extends RMapRx<K, V>, RDestroyable {
* @return previous associated value
*/
Maybe<V> putIfAbsent(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit);
/**
* If the specified key is not already associated
* with a value, attempts to compute its value using the given mapping function and enters it into this map .
* <p>
* Stores value mapped by key with specified time to live.
* Entry expires after specified time to live.
*
* @param key - map key
* @param ttl - time to live for key\value entry.
* If <code>0</code> then stores infinitely.
* @param mappingFunction the mapping function to compute a value
* @return current associated value
*/
Maybe<V> computeIfAbsentAsync(K key, Duration ttl, Function<? super K, ? extends V> mappingFunction);
/**
* Stores value mapped by key with specified time to live.
* Entry expires after specified time to live.

@ -1470,5 +1470,21 @@ public class RedissonMapCacheTest extends BaseMapTest {
map.destroy();
}
@Test
public void testComputeIfAbsentWithTTL() throws Exception{
RMapCache<String, String> map = redisson.getMapCache("testMap");
map.delete();
Duration duration = Duration.ofSeconds(1);
String value = map.computeIfAbsent("key1",duration, (t1) -> "value1");
assertThat("value1".equals(value)).isTrue();
value = map.computeIfAbsent("key1", duration, (t1) -> "value2");
assertThat("value2".equals(value)).isFalse();
Thread.sleep(1100);
value = map.computeIfAbsent("key1", duration, (t1) -> "value3");
assertThat("value3".equals(value)).isTrue();
map.destroy();
}
}

Loading…
Cancel
Save