Package io.keikai.model.util
Class CacheMap<K,V>
java.lang.Object
io.keikai.model.util.CacheMap<K,V>
- All Implemented Interfaces:
Serializable
A simple LRU cache map with optional time-based entry expiry.
Drop-in replacement for org.zkoss.util.CacheMap.
Backed by a LinkedHashMap in access-order mode so that
least-recently-used entries are evicted when the map exceeds
its maximum size.
When a lifetime is configured,
entries older than the lifetime are treated as absent on
get(Object) and silently removed.
Thread safety: Public mutators (get(Object),
put(Object, Object), size()) synchronize on the
instance. The underlying LinkedHashMap in access-order mode
mutates its internal linkage on every get, so unguarded
concurrent access would corrupt the linked list — callers do not
need to add their own external locking.
- Since:
- 7.0.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the value for the given key, ornullif absent or expired.Stores a key-value pair, recording the current timestamp.voidsetLifetime(long lifetimeMillis) Sets the maximum lifetime of each entry in milliseconds.intsize()Returns the number of entries currently stored (including potentially expired ones that have not yet been evicted).
-
Constructor Details
-
CacheMap
public CacheMap(int maxSize) Creates a cache map with the given maximum size.- Parameters:
maxSize- the maximum number of entries before LRU eviction
-
-
Method Details
-
setLifetime
public void setLifetime(long lifetimeMillis) Sets the maximum lifetime of each entry in milliseconds. Entries older than this are considered expired and will be removed on the nextget(Object)call.- Parameters:
lifetimeMillis- lifetime in milliseconds; 0 means no expiry
-
get
Retrieves the value for the given key, ornullif absent or expired. -
put
Stores a key-value pair, recording the current timestamp. -
size
public int size()Returns the number of entries currently stored (including potentially expired ones that have not yet been evicted).
-