Class ReentrantCache<K,V>
java.lang.Object
org.miaixz.bus.core.cache.provider.AbstractCache<K,V>
org.miaixz.bus.core.cache.provider.ReentrantCache<K,V>
- Type Parameters:
K- 键类型V- 值类型
- All Implemented Interfaces:
Serializable,Iterable<V>,Cache<K,V>
- Direct Known Subclasses:
LRUCache
使用
ReentrantLock保护的缓存,读写都使用悲观锁完成,主要避免某些Map无法使用读写锁的问题 例如使用了LinkedHashMap的缓存,由于get方法也会改变Map的结构,因此读写必须加互斥锁- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ReentrantLock特殊缓存,例如使用了LinkedHashMap的缓存,由于get方法也会改变Map的结构,导致无法使用读写锁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,否则返回值。final intprune()从缓存中清理过期对象,清理策略取决于具体实现void将对象加入到缓存,使用指定失效时长 如果缓存空间满了,Cache.prune()将被调用以获得空间来存放新对象void从缓存中移除对象toString()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, timeoutMethods 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
特殊缓存,例如使用了LinkedHashMap的缓存,由于get方法也会改变Map的结构,导致无法使用读写锁
-
-
Constructor Details
-
ReentrantCache
public ReentrantCache()
-
-
Method Details
-
put
Description copied from interface:Cache将对象加入到缓存,使用指定失效时长 如果缓存空间满了,Cache.prune()将被调用以获得空间来存放新对象- Parameters:
key- 键object- 缓存的对象timeout- 失效时长,单位毫秒
-
containsKey
-
get
-
cacheObjIterator
-
prune
public final int prune()Description copied from interface:Cache从缓存中清理过期对象,清理策略取决于具体实现- Returns:
- 清理的缓存对象个数
-
remove
-
clear
-
toString
- Overrides:
toStringin classAbstractCache<K,V>
-