From 2151a5efbac31c1f6a1b2563ccb352170aa48c9c Mon Sep 17 00:00:00 2001 From: 935090232@qq.com <ak473600000> Date: Mon, 25 Oct 2021 14:14:16 +0800 Subject: [PATCH] 新增本地缓存 --- zq-erp/src/test/java/com/matrix/LocalCacheTest.java | 35 +++++++++++ zq-erp/src/main/java/com/matrix/system/common/init/LocalCache.java | 116 +++++++++++++++++++++++++++++--------- zq-erp/src/main/java/com/matrix/system/common/actions/DeveloperAction.java | 4 3 files changed, 126 insertions(+), 29 deletions(-) diff --git a/zq-erp/src/main/java/com/matrix/system/common/actions/DeveloperAction.java b/zq-erp/src/main/java/com/matrix/system/common/actions/DeveloperAction.java index caf92b3..aa6c8c5 100644 --- a/zq-erp/src/main/java/com/matrix/system/common/actions/DeveloperAction.java +++ b/zq-erp/src/main/java/com/matrix/system/common/actions/DeveloperAction.java @@ -3,7 +3,7 @@ import com.matrix.core.constance.MatrixConstance; import com.matrix.core.pojo.AjaxResult; import com.matrix.core.web.BaseAction; -import com.matrix.system.common.init.InitWebContainer; +import com.matrix.system.common.init.LocalCache; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -76,7 +76,7 @@ @RequestMapping("/getLocalCache") public @ResponseBody AjaxResult getLocalCache(String key) { - Object cache=InitWebContainer.getLocalCache(key); + Object cache= LocalCache.get(key); return AjaxResult.buildSuccessInstance(cache); } diff --git a/zq-erp/src/main/java/com/matrix/system/common/init/LocalCache.java b/zq-erp/src/main/java/com/matrix/system/common/init/LocalCache.java index 26b3098..00aa8b5 100644 --- a/zq-erp/src/main/java/com/matrix/system/common/init/LocalCache.java +++ b/zq-erp/src/main/java/com/matrix/system/common/init/LocalCache.java @@ -1,73 +1,135 @@ package com.matrix.system.common.init; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.matrix.core.tools.LogUtil; -import java.util.Date; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.*; public class LocalCache { + /* + * 清理线程运行状态 0 未启动,1 已启动 + */ + private static int CLEAR_THREAD_STATUS = 0; - private static ConcurrentMap<String,Value> localCache=new ConcurrentHashMap(60); + private static ConcurrentMap<String, Value> localCache = new ConcurrentHashMap(60); + /** * 获取本地缓存 + * * @param key * @param <T> * @return */ - public static <T> T get(String key){ - return (T)localCache.get(key); + public static <T> T get(String key) { + return (T) localCache.get(key); } /** * 保存一个本地缓存 + * * @param key * @param value */ - public static void save(String key,Object value){ - if(null!=localCache.put(key,value)){ - LogUtil.debug("覆盖原有缓存{}",key); + public static void save(String key, Object value) { + if (null != localCache.put(key, buildValue(value))) { + LogUtil.debug("覆盖原有缓存{}", key); } } - public static void save(String key,Object value,long timeOut){ - if(null!=localCache.put(key,value)){ - LogUtil.debug("覆盖原有缓存{}",key); + /** + * 设置含过期时间的缓存 + * + * @param key + * @param value + * @param timeOut 毫秒 + */ + public static void save(String key, Object value, long timeOut) { + if (null != localCache.put(key, buildValue(value, timeOut))) { + LogUtil.debug("覆盖原有缓存{}", key); } + startClearThread(); + } + + /** + * 清理过期对象 + */ + private synchronized static void startClearThread() { + if (CLEAR_THREAD_STATUS == 0) { + ThreadFactory namedThreadFactory = new ThreadFactoryBuilder() + .setNameFormat("demo-pool-%d").build(); + ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1, + 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<Runnable>(1), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy()); + singleThreadPool.execute(() -> { + CLEAR_THREAD_STATUS = 1; + while (true) { + try { + Set<Map.Entry<String, Value>> entries = localCache.entrySet(); + Iterator<Map.Entry<String, Value>> iterator = entries.iterator(); + while (iterator.hasNext()) { + Map.Entry<String, Value> next = iterator.next(); + + if (next.getValue().timeOut == 0) { + continue; + } + + boolean isTimeOut = (System.currentTimeMillis() - next.getValue().createTime) > next.getValue().timeOut; + if (isTimeOut) { + Value removed = localCache.remove(next.getKey()); + LogUtil.debug("清除过期对象:{}", removed.value); + } + } + Thread.sleep(1000); + } catch (InterruptedException e) { + LogUtil.error("清理缓存线程异常停止", e); + CLEAR_THREAD_STATUS = 0; + } + } + }); + + + } + } + + + private static Value buildValue(Object value) { + return buildValue(value, 0); + } + + + private static Value buildValue(Object value, long timeOut) { + Value instances = new Value(); + instances.createTime = System.currentTimeMillis(); + instances.value = value; + instances.timeOut = timeOut; + return instances; } /** * 缓存对象 */ - class Value{ + static class Value { /** - * 过期时间,0 表示不过期 + * 过期时间,0 表示不过期,单位毫秒 */ - private long timeOut=0; + private long timeOut = 0; /** * 缓存值 */ - private Object value; + private Object value; /** * 缓存创建时间 */ - private long createTime; + private long createTime; - - - - public static Value build(String value,long timeOut){ - Value instances=new Value(); - instances.createTime=System.currentTimeMillis(); - instances.value=value; - instances.timeOut=timeOut; - return instances; - } } diff --git a/zq-erp/src/test/java/com/matrix/LocalCacheTest.java b/zq-erp/src/test/java/com/matrix/LocalCacheTest.java new file mode 100644 index 0000000..df54f35 --- /dev/null +++ b/zq-erp/src/test/java/com/matrix/LocalCacheTest.java @@ -0,0 +1,35 @@ +/** + * projectName: zq-erp + * fileName: LocalCacheTest.java + * packageName: com.matrix + * date: 2021-10-25 14:03 + * copyright(c) 2021 http://www.hydee.cn/ Inc. All rights reserved. + */ +package com.matrix; + +import com.matrix.system.common.init.LocalCache; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + +/** + * @version: V1.0 + * @author: JiangYouYao + * @className: LocalCacheTest + * @packageName: com.matrix + * @description: LocalCache + * @data: 2021-10-25 14:03 + **/ +public class LocalCacheTest { + + @Test + public void testNoticeTask() throws IOException, InterruptedException { + LocalCache.save("name","jyy",1000*3); + System.out.println("放入对象"); + Assert.assertNotNull(LocalCache.get("name")); + Thread.sleep(1000*5); + Assert.assertNull(LocalCache.get("name")); + } + +} \ No newline at end of file -- Gitblit v1.9.1