Class ReentrantCache<K,V>

java.lang.Object
com.xxl.tool.cache.impl.ReentrantCache<K,V>
Type Parameters:
K - 键类型
V - 值类型
All Implemented Interfaces:
Cache<K,V>, Serializable
Direct Known Subclasses:
FIFOCache, LFUCache, LRUCache, UnlimitedCache

public abstract class ReentrantCache<K,V> extends Object implements Cache<K,V>
Reentrant Cache
See Also:
  • Field Details

    • timeout

      protected long timeout
      缓存有效期,0 表示无限制,单位毫秒
    • capacity

      protected int capacity
      缓存容量, 0 表示无限制
    • expireType

      protected boolean expireType
      缓存过期方式: - true: expireAfterWrite , 从缓存写入开始计算,有效期到期则被清除 - false: expireAfterAccess, 从缓存最后一次访问开始计算,有效期到期则被清除
    • writeLockMap

      protected final ConcurrentMap<Long,Lock> writeLockMap
      write key map
    • writeLockCount

      protected final int writeLockCount
      write lock count
      See Also:
    • cacheMap

      protected volatile Map<CacheKey<K>,CacheObject<K,V>> cacheMap
      缓存数据
    • hitCount

      protected LongAdder hitCount
      缓存命中计数
    • missCount

      protected LongAdder missCount
      缓存未命中计数
    • listener

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

      protected CacheLoader<K,V> loader
      缓存加载器
  • Constructor Details

    • ReentrantCache

      public ReentrantCache()
  • 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 - 值对象
    • get

      public V get(K key)
      Description copied from interface: Cache
      获取缓存对象

      1、缓存对象不存在 或 已过期:尝试使用 cacheLoader 加载并返回对象,如果 cacheLoader 不存在返回 null 2、每次调用此方法会刷新最后访问时间,即重新计算超时时间

      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - 键
      Returns:
      值对象
    • getIfPresent

      public V getIfPresent(K key)
      Description copied from interface: Cache
      获取缓存对象

      1、缓存对象不存在 或 已过期:直接返回 null 2、每次调用此方法会刷新最后访问时间,即重新计算超时时间

      Specified by:
      getIfPresent in interface Cache<K,V>
      Parameters:
      key - 键
      Returns:
      值对象
    • get

      public V get(K key, CacheLoader<K,V> cacheLoader)
      Description copied from interface: Cache
      获取缓存对象

      1、缓存对象不存在 或 已过期:使用 cacheLoader 加载并返回对象 2、每次调用此方法,可选是否 刷新最后访问时间,即重新计算超时时间

      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - 键
      cacheLoader - 回调方法,用于生产值对象
      Returns:
      值对象
    • containsKey

      public boolean containsKey(K key)
      Description copied from interface: Cache
      是否包含key
      Specified by:
      containsKey in interface Cache<K,V>
      Parameters:
      key - 键
      Returns:
      值对象
    • asMap

      public Map<K,V> asMap()
      Description copied from interface: Cache
      返回 键-值 映射,仅包括有效数据
      Specified by:
      asMap in interface Cache<K,V>
      Returns:
      键-值 映射(过滤已过期数据)
    • size

      public int size()
      Description copied from interface: Cache
      缓存对象数量

      1、包含过期数据

      Specified by:
      size in interface Cache<K,V>
    • isFull

      public boolean isFull()
      Description copied from interface: Cache
      缓存是否已满,仅用于有空间限制的缓存对象

      1、包含过期数据

      Specified by:
      isFull in interface Cache<K,V>
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Cache
      缓存是否为空

      1、包含过期数据

      Specified by:
      isEmpty in interface Cache<K,V>
    • remove

      public void remove(K key)
      Description copied from interface: Cache
      删除单个缓存对象
      Specified by:
      remove in interface Cache<K,V>
      Parameters:
      key - 键
    • removeWithoutLock

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

      protected void onRemove(CacheObject<K,V> cacheObject)
      对象移除回调
      Parameters:
      cacheObject - 被缓存的对象
    • prune

      public final int prune()
      Description copied from interface: Cache
      清理过期缓存对象
      Specified by:
      prune in interface Cache<K,V>
    • doPrune

      protected abstract int doPrune()
      清理实现
      子类实现此方法时无需加锁
      Returns:
      清理数据量
    • clear

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

      public long hitCount()
      Specified by:
      hitCount in interface Cache<K,V>
      Returns:
      命中数
    • missCount

      public long missCount()
      Specified by:
      missCount in interface Cache<K,V>
      Returns:
      丢失数
    • capacity

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

      public long timeout()
      Description copied from interface: Cache
      缓存有效期,0 表示无限制,单位毫秒
      Specified by:
      timeout in interface Cache<K,V>
    • setListener

      public ReentrantCache<K,V> setListener(CacheListener<K,V> listener)
      设置监听
      Specified by:
      setListener in interface Cache<K,V>
    • setLoader

      public ReentrantCache<K,V> setLoader(CacheLoader<K,V> loader)
      Description copied from interface: Cache
      设置监听
      Specified by:
      setLoader in interface Cache<K,V>
    • toString

      public String toString()
      是否支持过期对象清理,仅设置 过期时间 才支持;
      Overrides:
      toString in class Object