类 ConcurrentLruCache<K,V>
java.lang.Object
cn.taketoday.util.ConcurrentLruCache<K,V>
- 类型参数:
K- the type of the key used for cache retrievalV- the type of the cached values, does not allow null values
Simple LRU (Least Recently Used) cache, bounded by a specified cache capacity.
This is a simplified, opinionated implementation of a LRU cache for internal use in Infra Framework. It is inspired from ConcurrentLinkedHashMap.
Read and write operations are internally recorded in dedicated buffers, then drained at chosen times to avoid contention.
- 从以下版本开始:
- 4.0
- 作者:
- Brian Clozel, Ben Manes, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private final classWrite operation recorded when a new entry is added to the cache.private static final recordprivate static enumprivate static enumDraining status for the read/write buffers.private static final classprivate static final classprivate static final classprivate final classWrite operation recorded when an entry is removed to the cache.private static final class -
字段概要
字段修饰符和类型字段说明private final ConcurrentMap<K,ConcurrentLruCache.Node<K, V>> private final intprivate final AtomicIntegerprivate final AtomicReference<ConcurrentLruCache.DrainStatus>private final ReentrantLockprivate final ConcurrentLruCache.EvictionQueue<K,V> Queue that contains all ACTIVE cache entries, ordered with least recently used entries first.private final ConcurrentLruCache.ReadOperations<K,V> private final ConcurrentLruCache.WriteOperations -
构造器概要
构造器限定符构造器说明ConcurrentLruCache(int capacity, Function<K, V> generator) Create a new cache instance with the given capacity and generator function.privateConcurrentLruCache(int capacity, Function<K, V> generator, int concurrencyLevel) -
方法概要
修饰符和类型方法说明intcapacity()Return the maximum number of entries in the cache.voidclear()Immediately remove all entries from this cache.booleanDetermine whether the given key is present in this cache.private voidRetrieve an entry from the cache, potentially triggering generation of the value.private voidmarkAsRemoved(ConcurrentLruCache.Node<K, V> node) Transition the node to theremovedstate and decrement the current size of the cache.private voidmarkForRemoval(ConcurrentLruCache.Node<K, V> node) Transition the node from theactivestate to thepending removalstate, if the transition is valid.private voidprocessRead(ConcurrentLruCache.Node<K, V> node) private voidprocessWrite(Runnable task) private voidbooleanImmediately remove the given key and any associated value.intsize()Return the current size of the cache.
-
字段详细资料
-
capacity
private final int capacity -
currentSize
-
cache
-
generator
-
readOperations
-
writeOperations
-
evictionLock
-
evictionQueue
Queue that contains all ACTIVE cache entries, ordered with least recently used entries first. Read and write operations are buffered and periodically processed to reorder the queue. -
drainStatus
-
-
构造器详细资料
-
ConcurrentLruCache
Create a new cache instance with the given capacity and generator function.- 参数:
capacity- the maximum number of entries in the cache (0 indicates no caching, always generating a new value)generator- a function to generate a new value for a given key
-
ConcurrentLruCache
-
-
方法详细资料
-
get
Retrieve an entry from the cache, potentially triggering generation of the value.- 参数:
key- the key to retrieve the entry for- 返回:
- the cached or newly generated value
-
put
-
processRead
-
processWrite
-
drainOperations
private void drainOperations() -
capacity
public int capacity()Return the maximum number of entries in the cache.- 另请参阅:
-
size
public int size()Return the current size of the cache.- 另请参阅:
-
clear
public void clear()Immediately remove all entries from this cache. -
markAsRemoved
Transition the node to theremovedstate and decrement the current size of the cache. -
contains
Determine whether the given key is present in this cache.- 参数:
key- the key to check for- 返回:
trueif the key is present,falseif there was no matching key
-
remove
Immediately remove the given key and any associated value.- 参数:
key- the key to evict the entry for- 返回:
trueif the key was present before,falseif there was no matching key
-
markForRemoval
Transition the node from theactivestate to thepending removalstate, if the transition is valid.
-