K - 键类型V - 值类型public abstract class LockedCache<K,V> extends AbstractCache<K,V>
Lock保护的缓存,读写都使用悲观锁完成,主要避免某些Map无法使用读写锁的问题| Modifier and Type | Field and Description |
|---|---|
protected Lock |
lock
一些特殊缓存,例如使用了LinkedHashMap的缓存,由于get方法也会改变Map的结构,导致无法使用读写锁
|
cacheMap, capacity, existCustomTimeout, hitCount, keyLockMap, listener, missCount, timeout| Constructor and Description |
|---|
LockedCache() |
| Modifier and Type | Method and Description |
|---|---|
Iterator<CacheObj<K,V>> |
cacheObjIterator()
返回包含键和值得迭代器
|
void |
clear()
清空缓存
|
boolean |
containsKey(K key)
是否包含key
|
V |
get(K key,
boolean isUpdateLastAccess)
从缓存中获得对象,当对象不在缓存中或已经过期(与当前时间差值大于超时时间)返回
null,否则返回值。 |
int |
prune()
从缓存中清理过期对象,清理策略取决于具体实现
|
void |
put(K key,
V object,
long timeout)
将对象加入到缓存,使用指定失效时长
如果缓存空间满了, Cache.prune() 将被调用以获得空间来存放新对象 |
void |
remove(K key)
从缓存中移除对象
|
String |
toString() |
cacheObjIter, capacity, get, get, getHitCount, getMissCount, getWithoutLock, isEmpty, isFull, isPruneExpiredActive, iterator, keySet, onRemove, pruneCache, put, putWithoutLock, removeWithoutLock, setListener, size, timeoutclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitforEach, spliteratorprotected Lock lock
public void put(K key, V object, long timeout)
CacheCache.prune() 将被调用以获得空间来存放新对象key - 键object - 缓存的对象timeout - 失效时长,单位毫秒public boolean containsKey(K key)
Cachekey - KEYpublic V get(K key, boolean isUpdateLastAccess)
Cachenull,否则返回值。
每次调用此方法会可选是否刷新最后访问时间,true表示会重新计算超时时间。
key - 键isUpdateLastAccess - 是否更新最后访问时间,即重新计算超时时间。public Iterator<CacheObj<K,V>> cacheObjIterator()
Cachepublic final int prune()
Cachepublic void remove(K key)
Cachekey - 键public void clear()
Cachepublic String toString()
toString in class AbstractCache<K,V>Copyright © 2025. All rights reserved.