Package io.kcache.utils
Class InMemoryCache<K,V>
- java.lang.Object
-
- com.google.common.collect.ForwardingObject
-
- com.google.common.collect.ForwardingMap<K,V>
-
- com.google.common.collect.ForwardingSortedMap<K,V>
-
- io.kcache.utils.InMemoryCache<K,V>
-
- All Implemented Interfaces:
Cache<K,V>
,Closeable
,AutoCloseable
,Map<K,V>
,SortedMap<K,V>
,org.apache.kafka.common.Configurable
- Direct Known Subclasses:
InMemoryBoundedCache
public class InMemoryCache<K,V> extends com.google.common.collect.ForwardingSortedMap<K,V> implements Cache<K,V>
In-memory cache
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.common.collect.ForwardingSortedMap
com.google.common.collect.ForwardingSortedMap.StandardKeySet
-
-
Constructor Summary
Constructors Constructor Description InMemoryCache()
InMemoryCache(Comparator<? super K> comparator)
InMemoryCache(NavigableMap<K,V> delegate)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description KeyValueIterator<K,V>
all()
Returns an iterator over all key-value pairs in this cache.void
close()
protected SortedMap<K,V>
delegate()
Cache<K,V>
descendingCache()
Returns a reverse order view of the mappings contained in this cache.void
destroy()
Destroys the cache, if persistent.void
flush()
Flushes the cache.void
init()
Initializes the cache.KeyValueIterator<K,V>
range(K from, boolean fromInclusive, K to, boolean toInclusive)
Returns an iterator over the portion of this cache whose keys range fromfromKey
totoKey
.void
reset()
Resets the cache, clearing stale data before a sync.Cache<K,V>
subCache(K from, boolean fromInclusive, K to, boolean toInclusive)
Returns a view of the portion of this cache whose keys range fromfromKey
totoKey
.void
sync()
Syncs (or re-initializes) the cache with the backing store.-
Methods inherited from class com.google.common.collect.ForwardingSortedMap
comparator, firstKey, headMap, lastKey, standardContainsKey, standardSubMap, subMap, tailMap
-
Methods inherited from class com.google.common.collect.ForwardingMap
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, standardClear, standardContainsValue, standardEquals, standardHashCode, standardIsEmpty, standardPutAll, standardRemove, standardToString, values
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, get, getOrDefault, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size
-
-
-
-
Constructor Detail
-
InMemoryCache
public InMemoryCache()
-
InMemoryCache
public InMemoryCache(Comparator<? super K> comparator)
-
InMemoryCache
public InMemoryCache(NavigableMap<K,V> delegate)
-
-
Method Detail
-
init
public void init()
Description copied from interface:Cache
Initializes the cache.
-
reset
public void reset()
Description copied from interface:Cache
Resets the cache, clearing stale data before a sync. This can be used if the leader changes in a cluster.
-
sync
public void sync()
Description copied from interface:Cache
Syncs (or re-initializes) the cache with the backing store.
-
subCache
public Cache<K,V> subCache(K from, boolean fromInclusive, K to, boolean toInclusive)
Description copied from interface:Cache
Returns a view of the portion of this cache whose keys range fromfromKey
totoKey
. IffromKey
andtoKey
are equal, the returned cache is empty unlessfromInclusive
andtoInclusive
are both true. The returned cache is backed by this cache, so changes in the returned cache are reflected in this cache, and vice-versa. The returned cache supports all optional cache operations that this cache supports.The returned cache will throw an
IllegalArgumentException
on an attempt to insert a key outside of its range, or to construct a subcache either of whose endpoints lie outside its range.- Specified by:
subCache
in interfaceCache<K,V>
- Parameters:
from
- low endpoint of the keys in the returned cache;null
indicates the beginningfromInclusive
-true
if the low endpoint is to be included in the returned viewto
- high endpoint of the keys in the returned cache;null
indicates the endtoInclusive
-true
if the high endpoint is to be included in the returned view- Returns:
- a view of the portion of this cache whose keys range from
fromKey
totoKey
-
range
public KeyValueIterator<K,V> range(K from, boolean fromInclusive, K to, boolean toInclusive)
Description copied from interface:Cache
Returns an iterator over the portion of this cache whose keys range fromfromKey
totoKey
. IffromKey
andtoKey
are equal, the returned iterator is empty unlessfromInclusive
andtoInclusive
are both true.- Specified by:
range
in interfaceCache<K,V>
- Parameters:
from
- low endpoint of the keys in the returned iterator;null
indicates the beginningfromInclusive
-true
if the low endpoint is to be included in the returned viewto
- high endpoint of the keys in the returned iterator;null
indicates the endtoInclusive
-true
if the high endpoint is to be included in the returned view- Returns:
- an iterator over the portion of this cache whose keys range from
fromKey
totoKey
-
all
public KeyValueIterator<K,V> all()
Description copied from interface:Cache
Returns an iterator over all key-value pairs in this cache.- Specified by:
all
in interfaceCache<K,V>
- Returns:
- a
KeyValueIterator
over the elements in this collection
-
descendingCache
public Cache<K,V> descendingCache()
Description copied from interface:Cache
Returns a reverse order view of the mappings contained in this cache. The descending cache is backed by this cache, so changes to the cache are reflected in the descending cache, and vice-versa. If either cache is modified while an iteration over a collection view of either cache is in progress (except through the iterator's ownremove
operation), the results of the iteration are undefined.- Specified by:
descendingCache
in interfaceCache<K,V>
- Returns:
- a reverse order view of this cache
-
flush
public void flush()
Description copied from interface:Cache
Flushes the cache.
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
-