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
使用
StampedLock保护的缓存,使用读写乐观锁- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final StampedLock乐观锁,此处使用乐观锁解决读多写少的场景 get时乐观读,再检查是否修改,修改则转入悲观读重新读一遍,可以有效解决在写时阻塞大量读操作的情况Fields inherited from class org.miaixz.bus.core.cache.provider.AbstractCache
cacheMap, capacity, existCustomTimeout, hitCount, keyLockMap, listener, missCount, timeout -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription返回包含键和值得迭代器voidclear()清空缓存booleancontainsKey(K key) 是否包含key从缓存中获得对象,当对象不在缓存中或已经过期(与当前时间差值大于超时时间)返回null,否则返回值。 每次调用此方法会可选是否刷新最后访问时间,true表示会重新计算超时时间。final intprune()从缓存中清理过期对象,清理策略取决于具体实现void将对象加入到缓存,使用指定失效时长 如果缓存空间满了,Cache.prune()将被调用以获得空间来存放新对象void从缓存中移除对象Methods inherited from class org.miaixz.bus.core.cache.provider.AbstractCache
cacheObjIter, capacity, get, get, getHitCount, getMissCount, getWithoutLock, isEmpty, isFull, isPruneExpiredActive, iterator, keySet, onRemove, pruneCache, put, putWithoutLock, removeWithoutLock, setListener, size, timeout, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
lock
乐观锁,此处使用乐观锁解决读多写少的场景 get时乐观读,再检查是否修改,修改则转入悲观读重新读一遍,可以有效解决在写时阻塞大量读操作的情况
-
-
Constructor Details
-
StampedCache
public StampedCache()
-
-
Method Details
-
put
Description copied from interface:Cache将对象加入到缓存,使用指定失效时长 如果缓存空间满了,Cache.prune()将被调用以获得空间来存放新对象- Parameters:
key- 键object- 缓存的对象timeout- 失效时长,单位毫秒
-
containsKey
Description copied from interface:Cache是否包含key- Parameters:
key- KEY- Returns:
- 是否包含key
-
get
Description copied from interface:Cache从缓存中获得对象,当对象不在缓存中或已经过期(与当前时间差值大于超时时间)返回null,否则返回值。 每次调用此方法会可选是否刷新最后访问时间,true表示会重新计算超时时间。- Parameters:
key- 键isUpdateLastAccess- 是否更新最后访问时间,即重新计算超时时间。- Returns:
- 键对应的对象
-
cacheObjIterator
Description copied from interface:Cache返回包含键和值得迭代器- Returns:
- 缓存对象迭代器
-
prune
public final int prune()Description copied from interface:Cache从缓存中清理过期对象,清理策略取决于具体实现- Returns:
- 清理的缓存对象个数
-
remove
Description copied from interface:Cache从缓存中移除对象- Parameters:
key- 键
-
clear
public void clear()Description copied from interface:Cache清空缓存
-