From 10c92d881286ae78bb303b4f924a32565cce2a9c Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 12 Sep 2020 02:59:40 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20CacheStore-local=20=E9=80=82?= =?UTF-8?q?=E9=85=8D=E6=9B=B4=E6=94=B9;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Update] LocalCacheStoreFactory 适配 CacheStore-api 对 CacheStoreFactory 的更改; --- .../cgj/bot/cache/local/LocalCacheStoreFactory.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCacheStoreFactory.java b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCacheStoreFactory.java index 028e5a7..da81605 100644 --- a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCacheStoreFactory.java +++ b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCacheStoreFactory.java @@ -26,6 +26,7 @@ import net.lamgc.cgj.bot.cache.convert.StringConverter; * 最简单的缓存实现, 无持久化功能. * @author LamGC */ +@Factory(name = "Local", priority = FactoryPriority.PRIORITY_LOWEST) public class LocalCacheStoreFactory implements CacheStoreFactory { @Override @@ -48,4 +49,15 @@ public class LocalCacheStoreFactory implements CacheStoreFactory { return new HashMapCacheStore<>(); } + /** + * 内存使用阀值. + *

当内存使用到了指定百分比时, 将禁止创建 CacheStore. + */ + private final static double MEMORY_USAGE_THRESHOLD = 85; + @Override + public boolean canGetCacheStore() { + Runtime runtime = Runtime.getRuntime(); + double memoryUsedPercentage = (double) runtime.totalMemory() / runtime.maxMemory(); + return memoryUsedPercentage < MEMORY_USAGE_THRESHOLD; + } }