Feature - Added option to store cache miss in a local cache #3221

Signed-off-by: Iñaki Perez <ipalbeniz@gmail.com>
pull/3293/head
Iñaki Perez 4 years ago
parent ad59e48aa7
commit 87b866afe3

@ -61,7 +61,7 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
private int invalidateEntryOnChange;
private SyncStrategy syncStrategy;
private LocalCachedMapOptions.StoreMode storeMode;
private boolean allowNullValues;
private boolean storeCacheMiss;
private LocalCacheListener listener;
private LocalCacheView<K, V> localCacheView;
@ -81,7 +81,7 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
private void init(String name, LocalCachedMapOptions<K, V> options, RedissonClient redisson, EvictionScheduler evictionScheduler) {
syncStrategy = options.getSyncStrategy();
storeMode = options.getStoreMode();
allowNullValues = options.isAllowNullValues();
storeCacheMiss = options.isStoreCacheMiss();
listener = new LocalCacheListener(name, commandExecutor, this, codec, options, cacheUpdateLogTime) {
@ -214,7 +214,7 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
CacheKey cacheKey = localCacheView.toCacheKey(key);
CacheValue cacheValue = cache.get(cacheKey);
if (cacheValue != null && (allowNullValues || cacheValue.getValue() != null)) {
if (cacheValue != null && (storeCacheMiss || cacheValue.getValue() != null)) {
return RedissonPromise.newSucceededFuture((V) cacheValue.getValue());
}
@ -228,7 +228,7 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
return;
}
if (allowNullValues || value != null) {
if (storeCacheMiss || value != null) {
cachePut(cacheKey, key, value);
}
});
@ -246,13 +246,6 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
return result;
}
@Override
protected void checkValue(Object value) {
if (allowNullValues) {
return;
}
super.checkValue(value);
}
@Override
protected RFuture<V> putOperationAsync(K key, V value) {

@ -136,7 +136,7 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
private long maxIdleInMillis;
private CacheProvider cacheProvider;
private StoreMode storeMode;
private boolean allowNullValues;
private boolean storeCacheMiss;
protected LocalCachedMapOptions() {
}
@ -150,7 +150,7 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
this.maxIdleInMillis = copy.maxIdleInMillis;
this.cacheProvider = copy.cacheProvider;
this.storeMode = copy.storeMode;
this.allowNullValues = copy.allowNullValues;
this.storeCacheMiss = copy.storeCacheMiss;
}
/**
@ -164,7 +164,7 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
* .reconnectionStrategy(ReconnectionStrategy.NONE)
* .cacheProvider(CacheProvider.REDISSON)
* .syncStrategy(SyncStrategy.INVALIDATE)
* .allowNullValues(false);
* .storeCacheMiss(false);
* </pre>
*
* @param <K> key type
@ -181,7 +181,7 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
.cacheProvider(CacheProvider.REDISSON)
.storeMode(StoreMode.LOCALCACHE_REDIS)
.syncStrategy(SyncStrategy.INVALIDATE)
.allowNullValues(false);
.storeCacheMiss(false);
}
public CacheProvider getCacheProvider() {
@ -338,18 +338,18 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
return this;
}
public boolean isAllowNullValues() {
return this.allowNullValues;
public boolean isStoreCacheMiss() {
return this.storeCacheMiss;
}
/**
* Defines whether the local cache allows null values.
* Defines whether to store a cache miss into the local cache.
*
* @param allowNullValues - whether the local cache allows null values
* @param storeCacheMiss - whether to store a cache miss into the local cache
* @return LocalCachedMapOptions instance
*/
public LocalCachedMapOptions<K, V> allowNullValues(boolean allowNullValues) {
this.allowNullValues = allowNullValues;
public LocalCachedMapOptions<K, V> storeCacheMiss(boolean storeCacheMiss) {
this.storeCacheMiss = storeCacheMiss;
return this;
}

@ -1,7 +1,6 @@
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Arrays;
import java.util.HashMap;
@ -471,22 +470,10 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
}
@Test
public void testPutGetAllowingNullValues() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.<String, Integer>defaults().allowNullValues(true));
public void testGetStoringCacheMiss() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.<String, Integer>defaults().storeCacheMiss(true));
Map<String, Integer> cache = map.getCachedMap();
map.put("19", null);
assertThat(map.get("19")).isNull();
Awaitility.await().atMost(Durations.ONE_SECOND)
.untilAsserted(() -> assertThat(cache.size()).isEqualTo(1));
map.remove("19");
Awaitility.await().atMost(Durations.ONE_SECOND)
.untilAsserted(() -> assertThat(cache.size()).isEqualTo(0));
assertThat(map.get("19")).isNull();
assertThat(map.get("19")).isNull();
Awaitility.await().atMost(Durations.ONE_SECOND)
@ -494,19 +481,10 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
}
@Test
public void testPutDisallowingNullValues() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.<String, Integer>defaults().allowNullValues(false));
assertThatThrownBy(() -> map.put("19", null))
.isInstanceOf(NullPointerException.class);
}
@Test
public void testGetDisallowingNullValues() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.<String, Integer>defaults().allowNullValues(false));
public void testGetNotStoringCacheMiss() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.<String, Integer>defaults().storeCacheMiss(false));
Map<String, Integer> cache = map.getCachedMap();
assertThat(map.get("19")).isNull();
assertThat(map.get("19")).isNull();
Awaitility.await().atMost(Durations.ONE_SECOND)

Loading…
Cancel
Save