K
- the cache key typeV
- the cache value type@ThreadSafe public abstract class Cache<K,V> extends Object implements Closeable
Modifier and Type | Class and Description |
---|---|
protected class |
Cache.Entry |
Constructor and Description |
---|
Cache(CacheConfiguration conf,
String name,
MetricKey evictionsKey,
MetricKey hitsKey,
MetricKey loadTimesKey,
MetricKey missesKey,
MetricKey sizeKey) |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears all entries from the map.
|
void |
close() |
void |
flush()
Flushes all data to the backing store.
|
protected abstract void |
flushEntries(List<Cache.Entry> candidates)
Attempts to flush the given entries to the backing store.
|
Optional<V> |
get(K key) |
Optional<V> |
get(K key,
ReadOption option)
Retrieves a value from the cache, loading it from the backing store if necessary.
|
protected Map<K,Cache.Entry> |
getCacheMap() |
protected abstract Optional<V> |
load(K key)
Loads a key from the backing store.
|
protected void |
onCacheRemove(K key)
Callback triggered when a key is removed from the cache.
|
protected void |
onCacheUpdate(K key,
V value)
Callback triggered when an update is made to a key/value pair in the cache.
|
protected void |
onPut(K key,
V existingValue,
V value,
boolean isNewKey)
Callback triggered whenever a new key/value pair is added by put(key, value).
|
protected void |
onRemove(K key)
Callback triggered whenever a key is removed by remove(key).
|
void |
put(K key,
V value)
Writes a key/value pair to the cache.
|
void |
putNewEntry(K key,
V value)
Writes a key/value pair to the cache.
|
void |
remove(K key)
Removes a key from the cache.
|
protected abstract void |
removeFromBackingStore(K key)
Removes a key from the backing store.
|
protected abstract void |
writeToBackingStore(K key,
V value)
Writes a key/value pair to the backing store.
|
public Cache(CacheConfiguration conf, String name, MetricKey evictionsKey, MetricKey hitsKey, MetricKey loadTimesKey, MetricKey missesKey, MetricKey sizeKey)
conf
- cache configurationname
- a name for the cacheevictionsKey
- the cache evictions metric keyhitsKey
- the cache hits metrics keyloadTimesKey
- the load times metrics keymissesKey
- the misses metrics keysizeKey
- the size metrics keypublic Optional<V> get(K key, ReadOption option)
key
- the key to get the value foroption
- the read optionspublic Optional<V> get(K key)
key
- the key to get the value forget(Object, ReadOption)
with default optionpublic void putNewEntry(K key, V value)
put(Object, Object)
, but with an added information that
the entry is new.key
- the keyvalue
- the valuepublic void put(K key, V value)
putNewEntry(Object, Object)
.key
- the keyvalue
- the valuepublic void remove(K key)
key
- the key to removepublic void flush() throws InterruptedException
InterruptedException
public void clear()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
protected Map<K,Cache.Entry> getCacheMap()
protected void onCacheUpdate(K key, @Nullable V value)
key
- the updated keyvalue
- the updated value, or null if the key is being removedprotected void onCacheRemove(K key)
key
- the removed keyprotected void onPut(K key, @Nullable V existingValue, V value, boolean isNewKey)
key
- the added keyexistingValue
- the current value if exists, otherwise nullvalue
- the added valueisNewKey
- a user input boolean indicates if the key is a new key or notprotected void onRemove(K key)
key
- the removed keyprotected abstract Optional<V> load(K key)
key
- the key to loadprotected abstract void writeToBackingStore(K key, V value)
key
- the keyvalue
- the valueprotected abstract void removeFromBackingStore(K key)
key
- the keyprotected abstract void flushEntries(List<Cache.Entry> candidates)
candidates
- the candidate entries to flushCopyright © 2023. All Rights Reserved.