Class LRUCache<K,V>
java.lang.Object
org.miaixz.bus.core.cache.provider.AbstractCache<K,V>
org.miaixz.bus.core.cache.provider.ReentrantCache<K,V>
org.miaixz.bus.core.cache.provider.LRUCache<K,V>
- Type Parameters:
K- 键类型V- 值类型
- All Implemented Interfaces:
Serializable,Iterable<V>,Cache<K,V>
LRU (least recently used)最近最久未使用缓存 根据使用时间来判定对象是否被持续缓存 当对象被访问时放入缓存,当缓存满了,最久未被使用的对象将被移除。
此缓存基于LinkedHashMap,因此当被缓存的对象每被访问一次,这个对象的key就到链表头部。 这个算法简单并且非常快,他比FIFO有一个显著优势是经常使用的对象不太可能被移除缓存。 缺点是当缓存满时,不能被很快的访问。
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Field Summary
Fields inherited from class org.miaixz.bus.core.cache.provider.ReentrantCache
lockFields inherited from class org.miaixz.bus.core.cache.provider.AbstractCache
cacheMap, capacity, existCustomTimeout, hitCount, keyLockMap, listener, missCount, timeout -
Constructor Summary
Constructors -
Method Summary
Methods inherited from class org.miaixz.bus.core.cache.provider.ReentrantCache
cacheObjIterator, clear, containsKey, get, prune, put, remove, toStringMethods inherited from class org.miaixz.bus.core.cache.provider.AbstractCache
cacheObjIter, capacity, get, get, getHitCount, getMissCount, getWithoutLock, isEmpty, isFull, isPruneExpiredActive, iterator, keySet, onRemove, 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
-
Constructor Details
-
LRUCache
public LRUCache(int capacity) 构造 默认无超时- Parameters:
capacity- 容量
-
LRUCache
public LRUCache(int capacity, long timeout) 构造- Parameters:
capacity- 容量timeout- 默认超时时间,单位:毫秒
-
-
Method Details
-
pruneCache
protected int pruneCache()只清理超时对象,LRU的实现会交给LinkedHashMap- Specified by:
pruneCachein classAbstractCache<K,V> - Returns:
- 清理数
-