K - 键类型V - 值类型public abstract class AbstractCache<K,V> extends Object implements Cache<K,V>
prune 策略
| 限定符和类型 | 字段和说明 |
|---|---|
protected Map<K,CacheObj<K,V>> |
cacheMap |
protected int |
capacity
返回缓存容量,
0表示无大小限制 |
protected boolean |
existCustomTimeout
每个对象是否有单独的失效时长,用于决定清理过期对象是否有必要。
|
protected int |
hitCount
命中数
|
protected int |
missCount
丢失数
|
protected long |
timeout
缓存失效时长,
0 表示无限制,单位毫秒 |
| 构造器和说明 |
|---|
AbstractCache() |
| 限定符和类型 | 方法和说明 |
|---|---|
Iterator<CacheObj<K,V>> |
cacheObjIterator()
返回包含键和值得迭代器
|
int |
capacity()
返回缓存容量,
0表示无大小限制 |
void |
clear()
清空缓存
|
boolean |
containsKey(K key)
是否包含key
|
V |
get(K key)
从缓存中获得对象,当对象不在缓存中或已经过期返回
null
调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。 |
V |
get(K key,
boolean isUpdateLastAccess)
从缓存中获得对象,当对象不在缓存中或已经过期返回
null
调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。 |
V |
get(K key,
Func0<V> supplier)
从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象
|
int |
getHitCount() |
int |
getMissCount() |
boolean |
isEmpty()
缓存是否为空
|
boolean |
isFull()
缓存是否已满,仅用于有空间限制的缓存对象
|
protected boolean |
isPruneExpiredActive()
只有设置公共缓存失效时长或每个对象单独的失效时长时清理可用
|
Iterator<V> |
iterator()
返回缓存迭代器
|
protected void |
onRemove(K key,
V cachedObject)
对象移除回调。
|
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)
从缓存中移除对象
|
int |
size()
缓存的对象数量
|
long |
timeout()
缓存失效时长,
0 表示没有设置,单位毫秒 |
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitforEach, spliteratorprotected int capacity
0表示无大小限制protected long timeout
0 表示无限制,单位毫秒protected boolean existCustomTimeout
protected int hitCount
protected int missCount
public void put(K key, V object)
Cacheput 在接口中 Cache<K,V>key - 键object - 缓存的对象Cache.put(Object, Object, long)public void put(K key, V object, long timeout)
CacheCache.prune() 将被调用以获得空间来存放新对象put 在接口中 Cache<K,V>key - 键object - 缓存的对象timeout - 失效时长,单位毫秒Cache.put(Object, Object, long)public boolean containsKey(K key)
CachecontainsKey 在接口中 Cache<K,V>key - KEYpublic int getHitCount()
public int getMissCount()
public V get(K key)
Cachenull
调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。
每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。
get 在接口中 Cache<K,V>key - 键Cache.get(Object, boolean)public V get(K key, boolean isUpdateLastAccess)
Cachenull
调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。
public Iterator<CacheObj<K,V>> cacheObjIterator()
CachecacheObjIterator 在接口中 Cache<K,V>protected abstract int pruneCache()
public final int prune()
Cachepublic int capacity()
Cache0表示无大小限制public long timeout()
Cache0 表示没有设置,单位毫秒protected boolean isPruneExpiredActive()
public boolean isFull()
CacheCopyright © 2023. All rights reserved.