[Fix] CacheStore-Redis 修复 RedisCacheStore 部分操作对于作用 Key 的范围不明确的问题;

[Fix] RedisCacheStore 修复 'clear()', 'keySet()' 和 'size()' 方法影响到不属于 CacheStore 的问题;
[Update] RedisSingleCacheStoreTest 完善测试项;
This commit is contained in:
LamGC 2021-01-11 16:42:06 +08:00
parent 2bdfbeb72d
commit a6fc04c07e
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 18 additions and 7 deletions

View File

@ -19,9 +19,10 @@ package net.lamgc.cgj.bot.cache.redis;
import net.lamgc.cgj.bot.cache.CacheKey;
import net.lamgc.cgj.bot.cache.CacheStore;
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
@ -62,7 +63,7 @@ public abstract class RedisCacheStore<V> implements CacheStore<V> {
} else {
result = jedis.persist(keyString);
}
return result == RedisUtils.RETURN_CODE_OK;
return result.intValue() == RedisUtils.RETURN_CODE_OK;
});
}
@ -76,12 +77,15 @@ public abstract class RedisCacheStore<V> implements CacheStore<V> {
@Override
public long size() {
return connectionPool.executeRedis(Jedis::dbSize);
return (long) connectionPool.executeRedis(jedis -> jedis.keys(getKeyString(RedisUtils.CACHE_KEY_ALL)).size());
}
@Override
public boolean clear() {
return connectionPool.executeRedis(jedis -> RedisUtils.isOk(jedis.flushDB()));
List<String> keys = new ArrayList<>(1);
keys.add(getKeyString(RedisUtils.CACHE_KEY_ALL));
connectionPool.executeScript(LuaScript.STORE_REMOVE_KEYS_BY_PREFIX, keys, null);
return true;
}
@Override
@ -96,7 +100,8 @@ public abstract class RedisCacheStore<V> implements CacheStore<V> {
@Override
public Set<String> keySet() {
Set<String> keys = connectionPool.executeRedis(jedis -> jedis.keys(RedisUtils.KEY_PATTERN_ALL));
Set<String> keys = connectionPool.executeRedis(jedis ->
jedis.keys(getKeyString(RedisUtils.CACHE_KEY_ALL)));
final int prefixLength = getKeyPrefix().length();
Set<String> newKeys = new HashSet<>();
for (String key : keys) {

View File

@ -53,7 +53,7 @@ public class RedisSingleCacheStoreTest {
}
}
private final static SingleCacheStore<String> cacheStore = factory.newSingleCacheStore("test", new StringToStringConverter());
private final static SingleCacheStore<String> cacheStore = factory.newSingleCacheStore("test:single", new StringToStringConverter());
@Before
public void before() {
@ -62,7 +62,7 @@ public class RedisSingleCacheStoreTest {
@Test
public void nullThrowTest() {
final SingleCacheStore<String> tempCacheStore = factory.newSingleCacheStore("test" + RedisUtils.KEY_SEPARATOR, new StringToStringConverter());
final SingleCacheStore<String> tempCacheStore = factory.newSingleCacheStore("test:single" + RedisUtils.KEY_SEPARATOR, new StringToStringConverter());
final CacheKey key = new CacheKey("testKey");
// RedisSingleCacheStore
@ -140,14 +140,18 @@ public class RedisSingleCacheStoreTest {
@Test
public void clearTest() {
final SingleCacheStore<String> secondSingleCacheStore =
factory.newSingleCacheStore("test:single_b", new StringToStringConverter());
final CacheKey key = new CacheKey("testKey");
final String value = "testValue";
Assert.assertTrue("Set operation failed!", cacheStore.set(key, value));
Assert.assertTrue("Set operation failed!", secondSingleCacheStore.set(key, value));
Assert.assertTrue(cacheStore.exists(key));
Assert.assertTrue("Clear operation failed!", cacheStore.clear());
Assert.assertFalse(cacheStore.exists(key));
Assert.assertTrue(secondSingleCacheStore.exists(key));
}
@Test
@ -160,9 +164,11 @@ public class RedisSingleCacheStoreTest {
expectedMap.put("test05", "testValue05");
expectedMap.put("test06", "testValue06");
Assert.assertEquals(0, cacheStore.size());
expectedMap.forEach((key, value) -> cacheStore.set(new CacheKey(key), value));
Assert.assertEquals(expectedMap.size(), cacheStore.size());
Assert.assertTrue(expectedMap.keySet().containsAll(cacheStore.keySet()));
Assert.assertTrue(cacheStore.keySet().containsAll(expectedMap.keySet()));
}
}