Merge branch 'master' of github.com:redisson/redisson

pull/3332/head^2 redisson-3.14.1
Nikita Koksharov 4 years ago
commit 6554da34e8

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

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

@ -9,6 +9,8 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.redisson.api.LocalCachedMapOptions; import org.redisson.api.LocalCachedMapOptions;
@ -466,7 +468,29 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
assertThat(map1.get("14")).isEqualTo(2); assertThat(map1.get("14")).isEqualTo(2);
assertThat(map1.get("15")).isEqualTo(3); 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 @Test
public void testGetAllCache() { public void testGetAllCache() {
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("getAll", LocalCachedMapOptions.defaults()); RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("getAll", LocalCachedMapOptions.defaults());

Loading…
Cancel
Save