K
- 键类型V
- 值类型public abstract class AbstractCache<K,V> extends Object implements Cache<K,V>
prune
策略Modifier and Type | Field and Description |
---|---|
protected Map<K,CacheObj<K,V>> |
cacheMap |
protected int |
capacity
返回缓存容量,
0 表示无大小限制 |
protected boolean |
existCustomTimeout
每个对象是否有单独的失效时长,用于决定清理过期对象是否有必要。
|
protected AtomicLong |
hitCount
命中数,即命中缓存计数
|
protected Map<K,Lock> |
keyLockMap
写的时候每个key一把锁,降低锁的粒度
|
protected CacheListener<K,V> |
listener
缓存监听
|
protected AtomicLong |
missCount
丢失数,即未命中缓存计数
|
protected long |
timeout
缓存失效时长,
0 表示无限制,单位毫秒 |
Constructor and Description |
---|
AbstractCache() |
Modifier and Type | Method and Description |
---|---|
Iterator<CacheObj<K,V>> |
cacheObjIterator()
返回包含键和值得迭代器
|
int |
capacity()
返回缓存容量,
0 表示无大小限制 |
void |
clear()
清空缓存
|
boolean |
containsKey(K key)
是否包含key
|
V |
get(K key,
boolean isUpdateLastAccess)
从缓存中获得对象,当对象不在缓存中或已经过期返回
null |
V |
get(K key,
boolean isUpdateLastAccess,
Func0<V> supplier)
从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象
|
long |
getHitCount() |
long |
getMissCount() |
boolean |
isEmpty()
缓存是否为空
|
boolean |
isFull()
缓存是否已满,仅用于有空间限制的缓存对象
|
protected boolean |
isPruneExpiredActive()
只有设置公共缓存失效时长或每个对象单独的失效时长时清理可用
|
Iterator<V> |
iterator() |
Set<K> |
keySet()
返回所有键
|
protected void |
onRemove(K key,
V cachedObject)
对象移除回调。默认无动作
子类可重写此方法用于监听移除事件,如果重写,listener将无效 |
int |
prune()
从缓存中清理过期对象,清理策略取决于具体实现
|
protected abstract int |
pruneCache()
清理实现
子类实现此方法时无需加锁 |
void |
put(K key,
V object)
将对象加入到缓存,使用默认失效时长
|
void |
put(K key,
V object,
long timeout)
将对象加入到缓存,使用指定失效时长
如果缓存空间满了, Cache.prune() 将被调用以获得空间来存放新对象 |
void |
remove(K key)
从缓存中移除对象
|
AbstractCache<K,V> |
setListener(CacheListener<K,V> listener)
设置监听
|
int |
size()
缓存的对象数量
|
long |
timeout()
缓存失效时长,
0 表示没有设置,单位毫秒 |
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
protected int capacity
0
表示无大小限制protected long timeout
0
表示无限制,单位毫秒protected boolean existCustomTimeout
protected AtomicLong hitCount
protected AtomicLong missCount
protected CacheListener<K,V> listener
public void put(K key, V object)
Cache
put
in interface Cache<K,V>
key
- 键object
- 缓存的对象Cache.put(Object, Object, long)
public void put(K key, V object, long timeout)
Cache
Cache.prune()
将被调用以获得空间来存放新对象public boolean containsKey(K key)
Cache
containsKey
in interface Cache<K,V>
key
- KEYpublic long getHitCount()
public long getMissCount()
public V get(K key, boolean isUpdateLastAccess, Func0<V> supplier)
Cache
调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null
,否则返回值。
每次调用此方法会可选是否刷新最后访问时间,true
表示会重新计算超时时间。
public V get(K key, boolean isUpdateLastAccess)
Cache
null
调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null
,否则返回值。
每次调用此方法会可选是否刷新最后访问时间,true
表示会重新计算超时时间。
public Iterator<CacheObj<K,V>> cacheObjIterator()
Cache
cacheObjIterator
in interface Cache<K,V>
protected abstract int pruneCache()
public final int prune()
Cache
public int capacity()
Cache
0
表示无大小限制public long timeout()
Cache
0
表示没有设置,单位毫秒protected boolean isPruneExpiredActive()
public boolean isFull()
Cache
public void clear()
Cache
public int size()
Cache
public boolean isEmpty()
Cache
public AbstractCache<K,V> setListener(CacheListener<K,V> listener)
setListener
in interface Cache<K,V>
listener
- 监听Copyright © 2021. All rights reserved.