Merge pull request #3293 from ipalbeniz/feature-local-cache-null-values

Feature - Added option to allow null values in a local cache #3221
pull/3332/head^2
Nikita Koksharov 4 years ago committed by GitHub
commit 253e2f6111
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -61,6 +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 storeCacheMiss;
private LocalCacheListener listener;
private LocalCacheView<K, V> localCacheView;
@ -80,6 +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();
storeCacheMiss = options.isStoreCacheMiss();
listener = new LocalCacheListener(name, commandExecutor, this, codec, options, cacheUpdateLogTime) {
@ -212,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 && cacheValue.getValue() != null) {
if (cacheValue != null && (storeCacheMiss || cacheValue.getValue() != null)) {
return RedissonPromise.newSucceededFuture((V) cacheValue.getValue());
}
@ -226,7 +228,7 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
return;
}
if (value != null) {
if (storeCacheMiss || value != null) {
cachePut(cacheKey, key, value);
}
});
@ -244,7 +246,7 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
return result;
}
@Override
protected RFuture<V> putOperationAsync(K key, V value) {
ByteBuf mapKey = encodeMapKey(key);

@ -136,6 +136,7 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
private long maxIdleInMillis;
private CacheProvider cacheProvider;
private StoreMode storeMode;
private boolean storeCacheMiss;
protected LocalCachedMapOptions() {
}
@ -149,6 +150,7 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
this.maxIdleInMillis = copy.maxIdleInMillis;
this.cacheProvider = copy.cacheProvider;
this.storeMode = copy.storeMode;
this.storeCacheMiss = copy.storeCacheMiss;
}
/**
@ -161,7 +163,8 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
* .evictionPolicy(EvictionPolicy.NONE)
* .reconnectionStrategy(ReconnectionStrategy.NONE)
* .cacheProvider(CacheProvider.REDISSON)
* .syncStrategy(SyncStrategy.INVALIDATE);
* .syncStrategy(SyncStrategy.INVALIDATE)
* .storeCacheMiss(false);
* </pre>
*
* @param <K> key type
@ -177,7 +180,8 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
.reconnectionStrategy(ReconnectionStrategy.NONE)
.cacheProvider(CacheProvider.REDISSON)
.storeMode(StoreMode.LOCALCACHE_REDIS)
.syncStrategy(SyncStrategy.INVALIDATE);
.syncStrategy(SyncStrategy.INVALIDATE)
.storeCacheMiss(false);
}
public CacheProvider getCacheProvider() {
@ -334,6 +338,21 @@ public class LocalCachedMapOptions<K, V> extends MapOptions<K, V> {
return this;
}
public boolean isStoreCacheMiss() {
return this.storeCacheMiss;
}
/**
* Defines whether to store a cache miss into the local cache.
*
* @param storeCacheMiss - whether to store a cache miss into the local cache
* @return LocalCachedMapOptions instance
*/
public LocalCachedMapOptions<K, V> storeCacheMiss(boolean storeCacheMiss) {
this.storeCacheMiss = storeCacheMiss;
return this;
}
@Override
public LocalCachedMapOptions<K, V> writeBehindBatchSize(int writeBehindBatchSize) {
return (LocalCachedMapOptions<K, V>) super.writeBehindBatchSize(writeBehindBatchSize);

@ -9,6 +9,8 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.Assert;
import org.junit.Test;
import org.redisson.api.LocalCachedMapOptions;
@ -466,7 +468,29 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
assertThat(map1.get("14")).isEqualTo(2);
assertThat(map1.get("15")).isEqualTo(3);
}
@Test
public void testGetStoringCacheMiss() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.<String, Integer>defaults().storeCacheMiss(true));
Map<String, Integer> cache = map.getCachedMap();
assertThat(map.get("19")).isNull();
Awaitility.await().atMost(Durations.ONE_SECOND)
.untilAsserted(() -> assertThat(cache.size()).isEqualTo(1));
}
@Test
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();
Awaitility.await().atMost(Durations.ONE_SECOND)
.untilAsserted(() -> assertThat(cache.size()).isEqualTo(0));
}
@Test
public void testGetAllCache() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("getAll", LocalCachedMapOptions.defaults());

Loading…
Cancel
Save