Class AbstractCache<K,V>
java.lang.Object
org.miaixz.bus.core.cache.provider.AbstractCache<K,V>
- Type Parameters:
K- 键类型V- 值类型
- All Implemented Interfaces:
Serializable,Iterable<V>,Cache<K,V>
- Direct Known Subclasses:
ReentrantCache,StampedCache
超时和限制大小的缓存的默认实现
继承此抽象缓存需要:
- 创建一个新的Map
- 实现
prune策略
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int返回缓存容量,0表示无大小限制protected boolean每个对象是否有单独的失效时长,用于决定清理过期对象是否有必要。protected LongAdder命中数,即命中缓存计数写的时候每个key一把锁,降低锁的粒度protected CacheListener<K, V> 缓存监听protected LongAdder丢失数,即未命中缓存计数protected long缓存失效时长,0表示无限制,单位毫秒 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Iterator<CacheObject<K, V>> 获取所有CacheObject值的Iterator形式intcapacity()返回缓存容量,0表示无大小限制从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象longlongprotected CacheObject<K, V> getWithoutLock(K key) 获取键对应的CacheObjectbooleanisEmpty()缓存是否为空booleanisFull()缓存是否已满,仅用于有空间限制的缓存对象protected boolean只有设置公共缓存失效时长或每个对象单独的失效时长时清理可用iterator()keySet()返回所有键protected void对象移除回调。默认无动作 子类可重写此方法用于监听移除事件,如果重写,listener将无效protected abstract int清理实现 子类实现此方法时无需加锁void将对象加入到缓存,使用默认失效时长protected voidputWithoutLock(K key, V object, long timeout) 加入元素,无锁protected CacheObject<K, V> removeWithoutLock(K key) 移除key对应的对象,不加锁setListener(CacheListener<K, V> listener) 设置监听intsize()缓存的对象数量longtimeout()缓存失效时长,0表示没有设置,单位毫秒toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.miaixz.bus.core.cache.Cache
cacheObjIterator, clear, containsKey, get, get, get, prune, put, removeMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
keyLockMap
写的时候每个key一把锁,降低锁的粒度 -
cacheMap
-
capacity
protected int capacity返回缓存容量,0表示无大小限制 -
timeout
protected long timeout缓存失效时长,0表示无限制,单位毫秒 -
existCustomTimeout
protected boolean existCustomTimeout每个对象是否有单独的失效时长,用于决定清理过期对象是否有必要。 -
hitCount
命中数,即命中缓存计数 -
missCount
丢失数,即未命中缓存计数 -
listener
缓存监听
-
-
Constructor Details
-
AbstractCache
public AbstractCache()
-
-
Method Details
-
put
Description copied from interface:Cache将对象加入到缓存,使用默认失效时长 -
putWithoutLock
加入元素,无锁- Parameters:
key- 键object- 值timeout- 超时时长
-
getHitCount
public long getHitCount()- Returns:
- 命中数
-
getMissCount
public long getMissCount()- Returns:
- 丢失数
-
get
Description copied from interface:Cache从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回
null,否则返回值。每次调用此方法会可选是否刷新最后访问时间,
true表示会重新计算超时时间。 -
get
Description copied from interface:Cache从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回
null,否则返回值。每次调用此方法会可选是否刷新最后访问时间,
true表示会重新计算超时时间。 -
getWithoutLock
获取键对应的CacheObject- Parameters:
key- 键,实际使用时会被包装为MutableObject- Returns:
CacheObject
-
iterator
-
pruneCache
protected abstract int pruneCache()清理实现 子类实现此方法时无需加锁- Returns:
- 清理数
-
capacity
public int capacity()Description copied from interface:Cache返回缓存容量,0表示无大小限制 -
timeout
public long timeout()Description copied from interface:Cache缓存失效时长,0表示没有设置,单位毫秒 -
isPruneExpiredActive
protected boolean isPruneExpiredActive()只有设置公共缓存失效时长或每个对象单独的失效时长时清理可用- Returns:
- 过期对象清理是否可用,内部使用
-
isFull
public boolean isFull()Description copied from interface:Cache缓存是否已满,仅用于有空间限制的缓存对象 -
size
public int size()Description copied from interface:Cache缓存的对象数量 -
isEmpty
public boolean isEmpty()Description copied from interface:Cache缓存是否为空 -
toString
-
setListener
设置监听- Specified by:
setListenerin interfaceCache<K,V> - Parameters:
listener- 监听- Returns:
- this
-
keySet
返回所有键- Returns:
- 所有键
-
onRemove
对象移除回调。默认无动作 子类可重写此方法用于监听移除事件,如果重写,listener将无效- Parameters:
key- 键cachedObject- 被缓存的对象
-
removeWithoutLock
移除key对应的对象,不加锁- Parameters:
key- 键- Returns:
- 移除的对象,无返回null
-
cacheObjIter
获取所有CacheObject值的Iterator形式- Returns:
Iterator
-