mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
clean history
This commit is contained in:
149
hutool-cache/src/main/java/cn/hutool/cache/Cache.java
vendored
Normal file
149
hutool-cache/src/main/java/cn/hutool/cache/Cache.java
vendored
Normal file
@@ -0,0 +1,149 @@
|
||||
package cn.hutool.cache;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Iterator;
|
||||
|
||||
import cn.hutool.cache.impl.CacheObj;
|
||||
import cn.hutool.core.lang.func.Func0;
|
||||
|
||||
/**
|
||||
* 缓存接口
|
||||
*
|
||||
* @author Looly,jodd
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
*/
|
||||
public interface Cache<K, V> extends Iterable<V>, Serializable {
|
||||
|
||||
/**
|
||||
* 返回缓存容量,<code>0</code>表示无大小限制
|
||||
*
|
||||
* @return 返回缓存容量,<code>0</code>表示无大小限制
|
||||
*/
|
||||
int capacity();
|
||||
|
||||
/**
|
||||
* 缓存失效时长, <code>0</code> 表示没有设置,单位毫秒
|
||||
*
|
||||
* @return 缓存失效时长, <code>0</code> 表示没有设置,单位毫秒
|
||||
*/
|
||||
long timeout();
|
||||
|
||||
/**
|
||||
* 将对象加入到缓存,使用默认失效时长
|
||||
*
|
||||
* @param key 键
|
||||
* @param object 缓存的对象
|
||||
* @see Cache#put(Object, Object, long)
|
||||
*/
|
||||
void put(K key, V object);
|
||||
|
||||
/**
|
||||
* 将对象加入到缓存,使用指定失效时长<br>
|
||||
* 如果缓存空间满了,{@link #prune()} 将被调用以获得空间来存放新对象
|
||||
*
|
||||
* @param key 键
|
||||
* @param object 缓存的对象
|
||||
* @param timeout 失效时长,单位毫秒
|
||||
* @see Cache#put(Object, Object, long)
|
||||
*/
|
||||
void put(K key, V object, long timeout);
|
||||
|
||||
/**
|
||||
* 从缓存中获得对象,当对象不在缓存中或已经过期返回<code>null</code>
|
||||
* <p>
|
||||
* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回<code>null</code>,否则返回值。
|
||||
* <p>
|
||||
* 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。
|
||||
*
|
||||
* @param key 键
|
||||
* @return 键对应的对象
|
||||
* @see #get(Object, boolean)
|
||||
*/
|
||||
V get(K key);
|
||||
|
||||
/**
|
||||
* 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象
|
||||
*
|
||||
* @param key 键
|
||||
* @param supplier 如果不存在回调方法,用于生产值对象
|
||||
* @return 值对象
|
||||
*/
|
||||
V get(K key, Func0<V> supplier);
|
||||
|
||||
/**
|
||||
* 从缓存中获得对象,当对象不在缓存中或已经过期返回<code>null</code>
|
||||
* <p>
|
||||
* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回<code>null</code>,否则返回值。
|
||||
*
|
||||
* @param key 键
|
||||
* @param isUpdateLastAccess 是否更新最后访问时间,即重新计算超时时间。
|
||||
* @return 键对应的对象
|
||||
*/
|
||||
V get(K key, boolean isUpdateLastAccess);
|
||||
|
||||
/**
|
||||
* 返回缓存迭代器
|
||||
*
|
||||
* @return 缓存迭代器
|
||||
*/
|
||||
@Override
|
||||
Iterator<V> iterator();
|
||||
|
||||
/**
|
||||
* 返回包含键和值得迭代器
|
||||
*
|
||||
* @return 缓存对象迭代器
|
||||
* @since 4.0.10
|
||||
*/
|
||||
Iterator<CacheObj<K, V>> cacheObjIterator();
|
||||
|
||||
/**
|
||||
* 从缓存中清理过期对象,清理策略取决于具体实现
|
||||
*
|
||||
* @return 清理的缓存对象个数
|
||||
*/
|
||||
int prune();
|
||||
|
||||
/**
|
||||
* 缓存是否已满,仅用于有空间限制的缓存对象
|
||||
*
|
||||
* @return 缓存是否已满,仅用于有空间限制的缓存对象
|
||||
*/
|
||||
boolean isFull();
|
||||
|
||||
/**
|
||||
* 从缓存中移除对象
|
||||
*
|
||||
* @param key 键
|
||||
*/
|
||||
void remove(K key);
|
||||
|
||||
/**
|
||||
* 清空缓存
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* 缓存的对象数量
|
||||
*
|
||||
* @return 缓存的对象数量
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* 缓存是否为空
|
||||
*
|
||||
* @return 缓存是否为空
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* 是否包含key
|
||||
*
|
||||
* @param key KEY
|
||||
* @return 是否包含key
|
||||
*/
|
||||
boolean containsKey(K key);
|
||||
}
|
129
hutool-cache/src/main/java/cn/hutool/cache/CacheUtil.java
vendored
Normal file
129
hutool-cache/src/main/java/cn/hutool/cache/CacheUtil.java
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
package cn.hutool.cache;
|
||||
|
||||
import cn.hutool.cache.impl.FIFOCache;
|
||||
import cn.hutool.cache.impl.LFUCache;
|
||||
import cn.hutool.cache.impl.LRUCache;
|
||||
import cn.hutool.cache.impl.NoCache;
|
||||
import cn.hutool.cache.impl.TimedCache;
|
||||
import cn.hutool.cache.impl.WeakCache;
|
||||
|
||||
/**
|
||||
* 缓存工具类
|
||||
* @author Looly
|
||||
*@since 3.0.1
|
||||
*/
|
||||
public class CacheUtil {
|
||||
|
||||
/**
|
||||
* 创建FIFO(first in first out) 先进先出缓存.
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
* @param timeout 过期时长,单位:毫秒
|
||||
* @return {@link FIFOCache}
|
||||
*/
|
||||
public static <K, V> FIFOCache<K, V> newFIFOCache(int capacity, long timeout){
|
||||
return new FIFOCache<K, V>(capacity, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建FIFO(first in first out) 先进先出缓存.
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
* @return {@link FIFOCache}
|
||||
*/
|
||||
public static <K, V> FIFOCache<K, V> newFIFOCache(int capacity){
|
||||
return new FIFOCache<K, V>(capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建LFU(least frequently used) 最少使用率缓存.
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
* @param timeout 过期时长,单位:毫秒
|
||||
* @return {@link LFUCache}
|
||||
*/
|
||||
public static <K, V> LFUCache<K, V> newLFUCache(int capacity, long timeout){
|
||||
return new LFUCache<K, V>(capacity, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建LFU(least frequently used) 最少使用率缓存.
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
* @return {@link LFUCache}
|
||||
*/
|
||||
public static <K, V> LFUCache<K, V> newLFUCache(int capacity){
|
||||
return new LFUCache<K, V>(capacity);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建LRU (least recently used)最近最久未使用缓存.
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
* @param timeout 过期时长,单位:毫秒
|
||||
* @return {@link LRUCache}
|
||||
*/
|
||||
public static <K, V> LRUCache<K, V> newLRUCache(int capacity, long timeout){
|
||||
return new LRUCache<K, V>(capacity, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建LRU (least recently used)最近最久未使用缓存.
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
* @return {@link LRUCache}
|
||||
*/
|
||||
public static <K, V> LRUCache<K, V> newLRUCache(int capacity){
|
||||
return new LRUCache<K, V>(capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建定时缓存.
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param timeout 过期时长,单位:毫秒
|
||||
* @return {@link TimedCache}
|
||||
*/
|
||||
public static <K, V> TimedCache<K, V> newTimedCache(long timeout){
|
||||
return new TimedCache<K, V>(timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建弱引用缓存.
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param timeout 过期时长,单位:毫秒
|
||||
* @return {@link WeakCache}
|
||||
* @since 3.0.7
|
||||
*/
|
||||
public static <K, V> WeakCache<K, V> newWeakCache(long timeout){
|
||||
return new WeakCache<K, V>(timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建无缓存实现.
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @return {@link NoCache}
|
||||
*/
|
||||
public static <K, V> NoCache<K, V> newNoCache(){
|
||||
return new NoCache<K, V>();
|
||||
}
|
||||
|
||||
}
|
83
hutool-cache/src/main/java/cn/hutool/cache/GlobalPruneTimer.java
vendored
Normal file
83
hutool-cache/src/main/java/cn/hutool/cache/GlobalPruneTimer.java
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
package cn.hutool.cache;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 全局缓存清理定时器池,用于在需要过期支持的缓存对象中超时任务池
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public enum GlobalPruneTimer {
|
||||
/** 单例对象 */
|
||||
INSTANCE;
|
||||
|
||||
/** 缓存任务计数 */
|
||||
private AtomicInteger cacheTaskNumber = new AtomicInteger(1);
|
||||
|
||||
/** 定时器 */
|
||||
private ScheduledExecutorService pruneTimer;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
private GlobalPruneTimer() {
|
||||
create();
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动定时任务
|
||||
*
|
||||
* @param task 任务
|
||||
* @param delay 周期
|
||||
* @return {@link ScheduledFuture}对象,可手动取消此任务
|
||||
*/
|
||||
public ScheduledFuture<?> schedule(Runnable task, long delay) {
|
||||
return this.pruneTimer.scheduleAtFixedRate(task, delay, delay, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建定时器
|
||||
*/
|
||||
public void create() {
|
||||
if (null != pruneTimer) {
|
||||
shutdownNow();
|
||||
}
|
||||
this.pruneTimer = new ScheduledThreadPoolExecutor(16, new ThreadFactory() {
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
return ThreadUtil.newThread(r, StrUtil.format("Pure-Timer-{}", cacheTaskNumber.getAndIncrement()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁全局定时器
|
||||
*/
|
||||
public void shutdown() {
|
||||
if (null != pruneTimer) {
|
||||
pruneTimer.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁全局定时器
|
||||
*
|
||||
* @return 销毁时未被执行的任务列表
|
||||
*/
|
||||
public List<Runnable> shutdownNow() {
|
||||
if (null != pruneTimer) {
|
||||
return pruneTimer.shutdownNow();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
134
hutool-cache/src/main/java/cn/hutool/cache/file/AbstractFileCache.java
vendored
Normal file
134
hutool-cache/src/main/java/cn/hutool/cache/file/AbstractFileCache.java
vendored
Normal file
@@ -0,0 +1,134 @@
|
||||
package cn.hutool.cache.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
|
||||
import cn.hutool.cache.Cache;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
|
||||
/**
|
||||
* 文件缓存,以解决频繁读取文件引起的性能问题
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractFileCache implements Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 容量 */
|
||||
protected final int capacity;
|
||||
/** 缓存的最大文件大小,文件大于此大小时将不被缓存 */
|
||||
protected final int maxFileSize;
|
||||
/** 默认超时时间,0表示无默认超时 */
|
||||
protected final long timeout;
|
||||
/** 缓存实现 */
|
||||
protected final Cache<File, byte[]> cache;
|
||||
|
||||
/** 已使用缓存空间 */
|
||||
protected int usedSize;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param capacity 缓存容量
|
||||
* @param maxFileSize 文件最大大小
|
||||
* @param timeout 默认超时时间,0表示无默认超时
|
||||
*/
|
||||
public AbstractFileCache(int capacity, int maxFileSize, long timeout) {
|
||||
this.capacity = capacity;
|
||||
this.maxFileSize = maxFileSize;
|
||||
this.timeout = timeout;
|
||||
this.cache = initCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 缓存容量(byte数)
|
||||
*/
|
||||
public int capacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 已使用空间大小(byte数)
|
||||
*/
|
||||
public int getUsedSize() {
|
||||
return usedSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 允许被缓存文件的最大byte数
|
||||
*/
|
||||
public int maxFileSize() {
|
||||
return maxFileSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 缓存的文件数
|
||||
*/
|
||||
public int getCachedFilesCount() {
|
||||
return cache.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 超时时间
|
||||
*/
|
||||
public long timeout() {
|
||||
return this.timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空缓存
|
||||
*/
|
||||
public void clear() {
|
||||
cache.clear();
|
||||
usedSize = 0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- get
|
||||
|
||||
/**
|
||||
* 获得缓存过的文件bytes
|
||||
* @param path 文件路径
|
||||
* @return 缓存过的文件bytes
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public byte[] getFileBytes(String path) throws IORuntimeException {
|
||||
return getFileBytes(new File(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得缓存过的文件bytes
|
||||
* @param file 文件
|
||||
* @return 缓存过的文件bytes
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public byte[] getFileBytes(File file) throws IORuntimeException {
|
||||
byte[] bytes = cache.get(file);
|
||||
if (bytes != null) {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// add file
|
||||
bytes = FileUtil.readBytes(file);
|
||||
|
||||
if ((maxFileSize != 0) && (file.length() > maxFileSize)) {
|
||||
//大于缓存空间,不缓存,直接返回
|
||||
return bytes;
|
||||
}
|
||||
|
||||
usedSize += bytes.length;
|
||||
|
||||
//文件放入缓存,如果usedSize > capacity,purge()方法将被调用
|
||||
cache.put(file, bytes);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- protected method start
|
||||
/**
|
||||
* 初始化实现文件缓存的缓存对象
|
||||
* @return {@link Cache}
|
||||
*/
|
||||
protected abstract Cache<File, byte[]> initCache();
|
||||
// ---------------------------------------------------------------- protected method end
|
||||
|
||||
}
|
64
hutool-cache/src/main/java/cn/hutool/cache/file/LFUFileCache.java
vendored
Normal file
64
hutool-cache/src/main/java/cn/hutool/cache/file/LFUFileCache.java
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
package cn.hutool.cache.file;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import cn.hutool.cache.Cache;
|
||||
import cn.hutool.cache.impl.LFUCache;
|
||||
|
||||
/**
|
||||
* 使用LFU缓存文件,以解决频繁读取文件引起的性能问题
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class LFUFileCache extends AbstractFileCache{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 构造<br>
|
||||
* 最大文件大小为缓存容量的一半<br>
|
||||
* 默认无超时
|
||||
* @param capacity 缓存容量
|
||||
*/
|
||||
public LFUFileCache(int capacity) {
|
||||
this(capacity, capacity / 2, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造<br>
|
||||
* 默认无超时
|
||||
* @param capacity 缓存容量
|
||||
* @param maxFileSize 最大文件大小
|
||||
*/
|
||||
public LFUFileCache(int capacity, int maxFileSize) {
|
||||
this(capacity, maxFileSize, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param capacity 缓存容量
|
||||
* @param maxFileSize 文件最大大小
|
||||
* @param timeout 默认超时时间,0表示无默认超时
|
||||
*/
|
||||
public LFUFileCache(int capacity, int maxFileSize, long timeout) {
|
||||
super(capacity, maxFileSize, timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Cache<File, byte[]> initCache() {
|
||||
Cache<File, byte[]> cache = new LFUCache<File, byte[]>(this.capacity, this.timeout) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean isFull() {
|
||||
return LFUFileCache.this.usedSize > this.capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRemove(File key, byte[] cachedObject) {
|
||||
usedSize -= cachedObject.length;
|
||||
}
|
||||
};
|
||||
return cache;
|
||||
}
|
||||
|
||||
}
|
64
hutool-cache/src/main/java/cn/hutool/cache/file/LRUFileCache.java
vendored
Normal file
64
hutool-cache/src/main/java/cn/hutool/cache/file/LRUFileCache.java
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
package cn.hutool.cache.file;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import cn.hutool.cache.Cache;
|
||||
import cn.hutool.cache.impl.LRUCache;
|
||||
|
||||
/**
|
||||
* 使用LRU缓存文件,以解决频繁读取文件引起的性能问题
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class LRUFileCache extends AbstractFileCache{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 构造<br>
|
||||
* 最大文件大小为缓存容量的一半<br>
|
||||
* 默认无超时
|
||||
* @param capacity 缓存容量
|
||||
*/
|
||||
public LRUFileCache(int capacity) {
|
||||
this(capacity, capacity / 2, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造<br>
|
||||
* 默认无超时
|
||||
* @param capacity 缓存容量
|
||||
* @param maxFileSize 最大文件大小
|
||||
*/
|
||||
public LRUFileCache(int capacity, int maxFileSize) {
|
||||
this(capacity, maxFileSize, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param capacity 缓存容量
|
||||
* @param maxFileSize 文件最大大小
|
||||
* @param timeout 默认超时时间,0表示无默认超时
|
||||
*/
|
||||
public LRUFileCache(int capacity, int maxFileSize, long timeout) {
|
||||
super(capacity, maxFileSize, timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Cache<File, byte[]> initCache() {
|
||||
Cache<File, byte[]> cache = new LRUCache<File, byte[]>(this.capacity, super.timeout) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean isFull() {
|
||||
return LRUFileCache.this.usedSize > this.capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRemove(File key, byte[] cachedObject) {
|
||||
usedSize -= cachedObject.length;
|
||||
}
|
||||
};
|
||||
return cache;
|
||||
}
|
||||
|
||||
}
|
7
hutool-cache/src/main/java/cn/hutool/cache/file/package-info.java
vendored
Normal file
7
hutool-cache/src/main/java/cn/hutool/cache/file/package-info.java
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* 提供针对文件的缓存实现
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.cache.file;
|
359
hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java
vendored
Normal file
359
hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java
vendored
Normal file
@@ -0,0 +1,359 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
|
||||
|
||||
import cn.hutool.cache.Cache;
|
||||
import cn.hutool.core.collection.CopiedIter;
|
||||
import cn.hutool.core.lang.func.Func0;
|
||||
|
||||
/**
|
||||
* 超时和限制大小的缓存的默认实现<br>
|
||||
* 继承此抽象缓存需要:<br>
|
||||
* <ul>
|
||||
* <li>创建一个新的Map</li>
|
||||
* <li>实现 <code>prune</code> 策略</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Looly,jodd
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
*/
|
||||
public abstract class AbstractCache<K, V> implements Cache<K, V> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected Map<K, CacheObj<K, V>> cacheMap;
|
||||
|
||||
private final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock();
|
||||
private final ReadLock readLock = cacheLock.readLock();
|
||||
private final WriteLock writeLock = cacheLock.writeLock();
|
||||
|
||||
/** 返回缓存容量,<code>0</code>表示无大小限制 */
|
||||
protected int capacity;
|
||||
/** 缓存失效时长, <code>0</code> 表示无限制,单位毫秒 */
|
||||
protected long timeout;
|
||||
|
||||
/** 每个对象是否有单独的失效时长,用于决定清理过期对象是否有必要。 */
|
||||
protected boolean existCustomTimeout;
|
||||
|
||||
/** 命中数 */
|
||||
protected int hitCount;
|
||||
/** 丢失数 */
|
||||
protected int missCount;
|
||||
|
||||
// ---------------------------------------------------------------- put start
|
||||
@Override
|
||||
public void put(K key, V object) {
|
||||
put(key, object, timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(K key, V object, long timeout) {
|
||||
writeLock.lock();
|
||||
|
||||
try {
|
||||
putWithoutLock(key, object, timeout);
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入元素,无锁
|
||||
*
|
||||
* @param key 键
|
||||
* @param object 值
|
||||
* @param timeout 超时时长
|
||||
* @since 4.5.16
|
||||
*/
|
||||
private void putWithoutLock(K key, V object, long timeout) {
|
||||
CacheObj<K, V> co = new CacheObj<K, V>(key, object, timeout);
|
||||
if (timeout != 0) {
|
||||
existCustomTimeout = true;
|
||||
}
|
||||
if (isFull()) {
|
||||
pruneCache();
|
||||
}
|
||||
cacheMap.put(key, co);
|
||||
}
|
||||
// ---------------------------------------------------------------- put end
|
||||
|
||||
// ---------------------------------------------------------------- get start
|
||||
@Override
|
||||
public boolean containsKey(K key) {
|
||||
readLock.lock();
|
||||
|
||||
try {
|
||||
// 不存在或已移除
|
||||
final CacheObj<K, V> co = cacheMap.get(key);
|
||||
if (co == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false == co.isExpired()) {
|
||||
// 命中
|
||||
return true;
|
||||
}
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
// 过期
|
||||
remove(key, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 命中数
|
||||
*/
|
||||
public int getHitCount() {
|
||||
this.readLock.lock();
|
||||
try {
|
||||
return hitCount;
|
||||
} finally {
|
||||
this.readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 丢失数
|
||||
*/
|
||||
public int getMissCount() {
|
||||
this.readLock.lock();
|
||||
try {
|
||||
return missCount;
|
||||
} finally {
|
||||
this.readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(K key) {
|
||||
return get(key, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(K key, Func0<V> supplier) {
|
||||
V v = get(key);
|
||||
if (null == v && null != supplier) {
|
||||
writeLock.lock();
|
||||
try {
|
||||
// 双重检查锁
|
||||
final CacheObj<K, V> co = cacheMap.get(key);
|
||||
if(null == co || co.isExpired() || null == co.getValue()) {
|
||||
try {
|
||||
v = supplier.call();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
putWithoutLock(key, v, this.timeout);
|
||||
} else {
|
||||
v = co.get(true);
|
||||
}
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(K key, boolean isUpdateLastAccess) {
|
||||
readLock.lock();
|
||||
|
||||
try {
|
||||
// 不存在或已移除
|
||||
final CacheObj<K, V> co = cacheMap.get(key);
|
||||
if (co == null) {
|
||||
missCount++;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (false == co.isExpired()) {
|
||||
// 命中
|
||||
hitCount++;
|
||||
return co.get(isUpdateLastAccess);
|
||||
}
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
// 过期
|
||||
remove(key, true);
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- get end
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Iterator<V> iterator() {
|
||||
CacheObjIterator<K, V> copiedIterator = (CacheObjIterator<K, V>) this.cacheObjIterator();
|
||||
return new CacheValuesIterator<V>(copiedIterator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<CacheObj<K, V>> cacheObjIterator() {
|
||||
CopiedIter<CacheObj<K, V>> copiedIterator;
|
||||
readLock.lock();
|
||||
try {
|
||||
copiedIterator = CopiedIter.copyOf(this.cacheMap.values().iterator());
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
return new CacheObjIterator<>(copiedIterator);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- prune start
|
||||
/**
|
||||
* 清理实现
|
||||
*
|
||||
* @return 清理数
|
||||
*/
|
||||
protected abstract int pruneCache();
|
||||
|
||||
@Override
|
||||
public final int prune() {
|
||||
writeLock.lock();
|
||||
try {
|
||||
return pruneCache();
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
// ---------------------------------------------------------------- prune end
|
||||
|
||||
// ---------------------------------------------------------------- common start
|
||||
@Override
|
||||
public int capacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 默认缓存失效时长。<br>
|
||||
* 每个对象可以单独设置失效时长
|
||||
*/
|
||||
@Override
|
||||
public long timeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 只有设置公共缓存失效时长或每个对象单独的失效时长时清理可用
|
||||
*
|
||||
* @return 过期对象清理是否可用,内部使用
|
||||
*/
|
||||
protected boolean isPruneExpiredActive() {
|
||||
this.readLock.lock();
|
||||
try {
|
||||
return (timeout != 0) || existCustomTimeout;
|
||||
} finally {
|
||||
this.readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFull() {
|
||||
this.readLock.lock();
|
||||
try {
|
||||
return (capacity > 0) && (cacheMap.size() >= capacity);
|
||||
} finally {
|
||||
this.readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(K key) {
|
||||
remove(key, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
writeLock.lock();
|
||||
try {
|
||||
cacheMap.clear();
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
this.readLock.lock();
|
||||
try {
|
||||
return cacheMap.size();
|
||||
} finally {
|
||||
this.readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
this.readLock.lock();
|
||||
try {
|
||||
return cacheMap.isEmpty();
|
||||
} finally {
|
||||
this.readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
this.readLock.lock();
|
||||
try {
|
||||
return this.cacheMap.toString();
|
||||
} finally {
|
||||
this.readLock.unlock();
|
||||
}
|
||||
}
|
||||
// ---------------------------------------------------------------- common end
|
||||
|
||||
/**
|
||||
* 对象移除回调。默认无动作
|
||||
*
|
||||
* @param key 键
|
||||
* @param cachedObject 被缓存的对象
|
||||
*/
|
||||
protected void onRemove(K key, V cachedObject) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除key对应的对象
|
||||
*
|
||||
* @param key 键
|
||||
* @param withMissCount 是否计数丢失数
|
||||
*/
|
||||
private void remove(K key, boolean withMissCount) {
|
||||
writeLock.lock();
|
||||
CacheObj<K, V> co;
|
||||
try {
|
||||
co = removeWithoutLock(key, withMissCount);
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
if (null != co) {
|
||||
onRemove(co.key, co.obj);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除key对应的对象,不加锁
|
||||
*
|
||||
* @param key 键
|
||||
* @param withMissCount 是否计数丢失数
|
||||
* @return 移除的对象,无返回null
|
||||
*/
|
||||
private CacheObj<K, V> removeWithoutLock(K key, boolean withMissCount) {
|
||||
final CacheObj<K, V> co = cacheMap.remove(key);
|
||||
if (withMissCount) {
|
||||
// 在丢失计数有效的情况下,移除一般为get时的超时操作,此处应该丢失数+1
|
||||
this.missCount++;
|
||||
}
|
||||
return co;
|
||||
}
|
||||
}
|
92
hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java
vendored
Normal file
92
hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 缓存对象
|
||||
* @author Looly
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
*/
|
||||
public class CacheObj<K, V> implements Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected final K key;
|
||||
protected final V obj;
|
||||
|
||||
/** 上次访问时间 */
|
||||
private long lastAccess;
|
||||
/** 访问次数 */
|
||||
protected long accessCount;
|
||||
/** 对象存活时长,0表示永久存活*/
|
||||
private long ttl;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param key 键
|
||||
* @param obj 值
|
||||
* @param ttl 超时时长
|
||||
*/
|
||||
protected CacheObj(K key, V obj, long ttl) {
|
||||
this.key = key;
|
||||
this.obj = obj;
|
||||
this.ttl = ttl;
|
||||
this.lastAccess = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否过期
|
||||
*
|
||||
* @return 是否过期
|
||||
*/
|
||||
boolean isExpired() {
|
||||
if(this.ttl > 0) {
|
||||
final long expiredTime = this.lastAccess + this.ttl;
|
||||
if(expiredTime > 0 && expiredTime < System.currentTimeMillis()) {
|
||||
// expiredTime > 0 杜绝Long类型溢出变负数问题,当当前时间超过过期时间,表示过期
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
*
|
||||
* @param isUpdateLastAccess 是否更新最后访问时间
|
||||
* @return 获得对象
|
||||
* @since 4.0.10
|
||||
*/
|
||||
V get(boolean isUpdateLastAccess) {
|
||||
if(isUpdateLastAccess) {
|
||||
lastAccess = System.currentTimeMillis();
|
||||
}
|
||||
accessCount++;
|
||||
return this.obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取键
|
||||
* @return 键
|
||||
* @since 4.0.10
|
||||
*/
|
||||
public K getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
* @return 值
|
||||
* @since 4.0.10
|
||||
*/
|
||||
public V getValue() {
|
||||
return this.obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CacheObj [key=" + key + ", obj=" + obj + ", lastAccess=" + lastAccess + ", accessCount=" + accessCount + ", ttl=" + ttl + "]";
|
||||
}
|
||||
}
|
74
hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObjIterator.java
vendored
Normal file
74
hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObjIterator.java
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* {@link cn.hutool.cache.impl.AbstractCache} 的CacheObj迭代器.
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @since 4.0.10
|
||||
*/
|
||||
public class CacheObjIterator<K, V> implements Iterator<CacheObj<K, V>>, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Iterator<CacheObj<K, V>> iterator;
|
||||
private CacheObj<K, V> nextValue;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param iterator 原{@link Iterator}
|
||||
* @param readLock 读锁
|
||||
*/
|
||||
CacheObjIterator(Iterator<CacheObj<K, V>> iterator) {
|
||||
this.iterator = iterator;
|
||||
nextValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 是否有下一个值
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextValue != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 下一个值
|
||||
*/
|
||||
@Override
|
||||
public CacheObj<K, V> next() {
|
||||
if (false == hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
final CacheObj<K, V> cachedObject = nextValue;
|
||||
nextValue();
|
||||
return cachedObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存中移除没有过期的当前值,此方法不支持
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Cache values Iterator is not support to modify.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 下一个值,当不存在则下一个值为null
|
||||
*/
|
||||
private void nextValue() {
|
||||
while (iterator.hasNext()) {
|
||||
nextValue = iterator.next();
|
||||
if (nextValue.isExpired() == false) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
nextValue = null;
|
||||
}
|
||||
}
|
49
hutool-cache/src/main/java/cn/hutool/cache/impl/CacheValuesIterator.java
vendored
Normal file
49
hutool-cache/src/main/java/cn/hutool/cache/impl/CacheValuesIterator.java
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* {@link cn.hutool.cache.impl.AbstractCache} 的值迭代器.
|
||||
* @author looly
|
||||
*
|
||||
* @param <V> 迭代对象类型
|
||||
*/
|
||||
public class CacheValuesIterator<V> implements Iterator<V>, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final CacheObjIterator<?, V> cacheObjIter;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param iterator 原{@link CacheObjIterator}
|
||||
* @param readLock 读锁
|
||||
*/
|
||||
CacheValuesIterator(CacheObjIterator<?, V> iterator) {
|
||||
this.cacheObjIter = iterator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 是否有下一个值
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return this.cacheObjIter.hasNext();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 下一个值
|
||||
*/
|
||||
@Override
|
||||
public V next() {
|
||||
return cacheObjIter.next().getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存中移除没有过期的当前值,不支持此方法
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
cacheObjIter.remove();
|
||||
}
|
||||
}
|
78
hutool-cache/src/main/java/cn/hutool/cache/impl/FIFOCache.java
vendored
Normal file
78
hutool-cache/src/main/java/cn/hutool/cache/impl/FIFOCache.java
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
/**
|
||||
* FIFO(first in first out) 先进先出缓存.
|
||||
*
|
||||
* <p>
|
||||
* 元素不停的加入缓存直到缓存满为止,当缓存满时,清理过期缓存对象,清理后依旧满则删除先入的缓存(链表首部对象)<br>
|
||||
* 优点:简单快速 <br>
|
||||
* 缺点:不灵活,不能保证最常用的对象总是被保留
|
||||
* </p>
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
*/
|
||||
public class FIFOCache<K, V> extends AbstractCache<K, V> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 构造,默认对象不过期
|
||||
*
|
||||
* @param capacity 容量
|
||||
*/
|
||||
public FIFOCache(int capacity) {
|
||||
this(capacity, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param capacity 容量
|
||||
* @param timeout 过期时长
|
||||
*/
|
||||
public FIFOCache(int capacity, long timeout) {
|
||||
if(Integer.MAX_VALUE == capacity) {
|
||||
capacity -= 1;
|
||||
}
|
||||
|
||||
this.capacity = capacity;
|
||||
this.timeout = timeout;
|
||||
cacheMap = new LinkedHashMap<K, CacheObj<K, V>>(capacity + 1, 1.0f, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 先进先出的清理策略<br>
|
||||
* 先遍历缓存清理过期的缓存对象,如果清理后还是满的,则删除第一个缓存对象
|
||||
*/
|
||||
@Override
|
||||
protected int pruneCache() {
|
||||
int count = 0;
|
||||
CacheObj<K, V> first = null;
|
||||
|
||||
// 清理过期对象并找出链表头部元素(先入元素)
|
||||
Iterator<CacheObj<K, V>> values = cacheMap.values().iterator();
|
||||
while (values.hasNext()) {
|
||||
CacheObj<K, V> co = values.next();
|
||||
if (co.isExpired()) {
|
||||
values.remove();
|
||||
count++;
|
||||
}
|
||||
if (first == null) {
|
||||
first = co;
|
||||
}
|
||||
}
|
||||
|
||||
// 清理结束后依旧是满的,则删除第一个被缓存的对象
|
||||
if (isFull() && null != first) {
|
||||
cacheMap.remove(first.key);
|
||||
onRemove(first.key, first.obj);
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
96
hutool-cache/src/main/java/cn/hutool/cache/impl/LFUCache.java
vendored
Normal file
96
hutool-cache/src/main/java/cn/hutool/cache/impl/LFUCache.java
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* LFU(least frequently used) 最少使用率缓存<br>
|
||||
* 根据使用次数来判定对象是否被持续缓存<br>
|
||||
* 使用率是通过访问次数计算的。<br>
|
||||
* 当缓存满时清理过期对象。<br>
|
||||
* 清理后依旧满的情况下清除最少访问(访问计数最小)的对象并将其他对象的访问数减去这个最小访问数,以便新对象进入后可以公平计数。
|
||||
*
|
||||
* @author Looly,jodd
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
*/
|
||||
public class LFUCache<K, V> extends AbstractCache<K, V> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param capacity 容量
|
||||
*/
|
||||
public LFUCache(int capacity) {
|
||||
this(capacity, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param capacity 容量
|
||||
* @param timeout 过期时长
|
||||
*/
|
||||
public LFUCache(int capacity, long timeout) {
|
||||
if(Integer.MAX_VALUE == capacity) {
|
||||
capacity -= 1;
|
||||
}
|
||||
|
||||
this.capacity = capacity;
|
||||
this.timeout = timeout;
|
||||
cacheMap = new HashMap<K, CacheObj<K, V>>(capacity + 1, 1.0f);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- prune
|
||||
|
||||
/**
|
||||
* 清理过期对象。<br>
|
||||
* 清理后依旧满的情况下清除最少访问(访问计数最小)的对象并将其他对象的访问数减去这个最小访问数,以便新对象进入后可以公平计数。
|
||||
*
|
||||
* @return 清理个数
|
||||
*/
|
||||
@Override
|
||||
protected int pruneCache() {
|
||||
int count = 0;
|
||||
CacheObj<K, V> comin = null;
|
||||
|
||||
// 清理过期对象并找出访问最少的对象
|
||||
Iterator<CacheObj<K, V>> values = cacheMap.values().iterator();
|
||||
CacheObj<K, V> co;
|
||||
while (values.hasNext()) {
|
||||
co = values.next();
|
||||
if (co.isExpired() == true) {
|
||||
values.remove();
|
||||
onRemove(co.key, co.obj);
|
||||
count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
//找出访问最少的对象
|
||||
if (comin == null || co.accessCount < comin.accessCount) {
|
||||
comin = co;
|
||||
}
|
||||
}
|
||||
|
||||
// 减少所有对象访问量,并清除减少后为0的访问对象
|
||||
if (isFull() && comin != null) {
|
||||
long minAccessCount = comin.accessCount;
|
||||
|
||||
values = cacheMap.values().iterator();
|
||||
CacheObj<K, V> co1;
|
||||
while (values.hasNext()) {
|
||||
co1 = values.next();
|
||||
co1.accessCount -= minAccessCount;
|
||||
if (co1.accessCount <= 0) {
|
||||
values.remove();
|
||||
onRemove(co1.key, co1.obj);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
71
hutool-cache/src/main/java/cn/hutool/cache/impl/LRUCache.java
vendored
Normal file
71
hutool-cache/src/main/java/cn/hutool/cache/impl/LRUCache.java
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import cn.hutool.core.map.FixedLinkedHashMap;
|
||||
|
||||
/**
|
||||
* LRU (least recently used)最近最久未使用缓存<br>
|
||||
* 根据使用时间来判定对象是否被持续缓存<br>
|
||||
* 当对象被访问时放入缓存,当缓存满了,最久未被使用的对象将被移除。<br>
|
||||
* 此缓存基于LinkedHashMap,因此当被缓存的对象每被访问一次,这个对象的key就到链表头部。<br>
|
||||
* 这个算法简单并且非常快,他比FIFO有一个显著优势是经常使用的对象不太可能被移除缓存。<br>
|
||||
* 缺点是当缓存满时,不能被很快的访问。
|
||||
* @author Looly,jodd
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
*/
|
||||
public class LRUCache<K, V> extends AbstractCache<K, V> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 构造<br>
|
||||
* 默认无超时
|
||||
* @param capacity 容量
|
||||
*/
|
||||
public LRUCache(int capacity) {
|
||||
this(capacity, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param capacity 容量
|
||||
* @param timeout 默认超时时间,单位:毫秒
|
||||
*/
|
||||
public LRUCache(int capacity, long timeout) {
|
||||
if(Integer.MAX_VALUE == capacity) {
|
||||
capacity -= 1;
|
||||
}
|
||||
|
||||
this.capacity = capacity;
|
||||
this.timeout = timeout;
|
||||
|
||||
//链表key按照访问顺序排序,调用get方法后,会将这次访问的元素移至头部
|
||||
cacheMap = new FixedLinkedHashMap<K, CacheObj<K, V>>(capacity);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- prune
|
||||
|
||||
/**
|
||||
* 只清理超时对象,LRU的实现会交给<code>LinkedHashMap</code>
|
||||
*/
|
||||
@Override
|
||||
protected int pruneCache() {
|
||||
if (isPruneExpiredActive() == false) {
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
Iterator<CacheObj<K, V>> values = cacheMap.values().iterator();
|
||||
CacheObj<K, V> co;
|
||||
while (values.hasNext()) {
|
||||
co = values.next();
|
||||
if (co.isExpired()) {
|
||||
values.remove();
|
||||
onRemove(co.key, co.obj);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
102
hutool-cache/src/main/java/cn/hutool/cache/impl/NoCache.java
vendored
Normal file
102
hutool-cache/src/main/java/cn/hutool/cache/impl/NoCache.java
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import cn.hutool.cache.Cache;
|
||||
import cn.hutool.core.lang.func.Func0;
|
||||
|
||||
/**
|
||||
* 无缓存实现,用于快速关闭缓存
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @author Looly,jodd
|
||||
*/
|
||||
public class NoCache<K, V> implements Cache<K, V> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public int capacity() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long timeout() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(K key, V object) {
|
||||
// 跳过
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(K key, V object, long timeout) {
|
||||
// 跳过
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(K key) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(K key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(K key, boolean isUpdateLastAccess) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(K key, Func0<V> supplier) {
|
||||
try {
|
||||
return (null == supplier) ? null : supplier.call();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<V> iterator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<CacheObj<K, V>> cacheObjIterator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int prune() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFull() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(K key) {
|
||||
// 跳过
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
// 跳过
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
92
hutool-cache/src/main/java/cn/hutool/cache/impl/TimedCache.java
vendored
Normal file
92
hutool-cache/src/main/java/cn/hutool/cache/impl/TimedCache.java
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
import cn.hutool.cache.GlobalPruneTimer;
|
||||
|
||||
/**
|
||||
* 定时缓存<br>
|
||||
* 此缓存没有容量限制,对象只有在过期后才会被移除
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
*/
|
||||
public class TimedCache<K, V> extends AbstractCache<K, V> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 正在执行的定时任务 */
|
||||
private ScheduledFuture<?> pruneJobFuture;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param timeout 超时(过期)时长,单位毫秒
|
||||
*/
|
||||
public TimedCache(long timeout) {
|
||||
this(timeout, new HashMap<K, CacheObj<K, V>>());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param timeout 过期时长
|
||||
* @param map 存储缓存对象的map
|
||||
*/
|
||||
public TimedCache(long timeout, Map<K, CacheObj<K, V>> map) {
|
||||
this.capacity = 0;
|
||||
this.timeout = timeout;
|
||||
this.cacheMap = map;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- prune
|
||||
/**
|
||||
* 清理过期对象
|
||||
*
|
||||
* @return 清理数
|
||||
*/
|
||||
@Override
|
||||
protected int pruneCache() {
|
||||
int count = 0;
|
||||
Iterator<CacheObj<K, V>> values = cacheMap.values().iterator();
|
||||
CacheObj<K, V> co;
|
||||
while (values.hasNext()) {
|
||||
co = values.next();
|
||||
if (co.isExpired()) {
|
||||
values.remove();
|
||||
onRemove(co.key, co.obj);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- auto prune
|
||||
/**
|
||||
* 定时清理
|
||||
*
|
||||
* @param delay 间隔时长,单位毫秒
|
||||
*/
|
||||
public void schedulePrune(long delay) {
|
||||
this.pruneJobFuture = GlobalPruneTimer.INSTANCE.schedule(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
prune();
|
||||
}
|
||||
}, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消定时清理
|
||||
*/
|
||||
public void cancelPruneSchedule() {
|
||||
if (null != pruneJobFuture) {
|
||||
pruneJobFuture.cancel(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
24
hutool-cache/src/main/java/cn/hutool/cache/impl/WeakCache.java
vendored
Normal file
24
hutool-cache/src/main/java/cn/hutool/cache/impl/WeakCache.java
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
package cn.hutool.cache.impl;
|
||||
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
/**
|
||||
* 弱引用缓存<br>
|
||||
* 对于一个给定的键,其映射的存在并不阻止垃圾回收器对该键的丢弃,这就使该键成为可终止的,被终止,然后被回收。<br>
|
||||
* 丢弃某个键时,其条目从映射中有效地移除。<br>
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @param <K> 键
|
||||
* @param <V> 值
|
||||
* @author looly
|
||||
* @since 3.0.7
|
||||
*/
|
||||
public class WeakCache<K, V> extends TimedCache<K, V>{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public WeakCache(long timeout) {
|
||||
super(timeout, new WeakHashMap<K, CacheObj<K, V>>());
|
||||
}
|
||||
|
||||
}
|
7
hutool-cache/src/main/java/cn/hutool/cache/impl/package-info.java
vendored
Normal file
7
hutool-cache/src/main/java/cn/hutool/cache/impl/package-info.java
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* 提供各种缓存实现
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.cache.impl;
|
7
hutool-cache/src/main/java/cn/hutool/cache/package-info.java
vendored
Normal file
7
hutool-cache/src/main/java/cn/hutool/cache/package-info.java
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* 提供简易的缓存实现,此模块参考了jodd工具中的Cache模块
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.cache;
|
Reference in New Issue
Block a user