[Update] CacheStore-local 适配因加入 CacheKey 带来的变更;

[Update] * 适配更改;
This commit is contained in:
2020-09-05 11:32:52 +08:00
parent a24b810183
commit 3c705ee16a
9 changed files with 90 additions and 75 deletions

View File

@ -17,6 +17,7 @@
package net.lamgc.cgj.bot.cache.local;
import net.lamgc.cgj.bot.cache.CacheKey;
import net.lamgc.cgj.bot.cache.ListCacheStore;
import java.util.*;
@ -30,7 +31,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteArrayListCacheStore<E> extends LocalCollectionCacheStore<E, List<E>> implements ListCacheStore<E> {
@Override
public E getElement(String key, int index) {
public E getElement(CacheKey key, int index) {
List<E> itemCollection = getCacheItemCollection(key, false);
try {
return itemCollection == null ? null : itemCollection.get(index);
@ -40,7 +41,7 @@ public class CopyOnWriteArrayListCacheStore<E> extends LocalCollectionCacheStore
}
@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);
if (listLength == -1) {
return null;
@ -62,7 +63,7 @@ public class CopyOnWriteArrayListCacheStore<E> extends LocalCollectionCacheStore
}
@Override
public boolean removeElement(String key, int index) {
public boolean removeElement(CacheKey key, int index) {
List<E> itemCollection = getCacheItemCollection(key, false);
if (itemCollection != null) {
try {
@ -76,16 +77,16 @@ public class CopyOnWriteArrayListCacheStore<E> extends LocalCollectionCacheStore
}
@Override
protected List<E> getCacheItemCollection(String key, boolean create) {
protected List<E> getCacheItemCollection(CacheKey key, boolean create) {
Objects.requireNonNull(key);
Map<String, CacheItem<List<E>>> cacheMap = getCacheMap();
if (!cacheMap.containsKey(key)) {
if (!cacheMap.containsKey(key.toString())) {
if (create) {
cacheMap.put(key, new CacheItem<>(new CopyOnWriteArrayList<>()));
cacheMap.put(key.toString(), new CacheItem<>(new CopyOnWriteArrayList<>()));
} else {
return null;
}
}
return cacheMap.get(key).getValue();
return cacheMap.get(key.toString()).getValue();
}
}

View File

@ -17,6 +17,7 @@
package net.lamgc.cgj.bot.cache.local;
import net.lamgc.cgj.bot.cache.CacheKey;
import net.lamgc.cgj.bot.cache.CacheStore;
import java.util.*;
@ -43,21 +44,21 @@ public abstract class HashCacheStore<V> implements CacheStore<V>, Cleanable {
}
@Override
public boolean setTimeToLive(String key, long ttl) {
public boolean setTimeToLive(CacheKey key, long ttl) {
if (!exists(key)) {
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));
return true;
}
@Override
public long getTimeToLive(String key) {
public long getTimeToLive(CacheKey key) {
if (!exists(key)) {
return -1;
}
CacheItem<V> item = cacheMap.get(key);
CacheItem<V> item = cacheMap.get(key.toString());
Date expireDate = item.getExpireDate();
if (expireDate != null) {
return expireDate.getTime() - System.currentTimeMillis();
@ -77,11 +78,11 @@ public abstract class HashCacheStore<V> implements CacheStore<V>, Cleanable {
}
@Override
public boolean exists(String key) {
if (!cacheMap.containsKey(key)) {
public boolean exists(CacheKey key) {
if (!cacheMap.containsKey(key.toString())) {
return false;
}
CacheItem<V> item = cacheMap.get(key);
CacheItem<V> item = cacheMap.get(key.toString());
// 在检查其过期情况后根据情况进行清理, 减轻主动清理机制的负担.
if (item.isExpire(new Date())) {
remove(key);
@ -91,9 +92,9 @@ public abstract class HashCacheStore<V> implements CacheStore<V>, Cleanable {
}
@Override
public boolean remove(String key) {
public boolean remove(CacheKey key) {
// 根据 Collection 说明, 删除时 key 存在映射就会返回, 只要返回 null 就代表没有.
return cacheMap.remove(key) != null;
return cacheMap.remove(key.toString()) != null;
}
@Override

View File

@ -17,6 +17,7 @@
package net.lamgc.cgj.bot.cache.local;
import net.lamgc.cgj.bot.cache.CacheKey;
import net.lamgc.cgj.bot.cache.MapCacheStore;
import java.util.*;
@ -32,22 +33,22 @@ import java.util.function.Function;
public class HashMapCacheStore<V> extends HashCacheStore<Map<String, V>> implements MapCacheStore<V> {
@Override
public int mapSize(String key) {
public int mapSize(CacheKey key) {
return getMap(key, false, Map::size, -1);
}
@Override
public Set<String> mapFieldSet(String key) {
public Set<String> mapFieldSet(CacheKey key) {
return getMap(key, false, map -> Collections.unmodifiableSet(map.keySet()), null);
}
@Override
public Set<V> mapValueSet(String key) {
public Set<V> mapValueSet(CacheKey key) {
return getMap(key, false, map -> new HashSet<>(map.values()), null);
}
@Override
public boolean put(String key, String field, V value) {
public boolean put(CacheKey key, String field, V value) {
return getMap(key, true, map -> {
map.put(Objects.requireNonNull(field), Objects.requireNonNull(value));
return true;
@ -55,7 +56,7 @@ public class HashMapCacheStore<V> extends HashCacheStore<Map<String, V>> impleme
}
@Override
public boolean putAll(String key, Map<String, V> map) {
public boolean putAll(CacheKey key, Map<String, V> map) {
return getMap(key, true, keyMap -> {
keyMap.putAll(Objects.requireNonNull(map));
return true;
@ -63,7 +64,7 @@ public class HashMapCacheStore<V> extends HashCacheStore<Map<String, V>> impleme
}
@Override
public boolean putIfNotExist(String key, String field, V value) {
public boolean putIfNotExist(CacheKey key, String field, V value) {
return getMap(key, true, map -> {
if (map.containsKey(Objects.requireNonNull(field))) {
return false;
@ -74,44 +75,45 @@ public class HashMapCacheStore<V> extends HashCacheStore<Map<String, V>> impleme
}
@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);
}
@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);
}
@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);
}
@Override
public boolean mapIsEmpty(String key) {
public boolean mapIsEmpty(CacheKey key) {
return getMap(key, false, Map::isEmpty, false);
}
@Override
public boolean clearMap(String key) {
public boolean clearMap(CacheKey key) {
return getMap(key, false, map -> {
map.clear();
return true;
}, 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);
String keyString = key.toString();
Map<String, CacheItem<Map<String, V>>> cacheMap = getCacheMap();
if (!cacheMap.containsKey(key)) {
if (!cacheMap.containsKey(keyString)) {
if (create) {
cacheMap.put(key, new CacheItem<>(new Hashtable<>()));
cacheMap.put(keyString, new CacheItem<>(new Hashtable<>()));
} else {
return isNull;
}
}
return notNull.apply(cacheMap.get(key).getValue());
return notNull.apply(cacheMap.get(keyString).getValue());
}
}

View File

@ -17,6 +17,7 @@
package net.lamgc.cgj.bot.cache.local;
import net.lamgc.cgj.bot.cache.CacheKey;
import net.lamgc.cgj.bot.cache.SetCacheStore;
import java.util.*;
@ -29,16 +30,18 @@ import java.util.*;
public class HashSetCacheStore<E> extends LocalCollectionCacheStore<E, Set<E>> implements SetCacheStore<E> {
@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();
if (!cacheMap.containsKey(key)) {
if (!cacheMap.containsKey(keyString)) {
if (create) {
cacheMap.put(key, new CacheItem<>(new HashSet<>()));
cacheMap.put(keyString, new CacheItem<>(new HashSet<>()));
} else {
return null;
}
}
return cacheMap.get(key).getValue();
return cacheMap.get(keyString).getValue();
}
}

View File

@ -17,6 +17,7 @@
package net.lamgc.cgj.bot.cache.local;
import net.lamgc.cgj.bot.cache.CacheKey;
import net.lamgc.cgj.bot.cache.SingleCacheStore;
import java.util.Objects;
@ -29,13 +30,13 @@ import java.util.Objects;
public class HashSingleCacheStore<V> extends HashCacheStore<V> implements SingleCacheStore<V> {
@Override
public boolean set(String key, V value) {
getCacheMap().put(Objects.requireNonNull(key), new CacheItem<>(Objects.requireNonNull(value)));
public boolean set(CacheKey key, V value) {
getCacheMap().put(Objects.requireNonNull(key).toString(), new CacheItem<>(Objects.requireNonNull(value)));
return true;
}
@Override
public boolean setIfNotExist(String key, V value) {
public boolean setIfNotExist(CacheKey key, V value) {
if (exists(key)) {
return false;
}
@ -43,11 +44,11 @@ public class HashSingleCacheStore<V> extends HashCacheStore<V> implements Single
}
@Override
public V get(String key) {
public V get(CacheKey key) {
if (!exists(key)) {
return null;
}
return getCacheMap().get(key).getValue();
return getCacheMap().get(key.toString()).getValue();
}
}

View File

@ -17,6 +17,7 @@
package net.lamgc.cgj.bot.cache.local;
import net.lamgc.cgj.bot.cache.CacheKey;
import net.lamgc.cgj.bot.cache.CollectionCacheStore;
import java.util.Collection;
@ -39,10 +40,10 @@ implements CollectionCacheStore<E, C> {
* @param create 如果不存在, 是否创建.
* @return 如果不存在且 create 为 false, 或添加失败, 返回 false, 添加成功返回 true.
*/
protected abstract C getCacheItemCollection(String key, boolean create);
protected abstract C getCacheItemCollection(CacheKey key, boolean create);
@Override
public boolean addElement(String key, E element) {
public boolean addElement(CacheKey key, E element) {
Objects.requireNonNull(key);
Objects.requireNonNull(element);
Collection<E> itemCollection = getCacheItemCollection(key, true);
@ -50,7 +51,7 @@ implements CollectionCacheStore<E, C> {
}
@Override
public boolean addElements(String key, Collection<E> elements) {
public boolean addElements(CacheKey key, Collection<E> elements) {
Objects.requireNonNull(key);
Objects.requireNonNull(elements);
Collection<E> itemCollection = getCacheItemCollection(key, true);
@ -58,7 +59,7 @@ implements CollectionCacheStore<E, C> {
}
@Override
public boolean containsElement(String key, E value) {
public boolean containsElement(CacheKey key, E value) {
Objects.requireNonNull(key);
Objects.requireNonNull(value);
Collection<E> itemCollection = getCacheItemCollection(key, false);
@ -69,7 +70,7 @@ implements CollectionCacheStore<E, C> {
}
@Override
public boolean isEmpty(String key) {
public boolean isEmpty(CacheKey key) {
Collection<E> itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false);
if (itemCollection == null) {
return false;
@ -78,7 +79,7 @@ implements CollectionCacheStore<E, C> {
}
@Override
public int elementsLength(String key) {
public int elementsLength(CacheKey key) {
Collection<E> itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false);
if (itemCollection == null) {
return -1;
@ -87,7 +88,7 @@ implements CollectionCacheStore<E, C> {
}
@Override
public boolean clearCollection(String key) {
public boolean clearCollection(CacheKey key) {
Collection<E> itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false);
if (itemCollection == null) {
return false;
@ -97,7 +98,7 @@ implements CollectionCacheStore<E, C> {
}
@Override
public boolean removeElement(String key, E element) {
public boolean removeElement(CacheKey key, E element) {
Objects.requireNonNull(key);
Objects.requireNonNull(element);
Collection<E> itemCollection = getCacheItemCollection(key, false);