Class ForgetfulMap<K,V>
- All Implemented Interfaces:
Map<K,
V>
Map
that can forget entries after a specified period of time – a cache in other words. The
entries can either expire after a certain amount of time has passed since they were created/written or
since they were last accessed.
In addition to implementing the Map
interface, also looked at the Cache interface of the Caffeine
library, and mimicked some of its methods.
Furthermore, this class provides a re-implementation of TypeCache
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
private static final class
Helper class to store value and timestampsstatic final class
A re-implementation ofTypeCache
backed by aForgetfulMap
.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ScheduledExecutorService
private final long
private final Map
<K, ForgetfulMap.CachedValue<V>> private final long
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
cleanUp()
Removes all entries that have expired.void
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) entrySet()
boolean
long
Returns the approximate number of entries in this cache.Returns the value associated with thekey
in this cache, obtaining that value from themappingFunction
if necessary.getAllPresent
(Iterable<? extends K> keys) Returns a map of the values associated with thekeys
in this cache.getIfPresent
(K key) Returns the value associated with thekey
in this cache, ornull
if there is no cached value for thekey
.int
hashCode()
void
invalidate
(K key) Discards any cached value for thekey
.void
Discards all entries in the cache.void
invalidateAll
(Iterable<? extends K> keys) Discards any cached values for thekeys
.boolean
isEmpty()
private boolean
isExpired
(long now, ForgetfulMap.CachedValue<V> cached) keySet()
static ForgetfulMap.Builder
newValueCache
(K key, Function<? super K, ? extends V> valueSupplier) void
Copies all of the mappings from the specified map to the cache.boolean
int
size()
unwrap
(Map.Entry<K, ForgetfulMap.CachedValue<V>> entry) Methods inherited from class java.util.AbstractMap
clone, toString, values
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, replace, replace, replaceAll
-
Field Details
-
CLEANER
-
myAccessLimit
private final long myAccessLimit -
myStorage
-
myWriteLimit
private final long myWriteLimit
-
-
Constructor Details
-
ForgetfulMap
ForgetfulMap(ForgetfulMap.Builder builder)
-
-
Method Details
-
newBuilder
-
cleanUp
public void cleanUp()Removes all entries that have expired. This method is automatically called at regular intervals, but can also be called manually to remove entries that have expired since the last cleanup. -
clear
public void clear() -
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Overrides:
containsKey
in classAbstractMap<K,
V>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V> - Overrides:
containsValue
in classAbstractMap<K,
V>
-
entrySet
-
equals
-
estimatedSize
public long estimatedSize()Returns the approximate number of entries in this cache. The value returned is an estimate; the actual count may differ if there are concurrent insertions or removals, or if some entries are pending removal due to expiration or weak/soft reference collection. In the case of stale entries this inaccuracy can be mitigated by performing acleanUp()
first. -
get
Returns the value associated with thekey
in this cache, obtaining that value from themappingFunction
if necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache and return" pattern.If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this cache unless
null
. The entire method invocation is performed atomically, so the function is applied at most once per key. Some attempted update operations on this cache by other threads may be blocked while the computation is in progress, so the computation should be short and simple, and must not attempt to update any other mappings of this cache. -
get
-
getAllPresent
Returns a map of the values associated with thekeys
in this cache. The returned map will only contain entries which are already present in the cache.Note that duplicate elements in
keys
, as determined byObject.equals(java.lang.Object)
, will be ignored. -
getIfPresent
Returns the value associated with thekey
in this cache, ornull
if there is no cached value for thekey
. -
hashCode
public int hashCode() -
invalidate
Discards any cached value for thekey
. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present. -
invalidateAll
public void invalidateAll()Discards all entries in the cache. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present. -
invalidateAll
Discards any cached values for thekeys
. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present. -
isEmpty
public boolean isEmpty() -
keySet
-
newValueCache
-
put
-
putAll
Copies all of the mappings from the specified map to the cache. The effect of this call is equivalent to that of callingput(k, v)
on this map once for each mapping from keyk
to valuev
in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress. -
remove
-
remove
-
size
public int size() -
isExpired
-
unwrap
-