封装hutool缓存
package com.ruoyi.common.utils;import cn.hutool.cache.CacheUtil;import cn.hutool.cache.impl.TimedCache;import cn.hutool.core.date.DateUnit;import com.ruoyi.project.cms.domain.CmsArticles;import lombok
·
package com.ruoyi.common.utils.cache;
import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.LFUCache;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit;
import com.ruoyi.project.cms.domain.CmsArticles;
import lombok.extern.slf4j.Slf4j;
/**
* Cache工具类
*
*
* @author ruoyi
*/
@Slf4j
public class HutoolCacheUtils {
/**
* 文章缓存,全局共享,直接使用
*/
public static LFUCache<Long, CmsArticles> articlesCache = CacheUtil.newLFUCache(10);
/**
* 默认缓存时长 单位s
*/
private static final Long DEFAULT_TIMEOUT = DateUnit.SECOND.getMillis() * 10;
/**
* 默认清理间隔时间 单位s
*/
private static final Long CLEAN_TIMEOUT = DateUnit.SECOND.getMillis() * 1;
/**
* 全局时间超时缓存,构造器启动每秒检测。
*/
public static TimedCache<String, Object> timedCache = CacheUtil.newTimedCache(DEFAULT_TIMEOUT);
static {
//启动定时任务
timedCache.schedulePrune(CLEAN_TIMEOUT);
}
public static void put(String key,Object value) {
timedCache.put(key, value);
}
/**
*
* @param key
* @param value
* @param expire 过期时间 秒
*/
public static void put(String key,Object value,Integer expire) {
timedCache.put(key, value, DateUnit.SECOND.getMillis() * expire);
}
/**
* 禁止延迟缓存 isUpdateLastAccess = false
* @param key
* @param isUpdateLastAccess
*/
public static Object get(String key,boolean isUpdateLastAccess){
return timedCache.get(key,isUpdateLastAccess);
}
public static Object get(String key){
return get(key,false);
}
public static void remove(String key){
timedCache.remove(key);
}
/**
* 清空所有缓存
*/
public static void clear(){
articlesCache.clear();
timedCache.clear();
}
}
更多推荐
所有评论(0)