[Add][Change] Common 更改 CacheStoreBuilder 为实例模式, 为 CacheStoreFactory 添加 initial 方法;

[Add] CacheStoreFactory 添加 'initial(File)' 方法供缓存组件进行初始化;
[Change] CacheStoreBuilder 将 CacheStoreBuilder 从静态类更改为实例类, 增加对 CacheStoreFactory 的初始化;
[Change] CacheStoreBuilderTest 适配 CacheStoreBuilder 的更改;
This commit is contained in:
2020-11-05 00:53:32 +08:00
parent 235c7452b8
commit b7d712da21
3 changed files with 97 additions and 19 deletions

View File

@ -17,6 +17,7 @@
package net.lamgc.cgj.bot.cache;
import com.google.common.base.Throwables;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.convert.StringToStringConverter;
import net.lamgc.cgj.bot.cache.local.CopyOnWriteArrayListCacheStore;
@ -24,31 +25,54 @@ import net.lamgc.cgj.bot.cache.local.HashSetCacheStore;
import net.lamgc.cgj.bot.cache.redis.RedisMapCacheStore;
import net.lamgc.cgj.bot.cache.redis.RedisSingleCacheStore;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
/**
* @see CacheStoreBuilder
*/
public class CacheStoreBuilderTest {
private final static TemporaryFolder tempDirectory = TemporaryFolder.builder().build();
@BeforeClass
public static void beforeAction() {
try {
tempDirectory.create();
} catch (IOException e) {
Assert.fail(Throwables.getStackTraceAsString(e));
}
}
@Test
public void getCacheStoreTest() {
final String identify = "test";
final StringConverter<String> converter = new StringToStringConverter();
CacheStoreBuilder cacheStoreBuilder;
try {
cacheStoreBuilder = CacheStoreBuilder.getInstance(tempDirectory.getRoot());
} catch (IOException e) {
Assert.fail(Throwables.getStackTraceAsString(e));
return;
}
SingleCacheStore<String> singleCacheStore = CacheStoreBuilder.newSingleCacheStore(CacheStoreSource.REMOTE, identify, converter);
SingleCacheStore<String> singleCacheStore = cacheStoreBuilder.newSingleCacheStore(CacheStoreSource.REMOTE, identify, converter);
Assert.assertNotNull(singleCacheStore);
Assert.assertEquals(RedisSingleCacheStore.class, singleCacheStore.getClass());
ListCacheStore<String> listCacheStore = CacheStoreBuilder.newListCacheStore(CacheStoreSource.MEMORY, identify, converter);
ListCacheStore<String> listCacheStore = cacheStoreBuilder.newListCacheStore(CacheStoreSource.MEMORY, identify, converter);
Assert.assertNotNull(listCacheStore);
Assert.assertEquals(CopyOnWriteArrayListCacheStore.class, listCacheStore.getClass());
MapCacheStore<String> mapCacheStore = CacheStoreBuilder.newMapCacheStore(CacheStoreSource.REMOTE, identify, converter);
MapCacheStore<String> mapCacheStore = cacheStoreBuilder.newMapCacheStore(CacheStoreSource.REMOTE, identify, converter);
Assert.assertNotNull(mapCacheStore);
Assert.assertEquals(RedisMapCacheStore.class, mapCacheStore.getClass());
SetCacheStore<String> setCacheStore = CacheStoreBuilder.newSetCacheStore(identify, converter);
SetCacheStore<String> setCacheStore = cacheStoreBuilder.newSetCacheStore(identify, converter);
Assert.assertNotNull(setCacheStore);
Assert.assertEquals(HashSetCacheStore.class, setCacheStore.getClass());
}