Class LfuCache<K,V>

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

public class LfuCache<K,V> extends Object implements Cache<K,V>
最不经常使用缓存:容量满时淘汰访问频率最低的条目(频率相同淘汰最早写入),参考 Hutool 的 LFUCache 设计。

线程安全(synchronized 保护);淘汰为 O(n) 扫描,适合中小规模缓存。

Since:
0.1.0
Author:
suretool
  • Constructor Details

    • LfuCache

      public LfuCache(int capacity)
      创建 LFU 缓存。
      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:
      是否为空