Package com.xxl.tool.cache.impl
Class ReentrantCache<K,V>
java.lang.Object
com.xxl.tool.cache.impl.ReentrantCache<K,V>
- Type Parameters:
K- 键类型V- 值类型
- All Implemented Interfaces:
Cache<K,,V> Serializable
- Direct Known Subclasses:
FIFOCache,LFUCache,LRUCache,UnlimitedCache
Reentrant Cache
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescription缓存数据protected int缓存容量,0表示无限制protected boolean缓存过期方式: - true: expireAfterWrite , 从缓存写入开始计算,有效期到期则被清除 - false: expireAfterAccess, 从缓存最后一次访问开始计算,有效期到期则被清除protected LongAdder缓存命中计数protected CacheListener<K,V> 缓存监听protected CacheLoader<K,V> 缓存加载器protected LongAdder缓存未命中计数protected long缓存有效期,0表示无限制,单位毫秒protected final intwrite lock countprotected final ConcurrentMap<Long,Lock> write key map -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasMap()返回 键-值 映射,仅包括有效数据intcapacity()缓存容量,0表示无限制voidclear()清空缓存对象booleancontainsKey(K key) 是否包含keyprotected abstract intdoPrune()清理实现
子类实现此方法时无需加锁获取缓存对象get(K key, CacheLoader<K, V> cacheLoader) 获取缓存对象getIfPresent(K key) 获取缓存对象longhitCount()booleanisEmpty()缓存是否为空booleanisFull()缓存是否已满,仅用于有空间限制的缓存对象longprotected voidonRemove(CacheObject<K, V> cacheObject) 对象移除回调final intprune()清理过期缓存对象void设置缓存对象void删除单个缓存对象protected CacheObject<K,V> removeWithoutLock(K key) 移除key对应的对象,不加锁setListener(CacheListener<K, V> listener) 设置监听setLoader(CacheLoader<K, V> loader) 设置监听intsize()缓存对象数量longtimeout()缓存有效期,0表示无限制,单位毫秒toString()是否支持过期对象清理,仅设置 过期时间 才支持;
-
Field Details
-
timeout
protected long timeout缓存有效期,0表示无限制,单位毫秒 -
capacity
protected int capacity缓存容量,0表示无限制 -
expireType
protected boolean expireType缓存过期方式: - true: expireAfterWrite , 从缓存写入开始计算,有效期到期则被清除 - false: expireAfterAccess, 从缓存最后一次访问开始计算,有效期到期则被清除 -
writeLockMap
write key map -
writeLockCount
protected final int writeLockCountwrite lock count- See Also:
-
cacheMap
缓存数据 -
hitCount
缓存命中计数 -
missCount
缓存未命中计数 -
listener
缓存监听 -
loader
缓存加载器
-
-
Constructor Details
-
ReentrantCache
public ReentrantCache()
-
-
Method Details
-
put
Description copied from interface:Cache设置缓存对象 -
get
Description copied from interface:Cache获取缓存对象1、缓存对象不存在 或 已过期:尝试使用 cacheLoader 加载并返回对象,如果 cacheLoader 不存在返回
null2、每次调用此方法会刷新最后访问时间,即重新计算超时时间 -
getIfPresent
Description copied from interface:Cache获取缓存对象1、缓存对象不存在 或 已过期:直接返回
null2、每次调用此方法会刷新最后访问时间,即重新计算超时时间- Specified by:
getIfPresentin interfaceCache<K,V> - Parameters:
key- 键- Returns:
- 值对象
-
get
Description copied from interface:Cache获取缓存对象1、缓存对象不存在 或 已过期:使用 cacheLoader 加载并返回对象 2、每次调用此方法,可选是否 刷新最后访问时间,即重新计算超时时间
-
containsKey
Description copied from interface:Cache是否包含key- Specified by:
containsKeyin interfaceCache<K,V> - Parameters:
key- 键- Returns:
- 值对象
-
asMap
Description copied from interface:Cache返回 键-值 映射,仅包括有效数据 -
size
public int size()Description copied from interface:Cache缓存对象数量1、包含过期数据
-
isFull
public boolean isFull()Description copied from interface:Cache缓存是否已满,仅用于有空间限制的缓存对象1、包含过期数据
-
isEmpty
public boolean isEmpty()Description copied from interface:Cache缓存是否为空1、包含过期数据
-
remove
Description copied from interface:Cache删除单个缓存对象 -
removeWithoutLock
移除key对应的对象,不加锁- Parameters:
key- 键- Returns:
- 移除的对象,无返回null
-
onRemove
对象移除回调- Parameters:
cacheObject- 被缓存的对象
-
prune
public final int prune()Description copied from interface:Cache清理过期缓存对象 -
doPrune
protected abstract int doPrune()清理实现
子类实现此方法时无需加锁- Returns:
- 清理数据量
-
clear
public void clear()Description copied from interface:Cache清空缓存对象 -
hitCount
public long hitCount() -
missCount
public long missCount() -
capacity
public int capacity()Description copied from interface:Cache缓存容量,0表示无限制 -
timeout
public long timeout()Description copied from interface:Cache缓存有效期,0表示无限制,单位毫秒 -
setListener
设置监听- Specified by:
setListenerin interfaceCache<K,V>
-
setLoader
Description copied from interface:Cache设置监听 -
toString
是否支持过期对象清理,仅设置 过期时间 才支持;
-