Feature - cachedKeySet, cachedValues, cachedEntrySet and getCachedMap methods added to RLocalCachedMap object. #1823

pull/1856/head
Nikita Koksharov 6 years ago
parent 4e1331fde8
commit 1bd6de829b

@ -18,7 +18,9 @@ package org.redisson;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.AbstractCollection;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -1091,4 +1093,185 @@ public class RedissonLocalCachedMap<K, V> extends RedissonMap<K, V> implements R
} }
} }
@Override
public Set<K> cachedKeySet() {
return new LocalKeySet();
}
class LocalKeySet extends AbstractSet<K> {
@Override
public Iterator<K> iterator() {
return new Iterator<K>() {
Iterator<CacheValue> iter = cache.values().iterator();
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public K next() {
return (K) iter.next().getKey();
}
};
}
@Override
public boolean contains(Object o) {
CacheKey cacheKey = toCacheKey(o);
return cache.containsKey(cacheKey);
}
@Override
public boolean remove(Object o) {
CacheKey cacheKey = toCacheKey(o);
return cache.remove(cacheKey) != null;
}
@Override
public int size() {
return cache.size();
}
@Override
public void clear() {
cache.clear();
}
}
@Override
public Collection<V> cachedValues() {
return new LocalValues();
}
final class LocalValues extends AbstractCollection<V> {
@Override
public Iterator<V> iterator() {
return new Iterator<V>() {
Iterator<CacheValue> iter = cache.values().iterator();
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public V next() {
return (V) iter.next().getValue();
}
};
}
@Override
public boolean contains(Object o) {
CacheValue cacheValue = new CacheValue(null, o);
return cache.containsValue(cacheValue);
}
@Override
public int size() {
return cache.size();
}
@Override
public void clear() {
cache.clear();
}
}
@Override
public Set<Entry<K, V>> cachedEntrySet() {
return new LocalEntrySet();
}
final class LocalEntrySet extends AbstractSet<Map.Entry<K,V>> {
@Override
public Iterator<Map.Entry<K,V>> iterator() {
return new Iterator<Map.Entry<K,V>>() {
Iterator<CacheValue> iter = cache.values().iterator();
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public Map.Entry<K,V> next() {
CacheValue e = iter.next();
return new AbstractMap.SimpleEntry<K, V>((K)e.getKey(), (V)e.getValue());
}
};
}
@Override
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
CacheKey cacheKey = toCacheKey(e.getKey());
CacheValue entry = cache.get(cacheKey);
return entry != null && entry.getValue().equals(e.getValue());
}
@Override
public boolean remove(Object o) {
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
CacheKey cacheKey = toCacheKey(e.getKey());
return cache.remove(cacheKey, new CacheValue(e.getKey(), e.getValue()));
}
return false;
}
@Override
public int size() {
return cache.size();
}
@Override
public void clear() {
cache.clear();
}
}
@Override
public Map<K, V> getCachedMap() {
return new LocalMap();
}
final class LocalMap extends AbstractMap<K,V> {
@Override
public V get(Object key) {
CacheKey cacheKey = toCacheKey(key);
CacheValue e = cache.get(cacheKey);
if (e != null) {
return (V) e.getValue();
}
return null;
}
@Override
public boolean containsKey(Object key) {
CacheKey cacheKey = toCacheKey(key);
return cache.containsKey(cacheKey);
}
@Override
public boolean containsValue(Object value) {
CacheValue cacheValue = new CacheValue(null, value);
return cache.containsValue(cacheValue);
}
@Override
public Set<Entry<K, V>> entrySet() {
return RedissonLocalCachedMap.this.cachedEntrySet();
}
}
} }

@ -15,6 +15,10 @@
*/ */
package org.redisson.api; package org.redisson.api;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
/** /**
* Map object with local entry cache support. * Map object with local entry cache support.
* <p> * <p>
@ -46,4 +50,33 @@ public interface RLocalCachedMap<K, V> extends RMap<K, V>, RDestroyable {
* Clears local cache across all instances * Clears local cache across all instances
*/ */
void clearLocalCache(); void clearLocalCache();
/**
* Returns all keys stored in local cache
*
* @return keys
*/
Set<K> cachedKeySet();
/**
* Returns all values stored in local cache
*
* @return values
*/
Collection<V> cachedValues();
/**
* Returns all map entries stored in local cache
*
* @return entries
*/
Set<Entry<K, V>> cachedEntrySet();
/**
* Returns state of local cache
*
* @return map
*/
Map<K, V> getCachedMap();
} }

@ -15,7 +15,10 @@
*/ */
package org.redisson.transaction; package org.redisson.transaction;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.redisson.api.RFuture; import org.redisson.api.RFuture;
@ -57,4 +60,24 @@ public class RedissonTransactionalLocalCachedMap<K, V> extends RedissonTransacti
throw new UnsupportedOperationException("clearLocalCache method is not supported in transaction"); throw new UnsupportedOperationException("clearLocalCache method is not supported in transaction");
} }
@Override
public Set<K> cachedKeySet() {
throw new UnsupportedOperationException("cachedKeySet method is not supported in transaction");
}
@Override
public Collection<V> cachedValues() {
throw new UnsupportedOperationException("cachedValues method is not supported in transaction");
}
@Override
public Set<Entry<K, V>> cachedEntrySet() {
throw new UnsupportedOperationException("cachedEntrySet method is not supported in transaction");
}
@Override
public Map<K, V> getCachedMap() {
throw new UnsupportedOperationException("getCachedMap method is not supported in transaction");
}
} }

@ -271,6 +271,23 @@ public class RedissonLocalCachedMapTest extends BaseMapTest {
assertThat(cache2.size()).isEqualTo(2); assertThat(cache2.size()).isEqualTo(2);
} }
@Test
public void testLocalCacheState() throws InterruptedException {
LocalCachedMapOptions<String, String> options = LocalCachedMapOptions.<String, String>defaults()
.evictionPolicy(EvictionPolicy.LFU)
.cacheSize(5)
.syncStrategy(SyncStrategy.INVALIDATE);
RLocalCachedMap<String, String> map = redisson.getLocalCachedMap("test", options);
map.put("1", "11");
map.put("2", "22");
assertThat(map.cachedKeySet()).containsExactly("1", "2");
assertThat(map.cachedValues()).containsExactlyInAnyOrder("11", "22");
assertThat(map.getCachedMap().keySet()).containsExactly("1", "2");
assertThat(map.getCachedMap().values()).containsExactly("11", "22");
}
@Test @Test
public void testLocalCacheClear() throws InterruptedException { public void testLocalCacheClear() throws InterruptedException {
LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults() LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults()

Loading…
Cancel
Save