mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Update] CacheStore-local 适配因加入 CacheKey 带来的变更;
[Update] * 适配更改;
This commit is contained in:
parent
a24b810183
commit
3c705ee16a
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package net.lamgc.cgj.bot.cache.local;
|
package net.lamgc.cgj.bot.cache.local;
|
||||||
|
|
||||||
|
import net.lamgc.cgj.bot.cache.CacheKey;
|
||||||
import net.lamgc.cgj.bot.cache.ListCacheStore;
|
import net.lamgc.cgj.bot.cache.ListCacheStore;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -30,7 +31,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|||||||
public class CopyOnWriteArrayListCacheStore<E> extends LocalCollectionCacheStore<E, List<E>> implements ListCacheStore<E> {
|
public class CopyOnWriteArrayListCacheStore<E> extends LocalCollectionCacheStore<E, List<E>> implements ListCacheStore<E> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E getElement(String key, int index) {
|
public E getElement(CacheKey key, int index) {
|
||||||
List<E> itemCollection = getCacheItemCollection(key, false);
|
List<E> itemCollection = getCacheItemCollection(key, false);
|
||||||
try {
|
try {
|
||||||
return itemCollection == null ? null : itemCollection.get(index);
|
return itemCollection == null ? null : itemCollection.get(index);
|
||||||
@ -40,7 +41,7 @@ public class CopyOnWriteArrayListCacheStore<E> extends LocalCollectionCacheStore
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<E> getElementsByRange(String key, int index, int length) {
|
public List<E> getElementsByRange(CacheKey key, int index, int length) {
|
||||||
int listLength = elementsLength(key);
|
int listLength = elementsLength(key);
|
||||||
if (listLength == -1) {
|
if (listLength == -1) {
|
||||||
return null;
|
return null;
|
||||||
@ -62,7 +63,7 @@ public class CopyOnWriteArrayListCacheStore<E> extends LocalCollectionCacheStore
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean removeElement(String key, int index) {
|
public boolean removeElement(CacheKey key, int index) {
|
||||||
List<E> itemCollection = getCacheItemCollection(key, false);
|
List<E> itemCollection = getCacheItemCollection(key, false);
|
||||||
if (itemCollection != null) {
|
if (itemCollection != null) {
|
||||||
try {
|
try {
|
||||||
@ -76,16 +77,16 @@ public class CopyOnWriteArrayListCacheStore<E> extends LocalCollectionCacheStore
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<E> getCacheItemCollection(String key, boolean create) {
|
protected List<E> getCacheItemCollection(CacheKey key, boolean create) {
|
||||||
Objects.requireNonNull(key);
|
Objects.requireNonNull(key);
|
||||||
Map<String, CacheItem<List<E>>> cacheMap = getCacheMap();
|
Map<String, CacheItem<List<E>>> cacheMap = getCacheMap();
|
||||||
if (!cacheMap.containsKey(key)) {
|
if (!cacheMap.containsKey(key.toString())) {
|
||||||
if (create) {
|
if (create) {
|
||||||
cacheMap.put(key, new CacheItem<>(new CopyOnWriteArrayList<>()));
|
cacheMap.put(key.toString(), new CacheItem<>(new CopyOnWriteArrayList<>()));
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cacheMap.get(key).getValue();
|
return cacheMap.get(key.toString()).getValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package net.lamgc.cgj.bot.cache.local;
|
package net.lamgc.cgj.bot.cache.local;
|
||||||
|
|
||||||
|
import net.lamgc.cgj.bot.cache.CacheKey;
|
||||||
import net.lamgc.cgj.bot.cache.CacheStore;
|
import net.lamgc.cgj.bot.cache.CacheStore;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -43,21 +44,21 @@ public abstract class HashCacheStore<V> implements CacheStore<V>, Cleanable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setTimeToLive(String key, long ttl) {
|
public boolean setTimeToLive(CacheKey key, long ttl) {
|
||||||
if (!exists(key)) {
|
if (!exists(key)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
CacheItem<V> item = cacheMap.get(key);
|
CacheItem<V> item = cacheMap.get(key.toString());
|
||||||
item.setExpireDate(ttl < 0 ? null : new Date(System.currentTimeMillis() + ttl));
|
item.setExpireDate(ttl < 0 ? null : new Date(System.currentTimeMillis() + ttl));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getTimeToLive(String key) {
|
public long getTimeToLive(CacheKey key) {
|
||||||
if (!exists(key)) {
|
if (!exists(key)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
CacheItem<V> item = cacheMap.get(key);
|
CacheItem<V> item = cacheMap.get(key.toString());
|
||||||
Date expireDate = item.getExpireDate();
|
Date expireDate = item.getExpireDate();
|
||||||
if (expireDate != null) {
|
if (expireDate != null) {
|
||||||
return expireDate.getTime() - System.currentTimeMillis();
|
return expireDate.getTime() - System.currentTimeMillis();
|
||||||
@ -77,11 +78,11 @@ public abstract class HashCacheStore<V> implements CacheStore<V>, Cleanable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean exists(String key) {
|
public boolean exists(CacheKey key) {
|
||||||
if (!cacheMap.containsKey(key)) {
|
if (!cacheMap.containsKey(key.toString())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
CacheItem<V> item = cacheMap.get(key);
|
CacheItem<V> item = cacheMap.get(key.toString());
|
||||||
// 在检查其过期情况后根据情况进行清理, 减轻主动清理机制的负担.
|
// 在检查其过期情况后根据情况进行清理, 减轻主动清理机制的负担.
|
||||||
if (item.isExpire(new Date())) {
|
if (item.isExpire(new Date())) {
|
||||||
remove(key);
|
remove(key);
|
||||||
@ -91,9 +92,9 @@ public abstract class HashCacheStore<V> implements CacheStore<V>, Cleanable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean remove(String key) {
|
public boolean remove(CacheKey key) {
|
||||||
// 根据 Collection 说明, 删除时 key 存在映射就会返回, 只要返回 null 就代表没有.
|
// 根据 Collection 说明, 删除时 key 存在映射就会返回, 只要返回 null 就代表没有.
|
||||||
return cacheMap.remove(key) != null;
|
return cacheMap.remove(key.toString()) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package net.lamgc.cgj.bot.cache.local;
|
package net.lamgc.cgj.bot.cache.local;
|
||||||
|
|
||||||
|
import net.lamgc.cgj.bot.cache.CacheKey;
|
||||||
import net.lamgc.cgj.bot.cache.MapCacheStore;
|
import net.lamgc.cgj.bot.cache.MapCacheStore;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -32,22 +33,22 @@ import java.util.function.Function;
|
|||||||
public class HashMapCacheStore<V> extends HashCacheStore<Map<String, V>> implements MapCacheStore<V> {
|
public class HashMapCacheStore<V> extends HashCacheStore<Map<String, V>> implements MapCacheStore<V> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int mapSize(String key) {
|
public int mapSize(CacheKey key) {
|
||||||
return getMap(key, false, Map::size, -1);
|
return getMap(key, false, Map::size, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> mapFieldSet(String key) {
|
public Set<String> mapFieldSet(CacheKey key) {
|
||||||
return getMap(key, false, map -> Collections.unmodifiableSet(map.keySet()), null);
|
return getMap(key, false, map -> Collections.unmodifiableSet(map.keySet()), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<V> mapValueSet(String key) {
|
public Set<V> mapValueSet(CacheKey key) {
|
||||||
return getMap(key, false, map -> new HashSet<>(map.values()), null);
|
return getMap(key, false, map -> new HashSet<>(map.values()), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean put(String key, String field, V value) {
|
public boolean put(CacheKey key, String field, V value) {
|
||||||
return getMap(key, true, map -> {
|
return getMap(key, true, map -> {
|
||||||
map.put(Objects.requireNonNull(field), Objects.requireNonNull(value));
|
map.put(Objects.requireNonNull(field), Objects.requireNonNull(value));
|
||||||
return true;
|
return true;
|
||||||
@ -55,7 +56,7 @@ public class HashMapCacheStore<V> extends HashCacheStore<Map<String, V>> impleme
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean putAll(String key, Map<String, V> map) {
|
public boolean putAll(CacheKey key, Map<String, V> map) {
|
||||||
return getMap(key, true, keyMap -> {
|
return getMap(key, true, keyMap -> {
|
||||||
keyMap.putAll(Objects.requireNonNull(map));
|
keyMap.putAll(Objects.requireNonNull(map));
|
||||||
return true;
|
return true;
|
||||||
@ -63,7 +64,7 @@ public class HashMapCacheStore<V> extends HashCacheStore<Map<String, V>> impleme
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean putIfNotExist(String key, String field, V value) {
|
public boolean putIfNotExist(CacheKey key, String field, V value) {
|
||||||
return getMap(key, true, map -> {
|
return getMap(key, true, map -> {
|
||||||
if (map.containsKey(Objects.requireNonNull(field))) {
|
if (map.containsKey(Objects.requireNonNull(field))) {
|
||||||
return false;
|
return false;
|
||||||
@ -74,44 +75,45 @@ public class HashMapCacheStore<V> extends HashCacheStore<Map<String, V>> impleme
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V get(String key, String field) {
|
public V get(CacheKey key, String field) {
|
||||||
return getMap(key, false, map -> map.get(Objects.requireNonNull(field)), null);
|
return getMap(key, false, map -> map.get(Objects.requireNonNull(field)), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean removeField(String key, String field) {
|
public boolean removeField(CacheKey key, String field) {
|
||||||
return getMap(key, false, map -> map.remove(Objects.requireNonNull(field)) != null, false);
|
return getMap(key, false, map -> map.remove(Objects.requireNonNull(field)) != null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsField(String key, String field) {
|
public boolean containsField(CacheKey key, String field) {
|
||||||
return getMap(key, false, map -> map.containsKey(Objects.requireNonNull(field)), false);
|
return getMap(key, false, map -> map.containsKey(Objects.requireNonNull(field)), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean mapIsEmpty(String key) {
|
public boolean mapIsEmpty(CacheKey key) {
|
||||||
return getMap(key, false, Map::isEmpty, false);
|
return getMap(key, false, Map::isEmpty, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean clearMap(String key) {
|
public boolean clearMap(CacheKey key) {
|
||||||
return getMap(key, false, map -> {
|
return getMap(key, false, map -> {
|
||||||
map.clear();
|
map.clear();
|
||||||
return true;
|
return true;
|
||||||
}, false);
|
}, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <R> R getMap(String key, boolean create, Function<Map<String, V>, R> notNull, R isNull) {
|
private <R> R getMap(CacheKey key, boolean create, Function<Map<String, V>, R> notNull, R isNull) {
|
||||||
Objects.requireNonNull(key);
|
Objects.requireNonNull(key);
|
||||||
|
String keyString = key.toString();
|
||||||
Map<String, CacheItem<Map<String, V>>> cacheMap = getCacheMap();
|
Map<String, CacheItem<Map<String, V>>> cacheMap = getCacheMap();
|
||||||
if (!cacheMap.containsKey(key)) {
|
if (!cacheMap.containsKey(keyString)) {
|
||||||
if (create) {
|
if (create) {
|
||||||
cacheMap.put(key, new CacheItem<>(new Hashtable<>()));
|
cacheMap.put(keyString, new CacheItem<>(new Hashtable<>()));
|
||||||
} else {
|
} else {
|
||||||
return isNull;
|
return isNull;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return notNull.apply(cacheMap.get(key).getValue());
|
return notNull.apply(cacheMap.get(keyString).getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package net.lamgc.cgj.bot.cache.local;
|
package net.lamgc.cgj.bot.cache.local;
|
||||||
|
|
||||||
|
import net.lamgc.cgj.bot.cache.CacheKey;
|
||||||
import net.lamgc.cgj.bot.cache.SetCacheStore;
|
import net.lamgc.cgj.bot.cache.SetCacheStore;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -29,16 +30,18 @@ import java.util.*;
|
|||||||
public class HashSetCacheStore<E> extends LocalCollectionCacheStore<E, Set<E>> implements SetCacheStore<E> {
|
public class HashSetCacheStore<E> extends LocalCollectionCacheStore<E, Set<E>> implements SetCacheStore<E> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Set<E> getCacheItemCollection(String key, boolean create) {
|
protected Set<E> getCacheItemCollection(CacheKey key, boolean create) {
|
||||||
|
Objects.requireNonNull(key);
|
||||||
|
String keyString = key.toString();
|
||||||
Map<String, CacheItem<Set<E>>> cacheMap = getCacheMap();
|
Map<String, CacheItem<Set<E>>> cacheMap = getCacheMap();
|
||||||
if (!cacheMap.containsKey(key)) {
|
if (!cacheMap.containsKey(keyString)) {
|
||||||
if (create) {
|
if (create) {
|
||||||
cacheMap.put(key, new CacheItem<>(new HashSet<>()));
|
cacheMap.put(keyString, new CacheItem<>(new HashSet<>()));
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cacheMap.get(key).getValue();
|
return cacheMap.get(keyString).getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package net.lamgc.cgj.bot.cache.local;
|
package net.lamgc.cgj.bot.cache.local;
|
||||||
|
|
||||||
|
import net.lamgc.cgj.bot.cache.CacheKey;
|
||||||
import net.lamgc.cgj.bot.cache.SingleCacheStore;
|
import net.lamgc.cgj.bot.cache.SingleCacheStore;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -29,13 +30,13 @@ import java.util.Objects;
|
|||||||
public class HashSingleCacheStore<V> extends HashCacheStore<V> implements SingleCacheStore<V> {
|
public class HashSingleCacheStore<V> extends HashCacheStore<V> implements SingleCacheStore<V> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean set(String key, V value) {
|
public boolean set(CacheKey key, V value) {
|
||||||
getCacheMap().put(Objects.requireNonNull(key), new CacheItem<>(Objects.requireNonNull(value)));
|
getCacheMap().put(Objects.requireNonNull(key).toString(), new CacheItem<>(Objects.requireNonNull(value)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setIfNotExist(String key, V value) {
|
public boolean setIfNotExist(CacheKey key, V value) {
|
||||||
if (exists(key)) {
|
if (exists(key)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -43,11 +44,11 @@ public class HashSingleCacheStore<V> extends HashCacheStore<V> implements Single
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V get(String key) {
|
public V get(CacheKey key) {
|
||||||
if (!exists(key)) {
|
if (!exists(key)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return getCacheMap().get(key).getValue();
|
return getCacheMap().get(key.toString()).getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package net.lamgc.cgj.bot.cache.local;
|
package net.lamgc.cgj.bot.cache.local;
|
||||||
|
|
||||||
|
import net.lamgc.cgj.bot.cache.CacheKey;
|
||||||
import net.lamgc.cgj.bot.cache.CollectionCacheStore;
|
import net.lamgc.cgj.bot.cache.CollectionCacheStore;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -39,10 +40,10 @@ implements CollectionCacheStore<E, C> {
|
|||||||
* @param create 如果不存在, 是否创建.
|
* @param create 如果不存在, 是否创建.
|
||||||
* @return 如果不存在且 create 为 false, 或添加失败, 返回 false, 添加成功返回 true.
|
* @return 如果不存在且 create 为 false, 或添加失败, 返回 false, 添加成功返回 true.
|
||||||
*/
|
*/
|
||||||
protected abstract C getCacheItemCollection(String key, boolean create);
|
protected abstract C getCacheItemCollection(CacheKey key, boolean create);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addElement(String key, E element) {
|
public boolean addElement(CacheKey key, E element) {
|
||||||
Objects.requireNonNull(key);
|
Objects.requireNonNull(key);
|
||||||
Objects.requireNonNull(element);
|
Objects.requireNonNull(element);
|
||||||
Collection<E> itemCollection = getCacheItemCollection(key, true);
|
Collection<E> itemCollection = getCacheItemCollection(key, true);
|
||||||
@ -50,7 +51,7 @@ implements CollectionCacheStore<E, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addElements(String key, Collection<E> elements) {
|
public boolean addElements(CacheKey key, Collection<E> elements) {
|
||||||
Objects.requireNonNull(key);
|
Objects.requireNonNull(key);
|
||||||
Objects.requireNonNull(elements);
|
Objects.requireNonNull(elements);
|
||||||
Collection<E> itemCollection = getCacheItemCollection(key, true);
|
Collection<E> itemCollection = getCacheItemCollection(key, true);
|
||||||
@ -58,7 +59,7 @@ implements CollectionCacheStore<E, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsElement(String key, E value) {
|
public boolean containsElement(CacheKey key, E value) {
|
||||||
Objects.requireNonNull(key);
|
Objects.requireNonNull(key);
|
||||||
Objects.requireNonNull(value);
|
Objects.requireNonNull(value);
|
||||||
Collection<E> itemCollection = getCacheItemCollection(key, false);
|
Collection<E> itemCollection = getCacheItemCollection(key, false);
|
||||||
@ -69,7 +70,7 @@ implements CollectionCacheStore<E, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty(String key) {
|
public boolean isEmpty(CacheKey key) {
|
||||||
Collection<E> itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false);
|
Collection<E> itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false);
|
||||||
if (itemCollection == null) {
|
if (itemCollection == null) {
|
||||||
return false;
|
return false;
|
||||||
@ -78,7 +79,7 @@ implements CollectionCacheStore<E, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int elementsLength(String key) {
|
public int elementsLength(CacheKey key) {
|
||||||
Collection<E> itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false);
|
Collection<E> itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false);
|
||||||
if (itemCollection == null) {
|
if (itemCollection == null) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -87,7 +88,7 @@ implements CollectionCacheStore<E, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean clearCollection(String key) {
|
public boolean clearCollection(CacheKey key) {
|
||||||
Collection<E> itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false);
|
Collection<E> itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false);
|
||||||
if (itemCollection == null) {
|
if (itemCollection == null) {
|
||||||
return false;
|
return false;
|
||||||
@ -97,7 +98,7 @@ implements CollectionCacheStore<E, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean removeElement(String key, E element) {
|
public boolean removeElement(CacheKey key, E element) {
|
||||||
Objects.requireNonNull(key);
|
Objects.requireNonNull(key);
|
||||||
Objects.requireNonNull(element);
|
Objects.requireNonNull(element);
|
||||||
Collection<E> itemCollection = getCacheItemCollection(key, false);
|
Collection<E> itemCollection = getCacheItemCollection(key, false);
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package net.lamgc.cgj.bot.cache.local;
|
package net.lamgc.cgj.bot.cache.local;
|
||||||
|
|
||||||
|
|
||||||
|
import net.lamgc.cgj.bot.cache.CacheKey;
|
||||||
import net.lamgc.cgj.bot.cache.MapCacheStore;
|
import net.lamgc.cgj.bot.cache.MapCacheStore;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -33,21 +34,22 @@ public class HashMapCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void nullThrowTest() {
|
public void nullThrowTest() {
|
||||||
final MapCacheStore<String> cacheStore = new HashMapCacheStore<>();
|
final MapCacheStore<String> cacheStore = new HashMapCacheStore<>();
|
||||||
|
final CacheKey key = new CacheKey("testKey");
|
||||||
|
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapSize(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapSize(null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapFieldSet(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapFieldSet(null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapValueSet(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapValueSet(null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.put(null, "field", "value"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.put(null, "field", "value"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.put("testKey", null, "value"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.put(key, null, "value"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.put("testKey", "field", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.put(key, "field", null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putAll(null, new HashMap<>()));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putAll(null, new HashMap<>()));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putAll("testKey", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putAll(key, null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putIfNotExist(null, "field", "value"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putIfNotExist(null, "field", "value"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putIfNotExist("testKey", null, "value"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putIfNotExist(key, null, "value"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putIfNotExist("testKey", "field", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.putIfNotExist(key, "field", null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.get("testKey", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.get(key, null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.get(null, "field"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.get(null, "field"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.removeField("testKey", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.removeField(key, null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.removeField(null, "field"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.removeField(null, "field"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapIsEmpty(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapIsEmpty(null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.clearMap(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.clearMap(null));
|
||||||
@ -56,7 +58,7 @@ public class HashMapCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void keyNotExistTest() {
|
public void keyNotExistTest() {
|
||||||
final MapCacheStore<String> cacheStore = new HashMapCacheStore<>();
|
final MapCacheStore<String> cacheStore = new HashMapCacheStore<>();
|
||||||
final String key = "testKey";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
|
|
||||||
Assert.assertEquals(-1, cacheStore.mapSize(key));
|
Assert.assertEquals(-1, cacheStore.mapSize(key));
|
||||||
Assert.assertFalse(cacheStore.mapIsEmpty(key));
|
Assert.assertFalse(cacheStore.mapIsEmpty(key));
|
||||||
@ -74,7 +76,7 @@ public class HashMapCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void putAndGetTest() {
|
public void putAndGetTest() {
|
||||||
final MapCacheStore<String> cacheStore = new HashMapCacheStore<>();
|
final MapCacheStore<String> cacheStore = new HashMapCacheStore<>();
|
||||||
final String key = "testKey";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
final Map<String, String> expectedMap = new HashMap<>();
|
final Map<String, String> expectedMap = new HashMap<>();
|
||||||
expectedMap.put("test01", "testValue01");
|
expectedMap.put("test01", "testValue01");
|
||||||
expectedMap.put("test02", "testValue02");
|
expectedMap.put("test02", "testValue02");
|
||||||
@ -103,7 +105,7 @@ public class HashMapCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void fieldChangeTest() {
|
public void fieldChangeTest() {
|
||||||
final MapCacheStore<String> cacheStore = new HashMapCacheStore<>();
|
final MapCacheStore<String> cacheStore = new HashMapCacheStore<>();
|
||||||
final String key = "testKey";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
final Map<String, String> expectedMap = new HashMap<>();
|
final Map<String, String> expectedMap = new HashMap<>();
|
||||||
expectedMap.put("test01", "testValue01");
|
expectedMap.put("test01", "testValue01");
|
||||||
expectedMap.put("test02", "testValue02");
|
expectedMap.put("test02", "testValue02");
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package net.lamgc.cgj.bot.cache.local;
|
package net.lamgc.cgj.bot.cache.local;
|
||||||
|
|
||||||
|
import net.lamgc.cgj.bot.cache.CacheKey;
|
||||||
import net.lamgc.cgj.bot.cache.SingleCacheStore;
|
import net.lamgc.cgj.bot.cache.SingleCacheStore;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -33,13 +34,14 @@ public class HashSingleCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void nullThrowTest() {
|
public void nullThrowTest() {
|
||||||
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
||||||
|
final CacheKey key = new CacheKey("testKey");
|
||||||
|
|
||||||
// HashSingleCacheStore
|
// HashSingleCacheStore
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.set(null, "testValue"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.set(null, "testValue"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.set("testKey", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.set(key, null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.get(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.get(null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.setIfNotExist(null, "testValue"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.setIfNotExist(null, "testValue"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.setIfNotExist("testKey", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.setIfNotExist(key, null));
|
||||||
|
|
||||||
// HashCacheStore
|
// HashCacheStore
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.exists(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.exists(null));
|
||||||
@ -51,11 +53,11 @@ public class HashSingleCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void setAndGetTest() {
|
public void setAndGetTest() {
|
||||||
SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
||||||
final String key = "test01";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
final String value = "testValue";
|
final String value = "testValue";
|
||||||
|
|
||||||
Assert.assertTrue("Set operation failed!", cacheStore.set(key, value));
|
Assert.assertTrue("Set operation failed!", cacheStore.set(key, value));
|
||||||
Assert.assertEquals(value, cacheStore.get("test01"));
|
Assert.assertEquals(value, cacheStore.get(key));
|
||||||
Assert.assertTrue("Remove operation failed!", cacheStore.remove(key));
|
Assert.assertTrue("Remove operation failed!", cacheStore.remove(key));
|
||||||
Assert.assertNull("Set operation failed!", cacheStore.get(key));
|
Assert.assertNull("Set operation failed!", cacheStore.get(key));
|
||||||
}
|
}
|
||||||
@ -63,7 +65,7 @@ public class HashSingleCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void setIfNotExistTest() {
|
public void setIfNotExistTest() {
|
||||||
SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
||||||
final String key = "test01";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
final String value = "testValue";
|
final String value = "testValue";
|
||||||
final String value2 = "testValue02";
|
final String value2 = "testValue02";
|
||||||
Assert.assertTrue("Set operation failed!", cacheStore.set(key, value));
|
Assert.assertTrue("Set operation failed!", cacheStore.set(key, value));
|
||||||
@ -75,7 +77,7 @@ public class HashSingleCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void expireTest() throws InterruptedException {
|
public void expireTest() throws InterruptedException {
|
||||||
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
||||||
final String key = "test01";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
final String value = "testValue";
|
final String value = "testValue";
|
||||||
|
|
||||||
// Cache
|
// Cache
|
||||||
@ -101,7 +103,7 @@ public class HashSingleCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void removeTest() {
|
public void removeTest() {
|
||||||
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
||||||
final String key = "test01";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
final String value = "testValue";
|
final String value = "testValue";
|
||||||
|
|
||||||
// 删除不存在Cache测试
|
// 删除不存在Cache测试
|
||||||
@ -114,7 +116,7 @@ public class HashSingleCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void clearTest() {
|
public void clearTest() {
|
||||||
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
||||||
final String key = "test01";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
final String value = "testValue";
|
final String value = "testValue";
|
||||||
|
|
||||||
Assert.assertTrue("Set operation failed!", cacheStore.set(key, value));
|
Assert.assertTrue("Set operation failed!", cacheStore.set(key, value));
|
||||||
@ -135,7 +137,7 @@ public class HashSingleCacheStoreTest {
|
|||||||
expectedMap.put("test06", "testValue06");
|
expectedMap.put("test06", "testValue06");
|
||||||
|
|
||||||
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
final SingleCacheStore<String> cacheStore = new HashSingleCacheStore<>();
|
||||||
expectedMap.forEach(cacheStore::set);
|
expectedMap.forEach((key, value) -> cacheStore.set(new CacheKey(key), value));
|
||||||
Assert.assertEquals(expectedMap.size(), cacheStore.size());
|
Assert.assertEquals(expectedMap.size(), cacheStore.size());
|
||||||
Assert.assertTrue(expectedMap.keySet().containsAll(cacheStore.keySet()));
|
Assert.assertTrue(expectedMap.keySet().containsAll(cacheStore.keySet()));
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package net.lamgc.cgj.bot.cache.local;
|
package net.lamgc.cgj.bot.cache.local;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import net.lamgc.cgj.bot.cache.CacheKey;
|
||||||
import net.lamgc.cgj.bot.cache.ListCacheStore;
|
import net.lamgc.cgj.bot.cache.ListCacheStore;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -35,19 +36,20 @@ public class ListCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void nullThrowTest() {
|
public void nullThrowTest() {
|
||||||
final ListCacheStore<String> cacheStore = new CopyOnWriteArrayListCacheStore<>();
|
final ListCacheStore<String> cacheStore = new CopyOnWriteArrayListCacheStore<>();
|
||||||
|
final CacheKey key = new CacheKey("testKey");
|
||||||
|
|
||||||
// LocalCollectionCacheStore
|
// LocalCollectionCacheStore
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.addElement(null, "testValue"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.addElement(null, "testValue"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.addElement("testKey", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.addElement(key, null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.addElements(null, new ArrayList<>()));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.addElements(null, new ArrayList<>()));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.addElements("testKey", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.addElements(key, null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.elementsLength(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.elementsLength(null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.containsElement(null, "testValue"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.containsElement(null, "testValue"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.containsElement("testKey", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.containsElement(key, null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.isEmpty(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.isEmpty(null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.clearCollection(null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.clearCollection(null));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.removeElement(null, "testValue"));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.removeElement(null, "testValue"));
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.removeElement("testKey", null));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.removeElement(key, null));
|
||||||
|
|
||||||
// CopyOnWriteArrayListCacheStore
|
// CopyOnWriteArrayListCacheStore
|
||||||
Assert.assertThrows(NullPointerException.class, () -> cacheStore.getElement(null, 0));
|
Assert.assertThrows(NullPointerException.class, () -> cacheStore.getElement(null, 0));
|
||||||
@ -59,7 +61,7 @@ public class ListCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void notExistCacheTest() {
|
public void notExistCacheTest() {
|
||||||
final ListCacheStore<String> cacheStore = new CopyOnWriteArrayListCacheStore<>();
|
final ListCacheStore<String> cacheStore = new CopyOnWriteArrayListCacheStore<>();
|
||||||
final String key = "testKey";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
|
|
||||||
Assert.assertFalse(cacheStore.clearCollection(key));
|
Assert.assertFalse(cacheStore.clearCollection(key));
|
||||||
Assert.assertFalse(cacheStore.isEmpty(key));
|
Assert.assertFalse(cacheStore.isEmpty(key));
|
||||||
@ -71,7 +73,7 @@ public class ListCacheStoreTest {
|
|||||||
@Test
|
@Test
|
||||||
public void addAndGetTest() {
|
public void addAndGetTest() {
|
||||||
final ListCacheStore<Integer> cacheStore = new CopyOnWriteArrayListCacheStore<>();
|
final ListCacheStore<Integer> cacheStore = new CopyOnWriteArrayListCacheStore<>();
|
||||||
final String key = "test01";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
List<Integer> numbers = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
List<Integer> numbers = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||||
// getElement/getElementsByRange Cache不存在测试
|
// getElement/getElementsByRange Cache不存在测试
|
||||||
Assert.assertNull(cacheStore.getElement(key, 0));
|
Assert.assertNull(cacheStore.getElement(key, 0));
|
||||||
@ -104,7 +106,7 @@ public class ListCacheStoreTest {
|
|||||||
public void removeElementTest() {
|
public void removeElementTest() {
|
||||||
// removeElement(String, E) / removeElement(String, int)
|
// removeElement(String, E) / removeElement(String, int)
|
||||||
final ListCacheStore<String> cacheStore = new CopyOnWriteArrayListCacheStore<>();
|
final ListCacheStore<String> cacheStore = new CopyOnWriteArrayListCacheStore<>();
|
||||||
final String key = "test01";
|
final CacheKey key = new CacheKey("testKey");
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
List<String> numbers = Lists.newArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9");
|
List<String> numbers = Lists.newArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user