Class CacheMap<K,V>

java.lang.Object
io.keikai.model.util.CacheMap<K,V>
All Implemented Interfaces:
Serializable

public class CacheMap<K,V> extends Object implements 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
    Constructor
    Description
    CacheMap(int maxSize)
    Creates a cache map with the given maximum size.
  • Method Summary

    Modifier and Type
    Method
    Description
    get(Object key)
    Retrieves the value for the given key, or null if absent or expired.
    put(K key, V value)
    Stores a key-value pair, recording the current timestamp.
    void
    setLifetime(long lifetimeMillis)
    Sets the maximum lifetime of each entry in milliseconds.
    int
    Returns the number of entries currently stored (including potentially expired ones that have not yet been evicted).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 next get(Object) call.
      Parameters:
      lifetimeMillis - lifetime in milliseconds; 0 means no expiry
    • get

      public V get(Object key)
      Retrieves the value for the given key, or null if absent or expired.
    • put

      public V put(K key, V value)
      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).