Package io.pravega.common.util
Class SimpleCache<KeyT,ValueT>
- java.lang.Object
-
- io.pravega.common.util.SimpleCache<KeyT,ValueT>
-
- Type Parameters:
KeyT- Key Type.ValueT- Value Type.
@ThreadSafe public class SimpleCache<KeyT,ValueT> extends java.lang.ObjectLightweight, thread-safe, key-value pair cache built on top of JavaHashMapthat supports eviction based on a maximum size or last-access-time. Eviction of items is triggered by one of the following: - InvokingcleanUp(). - InvokingputIfAbsent(KeyT, ValueT),put(KeyT, ValueT)orget(KeyT)for a Key that has expired or if thesize()of the cache has exceededgetMaxSize(). Every item access (upon insertion, updating or retrieval) will update the "last-access-time" of that item to the current time. Upon eviction, items will be evicted beginning with the least-accessed one (i.e, the ones that have not been used recently), in order of access time (oldest items first). Every eviction event will remove all expired items and any unexpired items as needed ifsize()exceedsgetMaxSize().
-
-
Constructor Summary
Constructors Constructor Description SimpleCache(int maxSize, @NonNull java.time.Duration expirationTime, java.util.function.BiConsumer<KeyT,ValueT> onExpiration)Creates a new instance of theSimpleCacheclass.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcleanUp()Performs a cleanup of this class.ValueTget(KeyT key)Gets a value associated with the given key.intgetMaxSize()ValueTput(KeyT key, ValueT value)Inserts a new Key-Value pair into the cache.ValueTputIfAbsent(KeyT key, ValueT value)Inserts a Key-Value pair into the cache, but only if the Key is not already present.ValueTremove(KeyT key)Removes any value that is associated with the given key.intsize()Gets a value representing the number of entries in the cache.
-
-
-
Constructor Detail
-
SimpleCache
public SimpleCache(int maxSize, @NonNull @NonNull java.time.Duration expirationTime, @Nullable java.util.function.BiConsumer<KeyT,ValueT> onExpiration)Creates a new instance of theSimpleCacheclass.- Parameters:
maxSize- Maximum number of elements in the cache. Ifsize()exceeds this value as a result of an insertion, the oldest entries will be evicted, in order, untilsize()falls below this value.expirationTime- Maximum amount of time, since the last access of an entry, until such an entry is "expired" and will be evicted. Entries will not be evicted exactly when they expire, rather upon calls tocleanUp()or any other accessor or mutator invocations.onExpiration- (Optional) AConsumerthat will be invoked when an Entry is evicted. This will not be invoked when the entry is replaced (i.e., viaput(KeyT, ValueT)or removed (viaremove(KeyT).
-
-
Method Detail
-
size
public int size()
Gets a value representing the number of entries in the cache. This may include expired entries as well (expired entries will be removed upon invokingcleanUp(), which will also update this value).- Returns:
- The number of entries in the cache.
-
put
public ValueT put(KeyT key, ValueT value)
Inserts a new Key-Value pair into the cache.- Parameters:
key- The key to insert.value- The value to insert.- Returns:
- The previous value associated with this key, or null if no such value exists or if it is expired.
-
putIfAbsent
public ValueT putIfAbsent(KeyT key, ValueT value)
Inserts a Key-Value pair into the cache, but only if the Key is not already present.- Parameters:
key- The key to insert.value- The value to insert.- Returns:
- The value that was already associated with this key. If no such value, or if the value is expired, then null will be returned. If null is returned, then the insertion can be considered successful.
-
remove
public ValueT remove(KeyT key)
Removes any value that is associated with the given key.- Parameters:
key- The key to remove.- Returns:
- The value associated with the given key, or null if no such value exists or if the value is expired.
-
get
public ValueT get(KeyT key)
Gets a value associated with the given key.- Parameters:
key- The key to lookup.- Returns:
- The value associated with the given key, or null if no such value exists or if the value is expired. If the value is expired, it will be automatically removed from the cache.
-
cleanUp
public void cleanUp()
Performs a cleanup of this class. After this method completes, the following will be true: -size()will be less than or equal togetMaxSize(). - All expired entries will be removed.
-
getMaxSize
public int getMaxSize()
-
-