From 3c705ee16af3e511878d0e5c1ac55ecae95b0438 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 5 Sep 2020 11:32:52 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20CacheStore-local=20=E9=80=82?= =?UTF-8?q?=E9=85=8D=E5=9B=A0=E5=8A=A0=E5=85=A5=20CacheKey=20=E5=B8=A6?= =?UTF-8?q?=E6=9D=A5=E7=9A=84=E5=8F=98=E6=9B=B4;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Update] * 适配更改; --- .../local/CopyOnWriteArrayListCacheStore.java | 15 +++++---- .../cgj/bot/cache/local/HashCacheStore.java | 19 +++++------ .../bot/cache/local/HashMapCacheStore.java | 32 ++++++++++--------- .../bot/cache/local/HashSetCacheStore.java | 11 ++++--- .../bot/cache/local/HashSingleCacheStore.java | 11 ++++--- .../local/LocalCollectionCacheStore.java | 17 +++++----- .../cache/local/HashMapCacheStoreTest.java | 24 +++++++------- .../cache/local/HashSingleCacheStoreTest.java | 20 ++++++------ .../bot/cache/local/ListCacheStoreTest.java | 16 ++++++---- 9 files changed, 90 insertions(+), 75 deletions(-) diff --git a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/CopyOnWriteArrayListCacheStore.java b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/CopyOnWriteArrayListCacheStore.java index 23fb17f..d8358b9 100644 --- a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/CopyOnWriteArrayListCacheStore.java +++ b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/CopyOnWriteArrayListCacheStore.java @@ -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 extends LocalCollectionCacheStore> implements ListCacheStore { @Override - public E getElement(String key, int index) { + public E getElement(CacheKey key, int index) { List itemCollection = getCacheItemCollection(key, false); try { return itemCollection == null ? null : itemCollection.get(index); @@ -40,7 +41,7 @@ public class CopyOnWriteArrayListCacheStore extends LocalCollectionCacheStore } @Override - public List getElementsByRange(String key, int index, int length) { + public List getElementsByRange(CacheKey key, int index, int length) { int listLength = elementsLength(key); if (listLength == -1) { return null; @@ -62,7 +63,7 @@ public class CopyOnWriteArrayListCacheStore extends LocalCollectionCacheStore } @Override - public boolean removeElement(String key, int index) { + public boolean removeElement(CacheKey key, int index) { List itemCollection = getCacheItemCollection(key, false); if (itemCollection != null) { try { @@ -76,16 +77,16 @@ public class CopyOnWriteArrayListCacheStore extends LocalCollectionCacheStore } @Override - protected List getCacheItemCollection(String key, boolean create) { + protected List getCacheItemCollection(CacheKey key, boolean create) { Objects.requireNonNull(key); Map>> 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(); } } diff --git a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashCacheStore.java b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashCacheStore.java index 124560f..2e644ac 100644 --- a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashCacheStore.java +++ b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashCacheStore.java @@ -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 implements CacheStore, Cleanable { } @Override - public boolean setTimeToLive(String key, long ttl) { + public boolean setTimeToLive(CacheKey key, long ttl) { if (!exists(key)) { return false; } - CacheItem item = cacheMap.get(key); + CacheItem 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 item = cacheMap.get(key); + CacheItem 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 implements CacheStore, Cleanable { } @Override - public boolean exists(String key) { - if (!cacheMap.containsKey(key)) { + public boolean exists(CacheKey key) { + if (!cacheMap.containsKey(key.toString())) { return false; } - CacheItem item = cacheMap.get(key); + CacheItem item = cacheMap.get(key.toString()); // 在检查其过期情况后根据情况进行清理, 减轻主动清理机制的负担. if (item.isExpire(new Date())) { remove(key); @@ -91,9 +92,9 @@ public abstract class HashCacheStore implements CacheStore, 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 diff --git a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashMapCacheStore.java b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashMapCacheStore.java index 1c10794..7b7713c 100644 --- a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashMapCacheStore.java +++ b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashMapCacheStore.java @@ -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 extends HashCacheStore> implements MapCacheStore { @Override - public int mapSize(String key) { + public int mapSize(CacheKey key) { return getMap(key, false, Map::size, -1); } @Override - public Set mapFieldSet(String key) { + public Set mapFieldSet(CacheKey key) { return getMap(key, false, map -> Collections.unmodifiableSet(map.keySet()), null); } @Override - public Set mapValueSet(String key) { + public Set 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 extends HashCacheStore> impleme } @Override - public boolean putAll(String key, Map map) { + public boolean putAll(CacheKey key, Map map) { return getMap(key, true, keyMap -> { keyMap.putAll(Objects.requireNonNull(map)); return true; @@ -63,7 +64,7 @@ public class HashMapCacheStore extends HashCacheStore> 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 extends HashCacheStore> 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 getMap(String key, boolean create, Function, R> notNull, R isNull) { + private R getMap(CacheKey key, boolean create, Function, R> notNull, R isNull) { Objects.requireNonNull(key); + String keyString = key.toString(); Map>> 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()); } } diff --git a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashSetCacheStore.java b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashSetCacheStore.java index b59fe0e..e4b15ff 100644 --- a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashSetCacheStore.java +++ b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashSetCacheStore.java @@ -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 extends LocalCollectionCacheStore> implements SetCacheStore { @Override - protected Set getCacheItemCollection(String key, boolean create) { + protected Set getCacheItemCollection(CacheKey key, boolean create) { + Objects.requireNonNull(key); + String keyString = key.toString(); Map>> 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(); } } diff --git a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStore.java b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStore.java index cfc5875..3c1c818 100644 --- a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStore.java +++ b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStore.java @@ -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 extends HashCacheStore implements SingleCacheStore { @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 extends HashCacheStore 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(); } } diff --git a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCollectionCacheStore.java b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCollectionCacheStore.java index cb6f3a4..e097b0e 100644 --- a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCollectionCacheStore.java +++ b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCollectionCacheStore.java @@ -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 { * @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 itemCollection = getCacheItemCollection(key, true); @@ -50,7 +51,7 @@ implements CollectionCacheStore { } @Override - public boolean addElements(String key, Collection elements) { + public boolean addElements(CacheKey key, Collection elements) { Objects.requireNonNull(key); Objects.requireNonNull(elements); Collection itemCollection = getCacheItemCollection(key, true); @@ -58,7 +59,7 @@ implements CollectionCacheStore { } @Override - public boolean containsElement(String key, E value) { + public boolean containsElement(CacheKey key, E value) { Objects.requireNonNull(key); Objects.requireNonNull(value); Collection itemCollection = getCacheItemCollection(key, false); @@ -69,7 +70,7 @@ implements CollectionCacheStore { } @Override - public boolean isEmpty(String key) { + public boolean isEmpty(CacheKey key) { Collection itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false); if (itemCollection == null) { return false; @@ -78,7 +79,7 @@ implements CollectionCacheStore { } @Override - public int elementsLength(String key) { + public int elementsLength(CacheKey key) { Collection itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false); if (itemCollection == null) { return -1; @@ -87,7 +88,7 @@ implements CollectionCacheStore { } @Override - public boolean clearCollection(String key) { + public boolean clearCollection(CacheKey key) { Collection itemCollection = getCacheItemCollection(Objects.requireNonNull(key), false); if (itemCollection == null) { return false; @@ -97,7 +98,7 @@ implements CollectionCacheStore { } @Override - public boolean removeElement(String key, E element) { + public boolean removeElement(CacheKey key, E element) { Objects.requireNonNull(key); Objects.requireNonNull(element); Collection itemCollection = getCacheItemCollection(key, false); diff --git a/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashMapCacheStoreTest.java b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashMapCacheStoreTest.java index 906a279..549d110 100644 --- a/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashMapCacheStoreTest.java +++ b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashMapCacheStoreTest.java @@ -18,6 +18,7 @@ package net.lamgc.cgj.bot.cache.local; +import net.lamgc.cgj.bot.cache.CacheKey; import net.lamgc.cgj.bot.cache.MapCacheStore; import org.junit.Assert; import org.junit.Test; @@ -33,21 +34,22 @@ public class HashMapCacheStoreTest { @Test public void nullThrowTest() { final MapCacheStore cacheStore = new HashMapCacheStore<>(); - + final CacheKey key = new CacheKey("testKey"); + Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapSize(null)); Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapFieldSet(null)); Assert.assertThrows(NullPointerException.class, () -> cacheStore.mapValueSet(null)); Assert.assertThrows(NullPointerException.class, () -> cacheStore.put(null, "field", "value")); - Assert.assertThrows(NullPointerException.class, () -> cacheStore.put("testKey", null, "value")); - Assert.assertThrows(NullPointerException.class, () -> cacheStore.put("testKey", "field", null)); + Assert.assertThrows(NullPointerException.class, () -> cacheStore.put(key, null, "value")); + Assert.assertThrows(NullPointerException.class, () -> cacheStore.put(key, "field", null)); 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("testKey", null, "value")); - Assert.assertThrows(NullPointerException.class, () -> cacheStore.putIfNotExist("testKey", "field", null)); - Assert.assertThrows(NullPointerException.class, () -> cacheStore.get("testKey", null)); + Assert.assertThrows(NullPointerException.class, () -> cacheStore.putIfNotExist(key, null, "value")); + Assert.assertThrows(NullPointerException.class, () -> cacheStore.putIfNotExist(key, "field", null)); + Assert.assertThrows(NullPointerException.class, () -> cacheStore.get(key, null)); 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.mapIsEmpty(null)); Assert.assertThrows(NullPointerException.class, () -> cacheStore.clearMap(null)); @@ -56,7 +58,7 @@ public class HashMapCacheStoreTest { @Test public void keyNotExistTest() { final MapCacheStore cacheStore = new HashMapCacheStore<>(); - final String key = "testKey"; + final CacheKey key = new CacheKey("testKey"); Assert.assertEquals(-1, cacheStore.mapSize(key)); Assert.assertFalse(cacheStore.mapIsEmpty(key)); @@ -74,7 +76,7 @@ public class HashMapCacheStoreTest { @Test public void putAndGetTest() { final MapCacheStore cacheStore = new HashMapCacheStore<>(); - final String key = "testKey"; + final CacheKey key = new CacheKey("testKey"); final Map expectedMap = new HashMap<>(); expectedMap.put("test01", "testValue01"); expectedMap.put("test02", "testValue02"); @@ -103,7 +105,7 @@ public class HashMapCacheStoreTest { @Test public void fieldChangeTest() { final MapCacheStore cacheStore = new HashMapCacheStore<>(); - final String key = "testKey"; + final CacheKey key = new CacheKey("testKey"); final Map expectedMap = new HashMap<>(); expectedMap.put("test01", "testValue01"); expectedMap.put("test02", "testValue02"); diff --git a/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStoreTest.java b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStoreTest.java index f97b27d..cd5eb90 100644 --- a/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStoreTest.java +++ b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/HashSingleCacheStoreTest.java @@ -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 org.junit.Assert; import org.junit.Test; @@ -33,13 +34,14 @@ public class HashSingleCacheStoreTest { @Test public void nullThrowTest() { final SingleCacheStore cacheStore = new HashSingleCacheStore<>(); + final CacheKey key = new CacheKey("testKey"); // HashSingleCacheStore 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.setIfNotExist(null, "testValue")); - Assert.assertThrows(NullPointerException.class, () -> cacheStore.setIfNotExist("testKey", null)); + Assert.assertThrows(NullPointerException.class, () -> cacheStore.setIfNotExist(key, null)); // HashCacheStore Assert.assertThrows(NullPointerException.class, () -> cacheStore.exists(null)); @@ -51,11 +53,11 @@ public class HashSingleCacheStoreTest { @Test public void setAndGetTest() { SingleCacheStore cacheStore = new HashSingleCacheStore<>(); - final String key = "test01"; + final CacheKey key = new CacheKey("testKey"); final String value = "testValue"; 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.assertNull("Set operation failed!", cacheStore.get(key)); } @@ -63,7 +65,7 @@ public class HashSingleCacheStoreTest { @Test public void setIfNotExistTest() { SingleCacheStore cacheStore = new HashSingleCacheStore<>(); - final String key = "test01"; + final CacheKey key = new CacheKey("testKey"); final String value = "testValue"; final String value2 = "testValue02"; Assert.assertTrue("Set operation failed!", cacheStore.set(key, value)); @@ -75,7 +77,7 @@ public class HashSingleCacheStoreTest { @Test public void expireTest() throws InterruptedException { final SingleCacheStore cacheStore = new HashSingleCacheStore<>(); - final String key = "test01"; + final CacheKey key = new CacheKey("testKey"); final String value = "testValue"; // Cache @@ -101,7 +103,7 @@ public class HashSingleCacheStoreTest { @Test public void removeTest() { final SingleCacheStore cacheStore = new HashSingleCacheStore<>(); - final String key = "test01"; + final CacheKey key = new CacheKey("testKey"); final String value = "testValue"; // 删除不存在Cache测试 @@ -114,7 +116,7 @@ public class HashSingleCacheStoreTest { @Test public void clearTest() { final SingleCacheStore cacheStore = new HashSingleCacheStore<>(); - final String key = "test01"; + final CacheKey key = new CacheKey("testKey"); final String value = "testValue"; Assert.assertTrue("Set operation failed!", cacheStore.set(key, value)); @@ -135,7 +137,7 @@ public class HashSingleCacheStoreTest { expectedMap.put("test06", "testValue06"); final SingleCacheStore cacheStore = new HashSingleCacheStore<>(); - expectedMap.forEach(cacheStore::set); + expectedMap.forEach((key, value) -> cacheStore.set(new CacheKey(key), value)); Assert.assertEquals(expectedMap.size(), cacheStore.size()); Assert.assertTrue(expectedMap.keySet().containsAll(cacheStore.keySet())); } diff --git a/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/ListCacheStoreTest.java b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/ListCacheStoreTest.java index 4db32bc..24fc253 100644 --- a/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/ListCacheStoreTest.java +++ b/ContentGrabbingJi-CacheStore-local/src/test/java/net/lamgc/cgj/bot/cache/local/ListCacheStoreTest.java @@ -18,6 +18,7 @@ package net.lamgc.cgj.bot.cache.local; import com.google.common.collect.Lists; +import net.lamgc.cgj.bot.cache.CacheKey; import net.lamgc.cgj.bot.cache.ListCacheStore; import org.junit.Assert; import org.junit.Test; @@ -35,19 +36,20 @@ public class ListCacheStoreTest { @Test public void nullThrowTest() { final ListCacheStore cacheStore = new CopyOnWriteArrayListCacheStore<>(); + final CacheKey key = new CacheKey("testKey"); // LocalCollectionCacheStore 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("testKey", null)); + Assert.assertThrows(NullPointerException.class, () -> cacheStore.addElements(key, null)); Assert.assertThrows(NullPointerException.class, () -> cacheStore.elementsLength(null)); 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.clearCollection(null)); 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 Assert.assertThrows(NullPointerException.class, () -> cacheStore.getElement(null, 0)); @@ -59,7 +61,7 @@ public class ListCacheStoreTest { @Test public void notExistCacheTest() { final ListCacheStore cacheStore = new CopyOnWriteArrayListCacheStore<>(); - final String key = "testKey"; + final CacheKey key = new CacheKey("testKey"); Assert.assertFalse(cacheStore.clearCollection(key)); Assert.assertFalse(cacheStore.isEmpty(key)); @@ -71,7 +73,7 @@ public class ListCacheStoreTest { @Test public void addAndGetTest() { final ListCacheStore cacheStore = new CopyOnWriteArrayListCacheStore<>(); - final String key = "test01"; + final CacheKey key = new CacheKey("testKey"); List numbers = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9); // getElement/getElementsByRange Cache不存在测试 Assert.assertNull(cacheStore.getElement(key, 0)); @@ -104,7 +106,7 @@ public class ListCacheStoreTest { public void removeElementTest() { // removeElement(String, E) / removeElement(String, int) final ListCacheStore cacheStore = new CopyOnWriteArrayListCacheStore<>(); - final String key = "test01"; + final CacheKey key = new CacheKey("testKey"); Random random = new Random(); List numbers = Lists.newArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9");