public class LimitedCache<T> extends LinkedHashMap<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.AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
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(Object key,
T value)
This method is used to insert a key value mapping in to the
cache.
|
boolean |
contains(Object key)
This is used to determine whether the specified key exists
with in the cache.
|
T |
fetch(Object key)
This method is used to get the value from the cache that is
mapped to the specified key.
|
protected boolean |
removeEldestEntry(Map.Entry<Object,T> entry)
This is used to remove the eldest entry from the cache.
|
T |
take(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
equals, hashCode, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, 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(Object key, T value)
public T take(Object key)
public T fetch(Object key)
public boolean contains(Object key)
protected boolean removeEldestEntry(Map.Entry<Object,T> entry)
removeEldestEntry
in class LinkedHashMap<Object,T>
entry
- this is the eldest entry that can be removed