Class LruCache<K,V>

java.lang.Object
com.sure.tool.cache.LruCache<K,V>
Type Parameters:
K - 键类型
V - 值类型
All Implemented Interfaces:
Cache<K,V>

public class LruCache<K,V> extends Object implements Cache<K,V>
最近最少使用缓存:容量满时淘汰最久未访问的条目,参考 Hutool 的 LRUCache 设计。

线程安全(synchronized 保护);get 会刷新访问顺序。

Since:
0.1.0
Author:
suretool
  • Constructor Details

    • LruCache

      public LruCache(int capacity)
      创建 LRU 缓存。
      Parameters:
      capacity - 容量上限(>0)
  • Method Details

    • get

      public V get(K key)
      Description copied from interface: Cache
      读取缓存值,不存在或已过期返回 null。
      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - 键
      Returns:
      值或 null
    • put

      public V put(K key, V value)
      Description copied from interface: Cache
      写入缓存。
      Specified by:
      put in interface Cache<K,V>
      Parameters:
      key - 键
      value - 值
      Returns:
      被覆盖的旧值或 null
    • remove

      public V remove(K key)
      Description copied from interface: Cache
      移除缓存项。
      Specified by:
      remove in interface Cache<K,V>
      Parameters:
      key - 键
      Returns:
      被移除的值或 null
    • clear

      public void clear()
      Description copied from interface: Cache
      清空缓存。
      Specified by:
      clear in interface Cache<K,V>
    • size

      public int size()
      Description copied from interface: Cache
      缓存项数量。
      Specified by:
      size in interface Cache<K,V>
      Returns:
      数量
    • containsKey

      public boolean containsKey(K key)
      Description copied from interface: Cache
      是否包含键(已过期视为不存在)。
      Specified by:
      containsKey in interface Cache<K,V>
      Parameters:
      key - 键
      Returns:
      是否包含
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Cache
      是否为空。
      Specified by:
      isEmpty in interface Cache<K,V>
      Returns:
      是否为空
    • getCapacity

      public int getCapacity()
      获取容量上限。
      Returns:
      容量