perf: 优化缓存代码

pull/10/head
Parker 3 years ago
parent 20845690c7
commit 7a6327f729

@ -413,6 +413,32 @@ public final class SecurityCache {
LFU_NULL_CACHE.invalidate(tempKey);
}
/**
* Hash
* @param redisTemplate redisTemplate
* @param key
* @param fields
* @return boolean
*/
public static boolean hDelMore(
final RedisTemplate<String, Object> redisTemplate,
final String key, final String... fields) {
if (null == redisTemplate || null == key
|| null == fields ) {
throw new RuntimeException("入参[redisTemplate,key,fields]必填");
}
int count = fields.length;
for (String field : fields) {
boolean isDel = hDel(redisTemplate, key, field);
if(isDel){
count--;
}
}
return 0 == count;
}
/**
* Hash
* @param redisTemplate redisTemplate
@ -423,7 +449,8 @@ public final class SecurityCache {
public static boolean hDel(
final RedisTemplate<String, Object> redisTemplate,
final String key, final String field) {
if (null == redisTemplate || null == key) {
if (null == redisTemplate || null == key
|| null == field ) {
throw new RuntimeException("入参[redisTemplate,key,field]必填");
}
@ -444,6 +471,28 @@ public final class SecurityCache {
}
/**
*
* @param redisTemplate redisTemplate
* @param keys
*/
public static boolean removeMore(
final RedisTemplate<String, Object> redisTemplate,
final String... keys) {
if (null == redisTemplate || null == keys) {
throw new RuntimeException("入参[redisTemplate,keys]必填");
}
int count = keys.length;
for (String key : keys) {
boolean isRemove = remove(redisTemplate, key);
if(isRemove){
count--;
}
}
return 0 == count;
}
/**
*
* @param redisTemplate redisTemplate

@ -243,22 +243,7 @@ public class DictUtil {
String cacheKeyByName = CacheUtil.formatKey(
RedisConstants.PREFIX_DICT_NAME + typeCode);
// 计数器
int count = 2;
{
boolean tmp = SecurityCache.remove(redisTemplate, cacheKeyByValue);
if(tmp){
count--;
}
}
{
boolean tmp = SecurityCache.remove(redisTemplate, cacheKeyByName);
if(tmp){
count--;
}
}
return count == 0;
return SecurityCache.removeMore(redisTemplate, cacheKeyByValue, cacheKeyByName);
}

@ -498,25 +498,8 @@ public class UserUtil {
String cacheKeyByUserId = CacheUtil.formatKey(RedisConstants.PREFIX_USER_ID + user.getId());
String cacheKeyByUsername = CacheUtil.formatKey(RedisConstants.PREFIX_USERNAME + user.getUsername());
// 计数器
int count = 2;
{
// 清除空拦截
boolean tmp = SecurityCache.remove(redisTemplate, cacheKeyByUserId);
if(tmp){
count--;
}
}
{
// 清除空拦截
boolean tmp = SecurityCache.remove(redisTemplate, cacheKeyByUsername);
if(tmp){
count--;
}
}
return count == 0;
return SecurityCache.removeMore(redisTemplate,
cacheKeyByUserId, cacheKeyByUsername);
}

Loading…
Cancel
Save