K - 键类型V - 值类型public abstract class AbstractCache<K extends Serializable,V extends Serializable> extends Object implements Cache<K,V>
prune 策略| 限定符和类型 | 字段和说明 |
|---|---|
protected boolean |
existCustomTimeout
每个对象是否有单独的失效时长,用于决定清理过期对象是否有必要。
|
protected LongAdder |
hitCount
命中数,即命中缓存计数
|
protected ConcurrentHashMap<K,Lock> |
keyLockMap
写的时候每个key一把锁,降低锁的粒度
|
protected CacheListener<K,V> |
listener
缓存监听
|
protected LongAdder |
missCount
丢失数,即未命中缓存计数
|
| 限定符和类型 | 方法和说明 |
|---|---|
protected Iterator<CacheObj<K,V>> |
cacheObjIter()
|
int |
capacity()
返回缓存容量,
0表示无大小限制 |
void |
clear()
清空缓存
|
V |
get(K key,
boolean isUpdateLastAccess,
Supplier<V> supplier)
从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象
调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回
null,否则返回值。 |
long |
getHitCount() |
long |
getMissCount() |
protected CacheObj<K,V> |
getWithoutLock(K key)
获取键对应的
CacheObj |
boolean |
isEmpty()
缓存是否为空
|
boolean |
isFull()
缓存是否已满,仅用于有空间限制的缓存对象
|
protected boolean |
isPruneExpiredActive()
只有设置公共缓存失效时长或每个对象单独的失效时长时清理可用
|
Set<K> |
keySet()
返回所有键
|
protected void |
onRemove(K key,
V cachedObject)
对象移除回调。
|
protected abstract int |
pruneCache()
清理实现
子类实现此方法时无需加锁 |
void |
put(K key,
V object)
将对象加入到缓存,使用默认失效时长
|
protected void |
putWithoutLock(K key,
V object,
long timeout)
加入元素,无锁
|
protected CacheObj<K,V> |
removeWithoutLock(K key,
boolean withMissCount)
移除key对应的对象,不加锁
|
AbstractCache<K,V> |
setListener(CacheListener<K,V> listener)
设置监听
|
int |
size()
缓存的对象数量
|
long |
timeout()
缓存失效时长,
0 表示没有设置,单位毫秒 |
String |
toString() |
protected final ConcurrentHashMap<K extends Serializable,Lock> keyLockMap
protected boolean existCustomTimeout
protected LongAdder hitCount
protected LongAdder missCount
protected transient CacheListener<K extends Serializable,V extends Serializable> listener
public void put(K key, V object)
Cacheput 在接口中 Cache<K extends Serializable,V extends Serializable>key - 键object - 缓存的对象Cache#put(Object, Object, long)protected void putWithoutLock(K key, V object, long timeout)
key - 键object - 值timeout - 超时时长public long getHitCount()
public long getMissCount()
public V get(K key, boolean isUpdateLastAccess, Supplier<V> supplier)
Cache
调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。
每次调用此方法会可选是否刷新最后访问时间,true表示会重新计算超时时间。
get 在接口中 Cache<K extends Serializable,V extends Serializable>key - 键isUpdateLastAccess - 是否更新最后访问时间,即重新计算超时时间。supplier - 如果不存在回调方法,用于生产值对象protected CacheObj<K,V> getWithoutLock(K key)
CacheObjkey - 键CacheObjprotected abstract int pruneCache()
public int capacity()
Cache0表示无大小限制capacity 在接口中 Cache<K extends Serializable,V extends Serializable>0表示无大小限制public long timeout()
Cache0 表示没有设置,单位毫秒timeout 在接口中 Cache<K extends Serializable,V extends Serializable>protected boolean isPruneExpiredActive()
public boolean isFull()
CacheisFull 在接口中 Cache<K extends Serializable,V extends Serializable>public void clear()
Cacheclear 在接口中 Cache<K extends Serializable,V extends Serializable>public int size()
Cachesize 在接口中 Cache<K extends Serializable,V extends Serializable>public boolean isEmpty()
CacheisEmpty 在接口中 Cache<K extends Serializable,V extends Serializable>public AbstractCache<K,V> setListener(CacheListener<K,V> listener)
setListener 在接口中 Cache<K extends Serializable,V extends Serializable>listener - 监听protected void onRemove(K key, V cachedObject)
key - 键cachedObject - 被缓存的对象protected CacheObj<K,V> removeWithoutLock(K key, boolean withMissCount)
key - 键withMissCount - 是否计数丢失数Copyright © 2023. All rights reserved.