[Add] CacheStore-local 添加 SetCacheStore 的实现, 添加 CacheStoreFactory 的实现;

[Add] HashSetCacheStore 添加基于 HashSet 的 SetCacheStore 实现;
[Add] LocalCacheStoreFactory 增加本地缓存库的 CacheStoreFactory;
This commit is contained in:
LamGC 2020-09-04 20:02:36 +08:00
parent 688b99d198
commit 49a6257148
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.local;
import net.lamgc.cgj.bot.cache.SetCacheStore;
import java.util.*;
/**
*
* @param <E> 元素类型.
* @author LamGC
*/
public class HashSetCacheStore<E> extends LocalCollectionCacheStore<E, Set<E>> implements SetCacheStore<E> {
@Override
protected Set<E> getCacheItemCollection(String key, boolean create) {
Map<String, CacheItem<Set<E>>> cacheMap = getCacheMap();
if (!cacheMap.containsKey(key)) {
if (create) {
cacheMap.put(key, new CacheItem<>(new HashSet<>()));
} else {
return null;
}
}
return cacheMap.get(key).getValue();
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.cache.local;
import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter;
/**
* 本地缓存存储容器工厂.
* 最快速但又是最占内存的方法, 适用于远端缓存失效, 或无远端缓存的情况下使用.
* 最简单的缓存实现, 无持久化功能.
* @author LamGC
*/
public class LocalCacheStoreFactory implements CacheStoreFactory {
@Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) {
return new HashSingleCacheStore<>();
}
@Override
public <V> ListCacheStore<V> newListCacheStore(String identify, StringConverter<V> converter) {
return new CopyOnWriteArrayListCacheStore<>();
}
@Override
public <V> SetCacheStore<V> newSetCacheStore(String identify, StringConverter<V> converter) {
return new HashSetCacheStore<>();
}
@Override
public <V> MapCacheStore<V> newMapCacheStore(String identify, StringConverter<V> converter) {
return new HashMapCacheStore<>();
}
}