Class AbstractCache<K,V>

java.lang.Object
org.miaixz.bus.core.cache.provider.AbstractCache<K,V>
Type Parameters:
K - 键类型
V - 值类型
All Implemented Interfaces:
Serializable, Iterable<V>, Cache<K,V>
Direct Known Subclasses:
ReentrantCache, StampedCache

public abstract class AbstractCache<K,V> extends Object implements Cache<K,V>
超时和限制大小的缓存的默认实现 继承此抽象缓存需要:
  • 创建一个新的Map
  • 实现 prune 策略
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • keyLockMap

      protected final Map<K,Lock> keyLockMap
      写的时候每个key一把锁,降低锁的粒度
    • cacheMap

      protected Map<Mutable<K>,CacheObject<K,V>> cacheMap
    • capacity

      protected int capacity
      返回缓存容量,0表示无大小限制
    • timeout

      protected long timeout
      缓存失效时长, 0 表示无限制,单位毫秒
    • existCustomTimeout

      protected boolean existCustomTimeout
      每个对象是否有单独的失效时长,用于决定清理过期对象是否有必要。
    • hitCount

      protected LongAdder hitCount
      命中数,即命中缓存计数
    • missCount

      protected LongAdder missCount
      丢失数,即未命中缓存计数
    • listener

      protected CacheListener<K,V> listener
      缓存监听
  • Constructor Details

    • AbstractCache

      public AbstractCache()
  • Method Details

    • put

      public void put(K key, V object)
      Description copied from interface: Cache
      将对象加入到缓存,使用默认失效时长
      Specified by:
      put in interface Cache<K,V>
      Parameters:
      key - 键
      object - 缓存的对象
      See Also:
    • putWithoutLock

      protected void putWithoutLock(K key, V object, long timeout)
      加入元素,无锁
      Parameters:
      key - 键
      object - 值
      timeout - 超时时长
    • getHitCount

      public long getHitCount()
      Returns:
      命中数
    • getMissCount

      public long getMissCount()
      Returns:
      丢失数
    • get

      public V get(K key, boolean isUpdateLastAccess, SupplierX<V> supplier)
      Description copied from interface: Cache
      从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象

      调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。

      每次调用此方法会可选是否刷新最后访问时间,true表示会重新计算超时时间。

      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - 键
      isUpdateLastAccess - 是否更新最后访问时间,即重新计算超时时间。
      supplier - 如果不存在回调方法,用于生产值对象
      Returns:
      值对象
    • get

      public V get(K key, boolean isUpdateLastAccess, long timeout, SupplierX<V> supplier)
      Description copied from interface: Cache
      从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象

      调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。

      每次调用此方法会可选是否刷新最后访问时间,true表示会重新计算超时时间。

      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - 键
      isUpdateLastAccess - 是否更新最后访问时间,即重新计算超时时间。
      timeout - 自定义超时时间
      supplier - 如果不存在回调方法,用于生产值对象
      Returns:
      值对象
    • getWithoutLock

      protected CacheObject<K,V> getWithoutLock(K key)
      获取键对应的CacheObject
      Parameters:
      key - 键,实际使用时会被包装为MutableObject
      Returns:
      CacheObject
    • iterator

      public Iterator<V> iterator()
      Specified by:
      iterator in interface Iterable<K>
    • pruneCache

      protected abstract int pruneCache()
      清理实现 子类实现此方法时无需加锁
      Returns:
      清理数
    • capacity

      public int capacity()
      Description copied from interface: Cache
      返回缓存容量,0表示无大小限制
      Specified by:
      capacity in interface Cache<K,V>
      Returns:
      返回缓存容量,0表示无大小限制
    • timeout

      public long timeout()
      Description copied from interface: Cache
      缓存失效时长, 0 表示没有设置,单位毫秒
      Specified by:
      timeout in interface Cache<K,V>
      Returns:
      默认缓存失效时长。 每个对象可以单独设置失效时长
    • isPruneExpiredActive

      protected boolean isPruneExpiredActive()
      只有设置公共缓存失效时长或每个对象单独的失效时长时清理可用
      Returns:
      过期对象清理是否可用,内部使用
    • isFull

      public boolean isFull()
      Description copied from interface: Cache
      缓存是否已满,仅用于有空间限制的缓存对象
      Specified by:
      isFull in interface Cache<K,V>
      Returns:
      缓存是否已满,仅用于有空间限制的缓存对象
    • size

      public int size()
      Description copied from interface: Cache
      缓存的对象数量
      Specified by:
      size in interface Cache<K,V>
      Returns:
      缓存的对象数量
    • isEmpty

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

      public String toString()
      Overrides:
      toString in class Object
    • setListener

      public AbstractCache<K,V> setListener(CacheListener<K,V> listener)
      设置监听
      Specified by:
      setListener in interface Cache<K,V>
      Parameters:
      listener - 监听
      Returns:
      this
    • keySet

      public Set<K> keySet()
      返回所有键
      Returns:
      所有键
    • onRemove

      protected void onRemove(K key, V cachedObject)
      对象移除回调。默认无动作 子类可重写此方法用于监听移除事件,如果重写,listener将无效
      Parameters:
      key - 键
      cachedObject - 被缓存的对象
    • removeWithoutLock

      protected CacheObject<K,V> removeWithoutLock(K key)
      移除key对应的对象,不加锁
      Parameters:
      key - 键
      Returns:
      移除的对象,无返回null
    • cacheObjIter

      protected Iterator<CacheObject<K,V>> cacheObjIter()
      获取所有CacheObject值的Iterator形式
      Returns:
      Iterator