Class TimedCache<K,V>

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

public class TimedCache<K,V> extends Object implements Cache<K,V>
定时过期缓存:条目可设定存活时间(TTL),读取时惰性过期,参考 Hutool 的 TimedCache 设计。

线程安全(synchronized 保护);默认 TTL 由构造参数指定,put 可单独覆盖; 过期的条目在访问时被移除,也可调用 prune() 主动清理。

Since:
0.1.0
Author:
suretool
  • Constructor Summary

    Constructors
    Constructor
    Description
    TimedCache(long defaultTimeout)
    创建定时缓存。
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    清空缓存。
    boolean
    是否包含键(已过期视为不存在)。
    get(K key)
    读取缓存值(过期返回 null 并移除)。
    long
    获取键的剩余存活时间。
    boolean
    是否为空。
    int
    主动清理全部过期条目。
    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

    • TimedCache

      public TimedCache(long defaultTimeout)
      创建定时缓存。
      Parameters:
      defaultTimeout - 默认存活时间(毫秒),<=0 表示不过期
  • Method Details

    • 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
    • get

      public V get(K key)
      读取缓存值(过期返回 null 并移除)。
      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - 键
      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:
      是否为空
    • prune

      public int prune()
      主动清理全部过期条目。
      Returns:
      清理数量
    • getRemainingTime

      public long getRemainingTime(K key)
      获取键的剩余存活时间。
      Parameters:
      key - 键
      Returns:
      剩余毫秒:不存在或已过期返回 0;永不过期返回 Long.MAX_VALUE