import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.http.HttpUtil;
/**
*
*/
public class IdCacheUtils {
private static TimedCache<String, String> cache = null;
//默认缓存时长 单位s
private static final Long DEFAULT_TIMEOUT = 5*60 * 60 * 60 * 1000L;// 60 * 1000L=1秒
/**
* 缓存
*/
public static String getVal(String id){
//初始化缓存
if(null==cache){
cache = CacheUtil.newTimedCache(DEFAULT_TIMEOUT);
//启动定时任务,每100秒秒检查一次过期
cache.schedulePrune(100000);
}
String res = cache.get(id);
if(StrUtil.isNotBlank(res )){
return res ;
}
String id = "123123";
String val = "缓存值XXXXXX";//接口请求获得
cache.put(id,val );
return val;
}
}
springboot hutool内存缓存
于 2024-01-31 08:07:00 首次发布