Class CacheMap<K,V>
- java.lang.Object
-
- com.identityworksllc.iiq.common.cache.CacheMap<K,V>
-
- Type Parameters:
K
- The key typeV
- The value type
- All Implemented Interfaces:
Serializable
,Function<K,Optional<V>>
,Map<K,V>
- Direct Known Subclasses:
VersionedCacheMap
public class CacheMap<K,V> extends Object implements Map<K,V>, Serializable, Function<K,Optional<V>>
Implements a Cache that exposes itself as a regular Map.Cached entries will be removed from the map only when an operation would touch that entry, including all bulk operations (e.g.
isEmpty()
).If you serialize this class, it will be replaced with a HashMap<K,V> snapshot in the serialization stream via
writeReplace()
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
CacheMap.CacheMapEntry
The cache map entry class that will be returned by getEntry()protected class
CacheMap.CacheMapEntrySet
The wrapper entry set for this map type, which will be returned by entrySet().
-
Constructor Summary
Constructors Constructor Description CacheMap()
Constructs a new cache map with the default expiration time of 10 minutesCacheMap(int amount, TimeUnit type)
Constructs a new empty cache map with the given expiration time in the given units.CacheMap(int amount, TimeUnit type, CacheGenerator<? extends V> generator)
Constructs a new empty cache map with the given expiration time in the given units.CacheMap(int amount, TimeUnit type, Map<? extends K,? extends V> input)
Constructs a new empty cache map with the given expiratino time in the given units.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<V>
apply(K k)
Returns an Optional wrapping a non-expired value, for use in streams, for example.protected void
beforeExpiration(K key, V value)
The default initial before expiration hook.protected CacheEntry<V>
cache(V value)
Caches the given value with the default expiration secondsvoid
clear()
boolean
containsKey(Object key)
Returns true if the map contains the given key and it is not expired.boolean
containsValue(Object value)
Set<Map.Entry<K,V>>
entrySet()
V
get(Object key)
protected ConcurrentMap<K,CacheEntry<? extends V>>
getInternalMap()
Gets the internal map, for use by subclasses onlyprotected Date
getNewExpirationDate()
Returns a new Date object offset from the current date by the default expiration duration in secondsdouble
getSecondsRemaining(K key)
Return the number of seconds remaining for the cache entry of the given key.void
invalidateRecords()
Invalidate all records in the internal storage that have expiredboolean
isEmpty()
Set<K>
keySet()
V
put(K key, V value)
void
putAll(Map<? extends K,? extends V> m)
Puts all entries from the other map into this one.protected void
putAllInternal(CacheMap<? extends K,? extends V> other)
Puts all cache entries from another CacheMap into this one.V
remove(Object key)
int
size()
Returns the size of the cache, after excluding expired records.HashMap<K,V>
snapshot()
Returns a snapshot of this cache at this moment in time as a java.util.HashMap.Collection<V>
values()
Returns all non-expired values from this Map.CacheMap<K,V>
withBeforeExpirationHook(BiConsumer<K,V> hook)
Adds the given hook function as a pre-expiration hook.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Constructor Detail
-
CacheMap
public CacheMap()
Constructs a new cache map with the default expiration time of 10 minutes
-
CacheMap
public CacheMap(int amount, TimeUnit type)
Constructs a new empty cache map with the given expiration time in the given units.The time will be converted internally to seconds.
- Parameters:
amount
- The amount of the given time unit before expirationtype
- The time unit
-
CacheMap
public CacheMap(int amount, TimeUnit type, Map<? extends K,? extends V> input)
Constructs a new empty cache map with the given expiratino time in the given units.Additionally, copies all values from the given Map into this CacheMap, setting their expiration as though they had just been inserted.
- Parameters:
amount
- The amount of the given time unit before expirationtype
- The time unitinput
- An arbitrary Map to copy into this CacheMap
-
CacheMap
public CacheMap(int amount, TimeUnit type, CacheGenerator<? extends V> generator)
Constructs a new empty cache map with the given expiration time in the given units.The time will be converted internally to seconds if the result is less than one second, then it will default to one second.
- Parameters:
amount
- The amount of the given time unit before expirationtype
- The time unitgenerator
- The value generator to use if get() does not match a key (pass null for none)
-
-
Method Detail
-
apply
public Optional<V> apply(K k)
Returns an Optional wrapping a non-expired value, for use in streams, for example.
-
beforeExpiration
protected void beforeExpiration(K key, V value)
The default initial before expiration hook.By default, this does nothing, but subclasses can override it to do whatever they’d like.
- Parameters:
key
- The key being expiredvalue
- The value being expired
-
cache
protected CacheEntry<V> cache(V value)
Caches the given value with the default expiration seconds- Parameters:
value
- The value to cache- Returns:
- A cache entry representing this value
-
containsKey
public boolean containsKey(Object key)
Returns true if the map contains the given key and it is not expired.If the map does not contain the key, and a value generator is defined, it will be invoked, causing this method to always return true.
- Specified by:
containsKey
in interfaceMap<K,V>
- See Also:
Map.containsKey(java.lang.Object)
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<K,V>
- See Also:
Map.containsValue(java.lang.Object)
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
- Specified by:
entrySet
in interfaceMap<K,V>
- See Also:
Map.entrySet()
-
get
public V get(Object key)
- Specified by:
get
in interfaceMap<K,V>
- See Also:
Map.get(java.lang.Object)
-
getInternalMap
protected ConcurrentMap<K,CacheEntry<? extends V>> getInternalMap()
Gets the internal map, for use by subclasses only- Returns:
- The internal map
-
getNewExpirationDate
protected Date getNewExpirationDate()
Returns a new Date object offset from the current date by the default expiration duration in seconds- Returns:
- The date of expiration starting now
-
getSecondsRemaining
public double getSecondsRemaining(K key)
Return the number of seconds remaining for the cache entry of the given key.If the key is not present or the cache entry has expired, this method returns 0.0.
- Parameters:
key
- The key to check- Returns:
- The number of seconds remaining
-
invalidateRecords
public void invalidateRecords()
Invalidate all records in the internal storage that have expired
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfaceMap<K,V>
- See Also:
Map.isEmpty()
-
put
public V put(K key, V value)
- Specified by:
put
in interfaceMap<K,V>
- See Also:
Map.put(java.lang.Object, java.lang.Object)
-
putAll
public void putAll(Map<? extends K,? extends V> m)
Puts all entries from the other map into this one.If the other Map is a CacheMap, forwards to
putAllInternal(CacheMap)
, which retains expiration.- Specified by:
putAll
in interfaceMap<K,V>
- See Also:
Map.putAll(java.util.Map)
-
putAllInternal
protected void putAllInternal(CacheMap<? extends K,? extends V> other)
Puts all cache entries from another CacheMap into this one.They will be copied as CacheEntry objects, thus retaining their expiration timestamps. Expired entries will not be copied.
- Parameters:
other
- The other CacheMap object
-
remove
public V remove(Object key)
- Specified by:
remove
in interfaceMap<K,V>
- See Also:
Map.remove(java.lang.Object)
-
size
public int size()
Returns the size of the cache, after excluding expired records.- Specified by:
size
in interfaceMap<K,V>
- See Also:
Map.size()
-
snapshot
public HashMap<K,V> snapshot()
Returns a snapshot of this cache at this moment in time as a java.util.HashMap.The snapshot will not be updated to reflect any future expirations of cache data.
- Returns:
- A snapshot of this cache
-
values
public Collection<V> values()
Returns all non-expired values from this Map.- Specified by:
values
in interfaceMap<K,V>
- See Also:
Map.values()
-
withBeforeExpirationHook
public CacheMap<K,V> withBeforeExpirationHook(BiConsumer<K,V> hook)
Adds the given hook function as a pre-expiration hook.It will be chained to any existing hooks using
BiConsumer.andThen(BiConsumer)
.- Parameters:
hook
- The new hook- Returns:
- This object, for chaining or static construction
-
-