Class WeakCache<K,V>

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

public class WeakCache<K,V> extends Object implements Cache<K,V>
弱引用缓存:值以 WeakReference 持有,对象被 GC 回收后自动清理键; 支持可选 TTL(默认不过期)与容量上限(0 表示无上限),参考 Hutool 的 WeakCache 设计。

所有操作线程安全(synchronized),可安全用于虚拟线程高并发场景。 值对象仅被本缓存弱引用时,JVM 可在任意时刻回收,读取时返回 null 并自动移除。

Since:
1.10.0
Author:
suretool
  • Constructor Summary

    Constructors
    Constructor
    Description
    创建弱引用缓存:无容量上限、默认不过期。
    WeakCache(int capacity, long defaultTimeout)
    创建弱引用缓存。
    WeakCache(long defaultTimeout)
    创建弱引用缓存:无容量上限、指定默认 TTL。
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    清空缓存。
    boolean
    是否包含键(已过期或已回收视为不存在)。
    get(K key)
    读取缓存值;已过期或已被 GC 回收返回 null 并移除。
    int
    容量上限。
    long
    默认 TTL(毫秒),<=0 表示不过期。
    boolean
    是否为空。
    put(K key, V value)
    写入缓存(使用默认 TTL)。
    put(K key, V value, long timeout)
    写入缓存并指定 TTL。
    remove(K key)
    移除缓存项。
    int
    缓存项数量(含待清理的已回收项,实际读取时自动收敛)。

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface Cache

    getOrPut
  • Constructor Details

    • WeakCache

      public WeakCache()
      创建弱引用缓存:无容量上限、默认不过期。
    • WeakCache

      public WeakCache(long defaultTimeout)
      创建弱引用缓存:无容量上限、指定默认 TTL。
      Parameters:
      defaultTimeout - 默认存活时间(毫秒),<=0 表示不过期
    • WeakCache

      public WeakCache(int capacity, long defaultTimeout)
      创建弱引用缓存。
      Parameters:
      capacity - 容量上限,<=0 表示无上限;达到上限时优先清理已回收/过期项,仍超则按写入顺序淘汰
      defaultTimeout - 默认存活时间(毫秒),<=0 表示不过期
  • Method Details

    • get

      public V get(K key)
      读取缓存值;已过期或已被 GC 回收返回 null 并移除。
      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - 键
      Returns:
      值或 null
    • put

      public V put(K key, V value)
      写入缓存(使用默认 TTL)。
      Specified by:
      put in interface Cache<K,V>
      Parameters:
      key - 键
      value - 值
      Returns:
      被覆盖的旧值或 null
    • put

      public V put(K key, V value, long timeout)
      写入缓存并指定 TTL。
      Parameters:
      key - 键
      value - 值
      timeout - 存活时间(毫秒),<=0 表示不过期
      Returns:
      被覆盖的旧值或 null
    • remove

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

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

      public int size()
      缓存项数量(含待清理的已回收项,实际读取时自动收敛)。
      Specified by:
      size in interface Cache<K,V>
      Returns:
      数量
    • containsKey

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

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

      public int getCapacity()
      容量上限。
      Returns:
      容量上限,0 表示无上限
    • getDefaultTimeout

      public long getDefaultTimeout()
      默认 TTL(毫秒),<=0 表示不过期。
      Returns:
      默认 TTL