RLocalCachedMap.clearLocalCache method added #1297

pull/1300/head
Nikita 7 years ago
parent 387ed744b8
commit a56da0517a

@ -949,6 +949,30 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
cache.put(cacheKey, new CacheValue(entry.getKey(), entry.getValue()));
}
}
@Override
public void clearLocalCache() {
get(clearLocalCacheAsync());
}
@Override
public RFuture<Void> clearLocalCacheAsync() {
final RPromise<Void> result = new RedissonPromise<Void>();
RFuture<Long> future = invalidationTopic.publishAsync(new LocalCachedMapClear());
future.addListener(new FutureListener<Long>() {
@Override
public void operationComplete(Future<Long> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
result.trySuccess(null);
}
});
return result;
}
@Override
public RFuture<Set<Entry<K, V>>> readAllEntrySetAsync() {

@ -27,10 +27,23 @@ package org.redisson.api;
* @param <V> map value
*/
public interface RLocalCachedMap<K, V> extends RMap<K, V>, RDestroyable {
/**
* Pre-warm the cached values. Not guaranteed to load ALL values, but statistically
* will preload approximately all (all if no concurrent mutating activity)
* Intended for use with no-eviction caches where entire maps are locally cached
*/
public void preloadCache();
void preloadCache();
/**
* Clears local cache across all instances
*
* @return void
*/
RFuture<Void> clearLocalCacheAsync();
/**
* Clears local cache across all instances
*/
void clearLocalCache();
}

@ -290,6 +290,37 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
assertThat(cache2.size()).isEqualTo(2);
}
@Test
public void testLocalCacheClear() throws InterruptedException {
LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults()
.evictionPolicy(EvictionPolicy.LFU)
.cacheSize(5)
.syncStrategy(SyncStrategy.INVALIDATE);
RLocalCachedMap<String, Integer> map1 = redisson.getLocalCachedMap("test", options);
Cache<CacheKey, CacheValue> cache1 = Deencapsulation.getField(map1, "cache");
RLocalCachedMap<String, Integer> map2 = redisson.getLocalCachedMap("test", options);
Cache<CacheKey, CacheValue> cache2 = Deencapsulation.getField(map2, "cache");
map1.put("1", 1);
map1.put("2", 2);
assertThat(map2.get("1")).isEqualTo(1);
assertThat(map2.get("2")).isEqualTo(2);
assertThat(cache1.size()).isEqualTo(2);
assertThat(cache2.size()).isEqualTo(2);
map1.clearLocalCache();
Thread.sleep(50);
assertThat(cache1.size()).isZero();
assertThat(cache2.size()).isZero();
}
@Test
public void testNoInvalidationOnRemove() throws InterruptedException {
LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults()

Loading…
Cancel
Save