Class StampedCache<K,V>

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

public abstract class StampedCache<K,V> extends AbstractCache<K,V>
使用StampedLock保护的缓存,使用读写乐观锁
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • lock

      protected final StampedLock lock
      乐观锁,此处使用乐观锁解决读多写少的场景 get时乐观读,再检查是否修改,修改则转入悲观读重新读一遍,可以有效解决在写时阻塞大量读操作的情况
  • Constructor Details

    • StampedCache

      public StampedCache()
  • Method Details

    • put

      public void put(K key, V object, long timeout)
      Description copied from interface: Cache
      将对象加入到缓存,使用指定失效时长 如果缓存空间满了,Cache.prune() 将被调用以获得空间来存放新对象
      Parameters:
      key - 键
      object - 缓存的对象
      timeout - 失效时长,单位毫秒
    • containsKey

      public boolean containsKey(K key)
      Description copied from interface: Cache
      是否包含key
      Parameters:
      key - KEY
      Returns:
      是否包含key
    • get

      public V get(K key, boolean isUpdateLastAccess)
      Description copied from interface: Cache
      从缓存中获得对象,当对象不在缓存中或已经过期返回null

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

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

      Parameters:
      key - 键
      isUpdateLastAccess - 是否更新最后访问时间,即重新计算超时时间。
      Returns:
      键对应的对象
    • cacheObjIterator

      public Iterator<CacheObject<K,V>> cacheObjIterator()
      Description copied from interface: Cache
      返回包含键和值得迭代器
      Returns:
      缓存对象迭代器
    • prune

      public final int prune()
      Description copied from interface: Cache
      从缓存中清理过期对象,清理策略取决于具体实现
      Returns:
      清理的缓存对象个数
    • remove

      public void remove(K key)
      Description copied from interface: Cache
      从缓存中移除对象
      Parameters:
      key - 键
    • clear

      public void clear()
      Description copied from interface: Cache
      清空缓存