Class Cache<K,V>

java.lang.Object
smile.util.Cache<K,V>
Type Parameters:
K - the type of cache keys.
V - the type of cache values.

public class Cache<K,V> extends Object
An LRU (Least Recently Used) cache with TTL (Time To Live) combines size-based eviction with time-based expiration.
  • Constructor Details

    • Cache

      public Cache(int capacity, Duration ttl)
      Constructor.
      Parameters:
      capacity - the maximum number of entries in the cache.
      ttl - the time-to-live duration for each cache entry.
  • Method Details

    • keySet

      public Set<K> keySet()
      Returns the set of keys in the cache. Expired entries will be evicted first.
      Returns:
      the set of keys in the cache.
    • valueSet

      public Collection<V> valueSet()
      Returns the set of values in the cache. Expired entries will be evicted first.
      Returns:
      the set of values in the cache.
    • put

      public V put(K key, V value)
      Puts a value in the cache with the current timestamp. If the cache exceeds its capacity, the oldest entry will be evicted.
      Parameters:
      key - the cache key.
      value - the cache value.
      Returns:
      the previous value associated with the key, or null if there was no mapping for the key.
    • get

      public V get(K key)
      Gets a value from the cache. If the entry has expired, it will be removed and null will be returned.
      Parameters:
      key - the cache key.
      Returns:
      the value associated with the key, or null if the key is not present or has expired.
    • remove

      public V remove(K key)
      Removes the cache value for a key.
      Parameters:
      key - the key whose cache value is to be removed from the map.
      Returns:
      the previous value associated with key, or null if there was no cache value for key.