public class LimitedCache<T> extends java.util.LinkedHashMap<java.lang.Object,T> implements Cache<T>
LimitedCache
interface is used to represent a
cache that will store key value pairs. This implementation is
backed by a LinkedHashMap
so that only a specific
number of elements can be stored in the cache at one time.Constructor and Description |
---|
LimitedCache()
Constructor of the
LimitedCache object. |
LimitedCache(int capacity)
Constructor of the
LimitedCache object. |
Modifier and Type | Method and Description |
---|---|
void |
cache(java.lang.Object key,
T value)
This method is used to insert a key value mapping in to the
cache.
|
boolean |
contains(java.lang.Object key)
This is used to determine whether the specified key exists
with in the cache.
|
T |
fetch(java.lang.Object key)
This method is used to get the value from the cache that is
mapped to the specified key.
|
protected boolean |
removeEldestEntry(java.util.Map.Entry<java.lang.Object,T> entry)
This is used to remove the eldest entry from the cache.
|
T |
take(java.lang.Object key)
This is used to exclusively take the value mapped to the
specified key from the cache.
|
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, values
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
public LimitedCache()
LimitedCache
object. This is
used to create a cache with a fixed size. The strategy for
this cache is least recently used. Any insert or fetch from
the cache is considered to be a use.public LimitedCache(int capacity)
LimitedCache
object. This is
used to create a cache with a fixed size. The strategy for
this cache is least recently used. Any insert or fetch from
the cache is considered to be a use.capacity
- this is the capacity of the cache objectpublic void cache(java.lang.Object key, T value)
public T take(java.lang.Object key)
public T fetch(java.lang.Object key)
public boolean contains(java.lang.Object key)
protected boolean removeEldestEntry(java.util.Map.Entry<java.lang.Object,T> entry)
removeEldestEntry
in class java.util.LinkedHashMap<java.lang.Object,T>
entry
- this is the eldest entry that can be removed