From b017599eaeccf8f213493672f80b8106be8f1035 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 2 Oct 2020 10:11:56 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20Core=20=E5=A2=9E=E5=8A=A0=20CacheSto?= =?UTF-8?q?re=20=E5=AD=98=E5=82=A8=E6=BA=90=E8=BF=87=E6=BB=A4;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] FactoryInfo 增加 source 属性; [Add] CacheStoreBuilder 增加对存储源的指定; [Add] CacheStoreBuilderTest 调整测试用例以对存储源过滤进行测试(但该测试依然是不完善的, 有待改进); --- .../cgj/bot/cache/CacheStoreBuilder.java | 69 +++++++++++++++++-- .../net/lamgc/cgj/bot/cache/FactoryInfo.java | 10 +++ .../cgj/bot/cache/CacheStoreBuilderTest.java | 6 +- 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreBuilder.java b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreBuilder.java index bace79e..5d431f1 100644 --- a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreBuilder.java +++ b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreBuilder.java @@ -37,13 +37,13 @@ import java.util.function.Function; public class CacheStoreBuilder { private final static Logger log = LoggerFactory.getLogger(CacheStoreBuilder.class); - private final static List FACTORY_LIST = new LinkedList<>(); + private final static List FACTORY_LIST = new ArrayList<>(); private final static Map FACTORY_INFO_MAP = new Hashtable<>(); /** * 使用 SPI 机制加载所有缓存组件. * - *

第一次执行时加载, 由 {@link #getFactory(Function)} 调用. + *

第一次执行时加载, 由 {@link #getFactory(CacheStoreSource, Function)} 调用. *

由于 ServiceLoader 线程不安全, 所以通过 synchronized 保证其安全性. * 不通过 static 块进行初始化的原因是因为担心发生异常导致无法继续执行 * (除非必要, 否则不要使用 static 执行可能会发生异常的代码.). @@ -97,7 +97,8 @@ public class CacheStoreBuilder { * 获取一个当前可用的高优先级 Factory 对象. * @return 返回可用的高优先级 Factory 对象. */ - private static > R getFactory(Function function) throws NoSuchFactoryException { + private static > R getFactory(CacheStoreSource storeSource, Function function) + throws NoSuchFactoryException { if (FACTORY_LIST.size() == 0) { loadFactory(); } @@ -105,6 +106,9 @@ public class CacheStoreBuilder { while (iterator.hasNext()) { CacheStoreFactory factory = iterator.next(); FactoryInfo info = FACTORY_INFO_MAP.get(factory); + if (storeSource != null && info.getStoreSource() != storeSource) { + continue; + } try { if (factory.canGetCacheStore()) { log.debug("CacheStoreFactory {} 可用(优先级: {}).", info.getFactoryName(), info.getFactoryPriority()); @@ -149,8 +153,21 @@ public class CacheStoreBuilder { * @throws GetCacheStoreException 当无法获取可用的 CacheStore 时抛出. */ public static SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) { + return newSingleCacheStore(null, identify, converter); + } + + /** + * 获取单项缓存存储容器. + * @param storeSource 存储类型. + * @param identify 缓存容器标识. + * @param converter 类型转换器. + * @param 值类型. + * @return 返回新的存储容器, 与其他容器互不干扰. + * @throws GetCacheStoreException 当无法获取可用的 CacheStore 时抛出. + */ + public static SingleCacheStore newSingleCacheStore(CacheStoreSource storeSource, String identify, StringConverter converter) { try { - return getFactory(factory -> { + return getFactory(storeSource, factory -> { SingleCacheStore singleCacheStoreInstance = factory.newSingleCacheStore(identify, converter); if (singleCacheStoreInstance == null) { throw new GetCacheStoreException("Factory " + factory.getClass().getName() + " 返回 null."); @@ -171,8 +188,21 @@ public class CacheStoreBuilder { * @throws GetCacheStoreException 当无法获取可用的 CacheStore 时抛出. */ public static ListCacheStore newListCacheStore(String identify, StringConverter converter) { + return newListCacheStore(null, identify, converter); + } + + /** + * 获取列表缓存存储容器. + * @param storeSource 存储类型. + * @param identify 缓存容器标识. + * @param converter 类型转换器. + * @param 元素类型. + * @return 返回新的存储容器, 与其他容器互不干扰. + * @throws GetCacheStoreException 当无法获取可用的 CacheStore 时抛出. + */ + public static ListCacheStore newListCacheStore(CacheStoreSource storeSource, String identify, StringConverter converter) { try { - return getFactory(factory -> { + return getFactory(storeSource, factory -> { ListCacheStore listCacheStoreInstance = factory.newListCacheStore(identify, converter); if (listCacheStoreInstance == null) { throw new GetCacheStoreException("Factory " + factory.getClass().getName() + " 返回 null."); @@ -193,8 +223,20 @@ public class CacheStoreBuilder { * @throws GetCacheStoreException 当无法获取可用的 CacheStore 时抛出. */ public static SetCacheStore newSetCacheStore(String identify, StringConverter converter) { + return newSetCacheStore(null, identify, converter); + } + + /** + * 获取集合缓存存储容器. + * @param identify 缓存容器标识. + * @param converter 类型转换器. + * @param 元素类型. + * @return 返回新的存储容器, 与其他容器互不干扰. + * @throws GetCacheStoreException 当无法获取可用的 CacheStore 时抛出. + */ + public static SetCacheStore newSetCacheStore(CacheStoreSource storeSource, String identify, StringConverter converter) { try { - return getFactory(factory -> { + return getFactory(storeSource, factory -> { SetCacheStore setCacheStoreInstance = factory.newSetCacheStore(identify, converter); if (setCacheStoreInstance == null) { throw new GetCacheStoreException("Factory " + factory.getClass().getName() + " 返回 null."); @@ -215,8 +257,21 @@ public class CacheStoreBuilder { * @throws GetCacheStoreException 当无法获取可用的 CacheStore 时抛出. */ public static MapCacheStore newMapCacheStore(String identify, StringConverter converter) { + return newMapCacheStore(null, identify, converter); + } + + /** + * 获取映射表缓存存储容器. + * @param storeSource 存储类型. + * @param identify 缓存容器标识. + * @param converter 类型转换器. + * @param 值类型. + * @return 返回新的存储容器, 与其他容器互不干扰. + * @throws GetCacheStoreException 当无法获取可用的 CacheStore 时抛出. + */ + public static MapCacheStore newMapCacheStore(CacheStoreSource storeSource, String identify, StringConverter converter) { try { - return getFactory(factory -> { + return getFactory(storeSource, factory -> { MapCacheStore mapCacheStoreInstance = factory.newMapCacheStore(identify, converter); if (mapCacheStoreInstance == null) { throw new GetCacheStoreException("Factory " + factory.getClass().getName() + " 返回 null."); diff --git a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java index e0eb95c..a1df59d 100644 --- a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java +++ b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java @@ -29,6 +29,7 @@ public class FactoryInfo { private final String factoryName; private final int factoryPriority; + private final CacheStoreSource storeSource; public FactoryInfo(Class factoryClass) { Factory factoryAnnotation = factoryClass.getAnnotation(Factory.class); @@ -39,6 +40,7 @@ public class FactoryInfo { } this.factoryName = factoryAnnotation.name(); + this.storeSource = factoryAnnotation.source(); int factoryPriority = factoryAnnotation.priority(); if (factoryPriority > FactoryPriority.PRIORITY_HIGHEST) { this.factoryPriority = FactoryPriority.PRIORITY_HIGHEST; @@ -65,6 +67,14 @@ public class FactoryInfo { return factoryPriority; } + /** + * 获取存储容器实现的存储源类型. + * @return 返回 Factory 所属实现组件的存储源类型. + */ + public CacheStoreSource getStoreSource() { + return storeSource; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/ContentGrabbingJi-core/src/test/java/net/lamgc/cgj/bot/cache/CacheStoreBuilderTest.java b/ContentGrabbingJi-core/src/test/java/net/lamgc/cgj/bot/cache/CacheStoreBuilderTest.java index f515e52..4106cdd 100644 --- a/ContentGrabbingJi-core/src/test/java/net/lamgc/cgj/bot/cache/CacheStoreBuilderTest.java +++ b/ContentGrabbingJi-core/src/test/java/net/lamgc/cgj/bot/cache/CacheStoreBuilderTest.java @@ -32,9 +32,9 @@ public class CacheStoreBuilderTest { final String identify = "test"; final StringConverter converter = new StringToStringConverter(); - Assert.assertNotNull(CacheStoreBuilder.newSingleCacheStore(identify, converter)); - Assert.assertNotNull(CacheStoreBuilder.newListCacheStore(identify, converter)); - Assert.assertNotNull(CacheStoreBuilder.newMapCacheStore(identify, converter)); + Assert.assertNotNull(CacheStoreBuilder.newSingleCacheStore(CacheStoreSource.REMOTE, identify, converter)); + Assert.assertNotNull(CacheStoreBuilder.newListCacheStore(CacheStoreSource.MEMORY, identify, converter)); + Assert.assertNotNull(CacheStoreBuilder.newMapCacheStore(CacheStoreSource.REMOTE, identify, converter)); Assert.assertNotNull(CacheStoreBuilder.newSetCacheStore(identify, converter)); }