refactor: 修改缓存处理逻辑.

This commit is contained in:
lijiahang
2023-11-21 13:53:33 +08:00
parent 69c71055d9
commit 5c21dd1144
22 changed files with 358 additions and 146 deletions

View File

@@ -45,19 +45,9 @@ public class CodeGenerators {
// .color("blue", "gray", "red", "green", "white")
// .valueUseFields()
// .build(),
Template.create("data_group", "数据分组", "data")
Template.create("data_permission", "数据权限", "data")
.enableProviderApi()
.disableUnitTest()
.cache("data:group:{}", "数据分组 ${type}")
.expire(1, TimeUnit.DAYS)
.vue("system", "data-group")
.build(),
Template.create("data_group_rel", "数据分组关联", "data")
.enableProviderApi()
.disableUnitTest()
.cache("data:group-rel:{}", "数据分组关联 ${groupId}")
.expire(1, TimeUnit.DAYS)
.vue("system", "data-group-rel")
.build(),
};
// jdbc 配置 - 使用配置文件

View File

@@ -13,6 +13,7 @@ import com.orion.ops.framework.common.utils.FileNames;
import com.orion.ops.framework.common.utils.Valid;
#if($meta.enableCache)
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
#end
#foreach($pkg in ${customModuleFilePackages})
import ${pkg}.*;
@@ -138,13 +139,12 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
// 查询数据库
list = ${typeLower}DAO.of().list(${type}Convert.MAPPER::toCache);
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, ${type}CacheDTO::new);
CacheBarriers.checkBarrier(list, ${type}CacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(${type}CacheKeyDefine.${typeConst}.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(${type}CacheKeyDefine.${typeConst});
RedisMaps.putAllJson(${type}CacheKeyDefine.${typeConst}, s -> s.getId().toString(), list);
}
// 删除屏障
RedisMaps.removeBarrier(list);
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(${type}Convert.MAPPER::to)

View File

@@ -76,7 +76,7 @@
#if($vue.enableCardView)
const appStore = useAppStore();
// FIXME 这里需要修改一下字段名称 同时 appStore 的类型和 AppPreferenceModel 都需要定义该字段类型
// FIXME 这里需要修改一下字段名称 并且在 appStore 定义该字段
const renderTable = computed(() => appStore.${vue.featureEntityFirstLower}View === 'table');
#end

View File

@@ -11,6 +11,8 @@ import java.util.stream.Collectors;
/**
* redis list 工具类
* <p>
* 写操作会自动设置过期时间 如果有
*
* @author Jiahang Li
* @version 1.0.0
@@ -82,6 +84,92 @@ public class RedisLists extends RedisUtils {
.collect(Collectors.toList());
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param <T> T
*/
public static <T> void push(CacheKeyDefine key, String value) {
push(key.getKey(), key, value, Function.identity());
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param <T> T
*/
public static <T> void push(String key, String value) {
push(key, null, value, Function.identity());
}
/**
* list 添加元素
*
* @param key key
* @param define define
* @param value value
* @param <T> T
*/
public static <T> void push(String key, CacheKeyDefine define, String value) {
push(key, define, value, Function.identity());
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param mapper mapper
* @param <T> T
*/
public static <T> void push(CacheKeyDefine key, T value, Function<T, String> mapper) {
push(key.getKey(), key, value, mapper);
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param mapper mapper
* @param <T> T
*/
public static <T> void push(String key, T value, Function<T, String> mapper) {
push(key, null, value, mapper);
redisTemplate.opsForList().rightPush(key, mapper.apply(value));
}
/**
* list 添加元素
*
* @param key key
* @param define define
* @param value value
* @param mapper mapper
* @param <T> T
*/
public static <T> void push(String key, CacheKeyDefine define, T value, Function<T, String> mapper) {
redisTemplate.opsForList().rightPush(key, mapper.apply(value));
if (define != null) {
setExpire(key, define);
}
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param <T> T
*/
public static <T> void pushJson(String key, T value) {
redisTemplate.opsForList().rightPush(key, JSON.toJSONString(value));
}
/**
* list 添加元素
*
@@ -129,45 +217,37 @@ public class RedisLists extends RedisUtils {
* @param list list
* @param <T> T
*/
public static <T> void pushAllJson(String key, List<T> list) {
List<String> values = list.stream()
.map(JSON::toJSONString)
.collect(Collectors.toList());
redisTemplate.opsForList().rightPushAll(key, values);
public static <T> void pushAllJson(CacheKeyDefine key, List<T> list) {
pushAllJson(key.getKey(), key, list);
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param <T> T
* @param key key
* @param list list
* @param <T> T
*/
public static <T> void push(String key, String value) {
redisTemplate.opsForList().rightPush(key, value);
public static <T> void pushAllJson(String key, List<T> list) {
pushAllJson(key, null, list);
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param mapper mapper
* @param define define
* @param list list
* @param <T> T
*/
public static <T> void push(String key, T value, Function<T, String> mapper) {
redisTemplate.opsForList().rightPush(key, mapper.apply(value));
}
/**
* list 添加元素
*
* @param key key
* @param value value
* @param <T> T
*/
public static <T> void pushJson(String key, T value) {
redisTemplate.opsForList().rightPush(key, JSON.toJSONString(value));
public static <T> void pushAllJson(String key, CacheKeyDefine define, List<T> list) {
List<String> values = list.stream()
.map(JSON::toJSONString)
.collect(Collectors.toList());
redisTemplate.opsForList().rightPushAll(key, values);
if (define != null) {
setExpire(key, define);
}
}
/**

View File

@@ -15,6 +15,8 @@ import java.util.stream.Collectors;
/**
* redis hash 工具类
* <p>
* 写操作会自动设置过期时间 如果有
*
* @author Jiahang Li
* @version 1.0.0
@@ -34,7 +36,7 @@ public class RedisMaps extends RedisUtils {
* @param value value
*/
public static void put(CacheKeyDefine key, Object hashKey, Object value) {
put(key.getKey(), hashKey, value);
put(key.getKey(), key, hashKey, value);
}
/**
@@ -45,7 +47,22 @@ public class RedisMaps extends RedisUtils {
* @param value value
*/
public static void put(String key, Object hashKey, Object value) {
put(key, null, hashKey, value);
}
/**
* 插入数据
*
* @param key key
* @param define define
* @param hashKey hashKey
* @param value value
*/
public static void put(String key, CacheKeyDefine define, Object hashKey, Object value) {
redisTemplate.opsForHash().put(key, Objects1.toString(hashKey), Objects1.toString(value));
if (define != null) {
setExpire(key, define);
}
}
/**
@@ -56,7 +73,7 @@ public class RedisMaps extends RedisUtils {
* @param value value
*/
public static void putJson(CacheKeyDefine key, Object hashKey, Object value) {
put(key.getKey(), hashKey, JSON.toJSONString(value));
put(key.getKey(), key, hashKey, JSON.toJSONString(value));
}
/**
@@ -67,7 +84,19 @@ public class RedisMaps extends RedisUtils {
* @param value value
*/
public static void putJson(String key, Object hashKey, Object value) {
put(key, hashKey, JSON.toJSONString(value));
put(key, null, hashKey, JSON.toJSONString(value));
}
/**
* 插入数据 json
*
* @param key key
* @param define define
* @param hashKey hashKey
* @param value value
*/
public static void putJson(String key, CacheKeyDefine define, Object hashKey, Object value) {
put(key, define, hashKey, JSON.toJSONString(value));
}
/**
@@ -79,7 +108,7 @@ public class RedisMaps extends RedisUtils {
* @param <V> V
*/
public static <V> void putJson(CacheKeyDefine key, Function<V, String> keyMapper, V value) {
put(key.getKey(), keyMapper.apply(value), JSON.toJSONString(value));
put(key.getKey(), key, keyMapper.apply(value), JSON.toJSONString(value));
}
/**
@@ -91,7 +120,20 @@ public class RedisMaps extends RedisUtils {
* @param <V> V
*/
public static <V> void putJson(String key, Function<V, String> keyMapper, V value) {
put(key, keyMapper.apply(value), JSON.toJSONString(value));
put(key, null, keyMapper.apply(value), JSON.toJSONString(value));
}
/**
* 插入数据 json
*
* @param key key
* @param define define
* @param keyMapper keyMapper
* @param value value
* @param <V> V
*/
public static <V> void putJson(String key, CacheKeyDefine define, Function<V, String> keyMapper, V value) {
put(key, define, keyMapper.apply(value), JSON.toJSONString(value));
}
/**
@@ -101,7 +143,7 @@ public class RedisMaps extends RedisUtils {
* @param values values
*/
public static void putAll(CacheKeyDefine key, Map<?, ?> values) {
putAll(key.getKey(), values);
putAll(key.getKey(), key, values);
}
/**
@@ -111,19 +153,22 @@ public class RedisMaps extends RedisUtils {
* @param values values
*/
public static void putAll(String key, Map<?, ?> values) {
Map<String, String> map = Maps.map(values, Objects1::toString, Objects1::toString);
redisTemplate.opsForHash().putAll(key, map);
putAll(key, null, values);
}
/**
* 插入数据 json
* 插入数据
*
* @param key key
* @param define define
* @param values values
* @param <V> V
*/
public static <V> void putAllJson(CacheKeyDefine key, Map<?, V> values) {
putAllJson(key.getKey(), values);
public static void putAll(String key, CacheKeyDefine define, Map<?, ?> values) {
Map<String, String> map = Maps.map(values, Objects1::toString, Objects1::toString);
redisTemplate.opsForHash().putAll(key, map);
if (define != null) {
setExpire(key, define);
}
}
/**
@@ -134,20 +179,34 @@ public class RedisMaps extends RedisUtils {
* @param <V> V
*/
public static <V> void putAllJson(String key, Map<?, V> values) {
Map<String, String> map = Maps.map(values, Objects1::toString, JSON::toJSONString);
redisTemplate.opsForHash().putAll(key, map);
putAllJson(key, null, values);
}
/**
* 插入数据 json
*
* @param key key
* @param keyMapper keyMapper
* @param values values
* @param <V> V
* @param key key
* @param values values
* @param <V> V
*/
public static <V> void putAllJson(CacheKeyDefine key, Function<V, String> keyMapper, List<V> values) {
putAllJson(key.getKey(), keyMapper, values);
public static <V> void putAllJson(CacheKeyDefine key, Map<?, V> values) {
putAllJson(key.getKey(), key, values);
}
/**
* 插入数据 json
*
* @param key key
* @param define define
* @param values values
* @param <V> V
*/
public static <V> void putAllJson(String key, CacheKeyDefine define, Map<?, V> values) {
Map<String, String> map = Maps.map(values, Objects1::toString, JSON::toJSONString);
redisTemplate.opsForHash().putAll(key, map);
if (define != null) {
setExpire(key, define);
}
}
/**
@@ -159,9 +218,38 @@ public class RedisMaps extends RedisUtils {
* @param <V> V
*/
public static <V> void putAllJson(String key, Function<V, String> keyMapper, List<V> values) {
putAllJson(key, null, keyMapper, values);
}
/**
* 插入数据 json
*
* @param key key
* @param keyMapper keyMapper
* @param values values
* @param <V> V
*/
public static <V> void putAllJson(CacheKeyDefine key, Function<V, String> keyMapper, List<V> values) {
putAllJson(key.getKey(), key, keyMapper, values);
}
/**
* 插入数据 json
*
* @param key key
* @param define define
* @param keyMapper keyMapper
* @param values values
* @param <V> V
*/
public static <V> void putAllJson(String key, CacheKeyDefine define,
Function<V, String> keyMapper, List<V> values) {
Map<String, String> map = values.stream()
.collect(Collectors.toMap(keyMapper, JSON::toJSONString, Functions.right()));
redisTemplate.opsForHash().putAll(key, map);
if (define != null) {
setExpire(key, define);
}
}
/**

View File

@@ -14,6 +14,8 @@ import java.util.stream.Collectors;
/**
* redis string 工具类
* <p>
* 写操作会自动设置过期时间 如果有
*
* @author Jiahang Li
* @version 1.0.0
@@ -224,7 +226,17 @@ public class RedisStrings extends RedisUtils {
}
/**
* 设置 json
* 设置
*
* @param define define
* @param value value
*/
public static void set(CacheKeyDefine define, Object value) {
set(define.getKey(), define, value);
}
/**
* 设置值
*
* @param key key
* @param define define
@@ -234,7 +246,7 @@ public class RedisStrings extends RedisUtils {
if (value == null) {
value = Strings.EMPTY;
}
if (define.getTimeout() == 0) {
if (define == null || define.getTimeout() == 0) {
// 不过期
redisTemplate.opsForValue().set(key, value.toString());
} else {
@@ -245,6 +257,16 @@ public class RedisStrings extends RedisUtils {
}
}
/**
* 设置 json
*
* @param define define
* @param value value
*/
public static void setJson(CacheKeyDefine define, Object value) {
setJson(define.getKey(), define, value);
}
/**
* 设置 json
*
@@ -253,7 +275,7 @@ public class RedisStrings extends RedisUtils {
* @param value value
*/
public static void setJson(String key, CacheKeyDefine define, Object value) {
if (define.getTimeout() == 0) {
if (define == null || define.getTimeout() == 0) {
// 不过期
redisTemplate.opsForValue().set(key, JSON.toJSONString(value));
} else {

View File

@@ -20,7 +20,7 @@ import java.util.Set;
* @version 1.0.0
* @since 2021/11/6 11:09
*/
public class RedisUtils extends CacheUtils {
public class RedisUtils {
protected static RedisTemplate<String, String> redisTemplate;
@@ -112,7 +112,7 @@ public class RedisUtils extends CacheUtils {
* @param define define
*/
public static void setExpire(String key, CacheKeyDefine define) {
if (define.getTimeout() != 0) {
if (define != null && define.getTimeout() != 0) {
// 设置过期时间
redisTemplate.expire(key, define.getTimeout(), define.getUnit());
}

View File

@@ -1,25 +1,26 @@
package com.orion.ops.framework.redis.core.utils;
package com.orion.ops.framework.redis.core.utils.barrier;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.constant.Const;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;
/**
* 缓存工具类
* 缓存屏障工具类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/11/15 1:22
*/
public class CacheUtils {
public class CacheBarriers {
protected CacheUtils() {
private CacheBarriers() {
}
public static final GenericsListBarrier<Long> LONG = GenericsListBarrier.create(Const.NONE_ID);
/**
* 创建屏障对象 防止穿透
*
@@ -33,15 +34,14 @@ public class CacheUtils {
return val;
}
/**
* 检测是否需要 创建屏障对象 防止穿透
* 检测是否需要添加屏障对象 防止穿透
*
* @param list list
* @param supplier supplier
* @param <T> T
*/
public static <T extends LongCacheIdModel> void checkBarrier(List<T> list, Supplier<T> supplier) {
public static <T extends LongCacheIdModel> void checkBarrier(Collection<T> list, Supplier<T> supplier) {
if (list != null && list.isEmpty()) {
// 添加屏障对象
list.add(createBarrier(supplier));

View File

@@ -0,0 +1,57 @@
package com.orion.ops.framework.redis.core.utils.barrier;
import com.orion.lang.utils.collect.Lists;
import java.util.Collection;
/**
* 标准集合屏障
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/11/21 11:46
*/
public class GenericsListBarrier<T> {
private final T barrierValue;
public GenericsListBarrier(T barrierValue) {
this.barrierValue = barrierValue;
}
/**
* 创建屏障
*
* @param barrierValue barrierValue
* @param <T> T
* @return barrier
*/
public static <T> GenericsListBarrier<T> create(T barrierValue) {
return new GenericsListBarrier<>(barrierValue);
}
/**
* 检测是否需要添加屏障对象 防止穿透
*
* @param list list
*/
public void check(Collection<T> list) {
if (list != null && list.isEmpty()) {
// 添加屏障对象
list.add(barrierValue);
}
}
/**
* 移除屏障对象
*
* @param list list
*/
public void remove(Collection<T> list) {
if (!Lists.isEmpty(list)) {
list.removeIf(s -> s.equals(barrierValue));
}
}
}

View File

@@ -45,8 +45,6 @@ public class HostController {
@Resource
private HostConfigService hostConfigService;
// fixme host_config 设置为缓存
@OperatorLog(HostOperatorType.CREATE)
@PostMapping("/create")
@Operation(summary = "创建主机")

View File

@@ -6,12 +6,12 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogs;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.security.PasswordModifier;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.module.asset.convert.HostIdentityConvert;
import com.orion.ops.module.asset.dao.HostConfigDAO;
import com.orion.ops.module.asset.dao.HostIdentityDAO;
@@ -118,13 +118,12 @@ public class HostIdentityServiceImpl implements HostIdentityService {
// 查询数据库
list = hostIdentityDAO.of().list(HostIdentityConvert.MAPPER::toCache);
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, HostIdentityCacheDTO::new);
CacheBarriers.checkBarrier(list, HostIdentityCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_IDENTITY.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(HostCacheKeyDefine.HOST_IDENTITY);
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_IDENTITY, s -> s.getId().toString(), list);
}
// 删除屏障
RedisMaps.removeBarrier(list);
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(HostIdentityConvert.MAPPER::to)

View File

@@ -4,13 +4,13 @@ import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogs;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.security.PasswordModifier;
import com.orion.ops.framework.common.utils.CryptoUtils;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.module.asset.convert.HostKeyConvert;
import com.orion.ops.module.asset.dao.HostConfigDAO;
import com.orion.ops.module.asset.dao.HostIdentityDAO;
@@ -132,13 +132,12 @@ public class HostKeyServiceImpl implements HostKeyService {
// 查询数据库
list = hostKeyDAO.of().list(HostKeyConvert.MAPPER::toCache);
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, HostKeyCacheDTO::new);
CacheBarriers.checkBarrier(list, HostKeyCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_KEY.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(HostCacheKeyDefine.HOST_KEY);
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_KEY, s -> s.getId().toString(), list);
}
// 删除屏障
RedisMaps.removeBarrier(list);
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(HostKeyConvert.MAPPER::to)

View File

@@ -10,6 +10,7 @@ import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogs;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.module.asset.convert.HostConvert;
import com.orion.ops.module.asset.dao.HostConfigDAO;
import com.orion.ops.module.asset.dao.HostDAO;
@@ -155,13 +156,12 @@ public class HostServiceImpl implements HostService {
// 查询数据库
list = hostDAO.of().list(HostConvert.MAPPER::toCache);
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, HostCacheDTO::new);
CacheBarriers.checkBarrier(list, HostCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_INFO.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(HostCacheKeyDefine.HOST_INFO);
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_INFO, s -> s.getId().toString(), list);
}
// 删除屏障
RedisMaps.removeBarrier(list);
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(HostConvert.MAPPER::to)

View File

@@ -40,13 +40,4 @@ public class AppPreferenceModel implements PreferenceModel {
@Schema(description = "菜单宽度")
private Number menuWidth;
@Schema(description = "主机视图")
private String hostView;
@Schema(description = "主机秘钥视图")
private String hostKeyView;
@Schema(description = "主机身份视图")
private String hostIdentityView;
}

View File

@@ -13,10 +13,6 @@ import org.springframework.stereotype.Component;
@Component
public class SystemPreferenceStrategy implements IPreferenceStrategy<AppPreferenceModel> {
private static final String TABLE = "table";
private static final String CARD = "card";
@Override
public AppPreferenceModel getDefault() {
return AppPreferenceModel.builder()
@@ -27,9 +23,6 @@ public class SystemPreferenceStrategy implements IPreferenceStrategy<AppPreferen
.tabBar(true)
.menuWidth(220)
.colorWeak(false)
.hostView(TABLE)
.hostKeyView(CARD)
.hostIdentityView(CARD)
.build();
}

View File

@@ -3,12 +3,12 @@ package com.orion.ops.module.infra.service.impl;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogs;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisLists;
import com.orion.ops.framework.redis.core.utils.RedisStrings;
import com.orion.ops.framework.redis.core.utils.RedisUtils;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.module.infra.convert.DataGroupRelConvert;
import com.orion.ops.module.infra.dao.DataGroupDAO;
import com.orion.ops.module.infra.dao.DataGroupRelDAO;
@@ -189,12 +189,12 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
.then()
.list(DataGroupRelConvert.MAPPER::toCache);
// 设置屏障 防止穿透
RedisStrings.checkBarrier(list, DataGroupRelCacheDTO::new);
CacheBarriers.checkBarrier(list, DataGroupRelCacheDTO::new);
// 设置缓存
RedisStrings.setJson(key, DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE, list);
}
// 删除屏障
RedisStrings.removeBarrier(list);
CacheBarriers.removeBarrier(list);
return list;
}
@@ -213,15 +213,13 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
.stream()
.map(DataGroupRelDO::getRelId)
.collect(Collectors.toList());
// 添加默认值 防止穿透
if (list.isEmpty()) {
list.add(Const.NONE_ID);
}
// 设置屏障 防止穿透
CacheBarriers.LONG.check(list);
// 设置缓存
RedisLists.pushAll(key, DataGroupCacheKeyDefine.DATA_GROUP_REL_GROUP, list, Object::toString);
}
// 删除默认值
list.remove(Const.NONE_ID);
// 删除屏障
CacheBarriers.LONG.remove(list);
return list;
}

View File

@@ -9,6 +9,7 @@ import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.enums.MovePosition;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisStrings;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.module.infra.convert.DataGroupConvert;
import com.orion.ops.module.infra.dao.DataGroupDAO;
import com.orion.ops.module.infra.define.cache.DataGroupCacheKeyDefine;
@@ -159,12 +160,12 @@ public class DataGroupServiceImpl implements DataGroupService {
.then()
.list(DataGroupConvert.MAPPER::toCache);
// 设置屏障 防止穿透
RedisStrings.checkBarrier(list, DataGroupCacheDTO::new);
CacheBarriers.checkBarrier(list, DataGroupCacheDTO::new);
// 设置缓存
RedisStrings.setJson(key, DataGroupCacheKeyDefine.DATA_GROUP_LIST, list);
}
// 删除屏障
RedisStrings.removeBarrier(list);
CacheBarriers.removeBarrier(list);
return list;
}
@@ -177,7 +178,7 @@ public class DataGroupServiceImpl implements DataGroupService {
// 查询列表缓存
List<DataGroupCacheDTO> rows = this.getDataGroupListByCache(type);
// 设置屏障 防止穿透
RedisStrings.checkBarrier(rows, DataGroupCacheDTO::new);
CacheBarriers.checkBarrier(rows, DataGroupCacheDTO::new);
if (!Lists.isEmpty(rows)) {
// 构建树
DataGroupCacheDTO rootNode = DataGroupCacheDTO.builder()
@@ -191,7 +192,7 @@ public class DataGroupServiceImpl implements DataGroupService {
RedisStrings.setJson(key, DataGroupCacheKeyDefine.DATA_GROUP_LIST, treeData);
}
// 删除屏障
RedisStrings.removeBarrier(treeData);
CacheBarriers.removeBarrier(treeData);
return treeData;
}

View File

@@ -15,6 +15,7 @@ import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.redis.core.utils.RedisStrings;
import com.orion.ops.framework.redis.core.utils.RedisUtils;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.module.infra.convert.DictKeyConvert;
import com.orion.ops.module.infra.dao.DictKeyDAO;
import com.orion.ops.module.infra.define.cache.DictCacheKeyDefine;
@@ -107,13 +108,12 @@ public class DictKeyServiceImpl implements DictKeyService {
// 查询数据库
list = dictKeyDAO.of().list(DictKeyConvert.MAPPER::toCache);
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, DictKeyCacheDTO::new);
CacheBarriers.checkBarrier(list, DictKeyCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(DictCacheKeyDefine.DICT_KEY.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(DictCacheKeyDefine.DICT_KEY);
RedisMaps.putAllJson(DictCacheKeyDefine.DICT_KEY, s -> s.getId().toString(), list);
}
// 删除屏障
RedisMaps.removeBarrier(list);
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(DictKeyConvert.MAPPER::to)

View File

@@ -2,9 +2,9 @@ package com.orion.ops.module.infra.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisLists;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.ops.module.infra.convert.FavoriteConvert;
import com.orion.ops.module.infra.dao.FavoriteDAO;
@@ -86,26 +86,24 @@ public class FavoriteServiceImpl implements FavoriteService {
Long userId = request.getUserId();
String cacheKey = FavoriteCacheKeyDefine.FAVORITE.format(type, userId);
// 获取缓存
List<Long> cacheRelIdList = RedisLists.range(cacheKey, Long::valueOf);
if (cacheRelIdList.isEmpty()) {
List<Long> list = RedisLists.range(cacheKey, Long::valueOf);
if (list.isEmpty()) {
// 条件
LambdaQueryWrapper<FavoriteDO> wrapper = this.buildQueryWrapper(request);
// 查询数据库
cacheRelIdList = favoriteDAO.of(wrapper)
list = favoriteDAO.of(wrapper)
.stream()
.map(FavoriteDO::getRelId)
.distinct()
.collect(Collectors.toList());
// 添加默认值 防止穿透
if (cacheRelIdList.isEmpty()) {
cacheRelIdList.add(Const.NONE_ID);
}
// 设置屏障 防止穿透
CacheBarriers.LONG.check(list);
// 设置缓存
RedisLists.pushAll(cacheKey, FavoriteCacheKeyDefine.FAVORITE, cacheRelIdList, String::valueOf);
RedisLists.pushAll(cacheKey, FavoriteCacheKeyDefine.FAVORITE, list, String::valueOf);
}
// 删除默认值
cacheRelIdList.remove(Const.NONE_ID);
return cacheRelIdList;
// 删除屏障
CacheBarriers.LONG.remove(list);
return list;
}
@Override

View File

@@ -14,6 +14,7 @@ import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.redis.core.utils.RedisStrings;
import com.orion.ops.framework.redis.core.utils.RedisUtils;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.ops.module.infra.convert.SystemUserConvert;
import com.orion.ops.module.infra.dao.OperatorLogDAO;
@@ -164,13 +165,12 @@ public class SystemUserServiceImpl implements SystemUserService {
// 查询数据库
list = systemUserDAO.of().list(SystemUserConvert.MAPPER::toUserInfo);
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, UserInfoDTO::new);
CacheBarriers.checkBarrier(list, UserInfoDTO::new);
// 设置缓存
RedisMaps.putAllJson(UserCacheKeyDefine.USER_LIST.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(UserCacheKeyDefine.USER_LIST);
RedisMaps.putAllJson(UserCacheKeyDefine.USER_LIST, s -> s.getId().toString(), list);
}
// 删除屏障
RedisMaps.removeBarrier(list);
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(SystemUserConvert.MAPPER::to)

View File

@@ -2,10 +2,10 @@ package com.orion.ops.module.infra.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.mybatis.core.query.Conditions;
import com.orion.ops.framework.redis.core.utils.RedisLists;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.module.infra.convert.TagConvert;
import com.orion.ops.module.infra.dao.TagDAO;
import com.orion.ops.module.infra.define.cache.TagCacheKeyDefine;
@@ -69,13 +69,12 @@ public class TagServiceImpl implements TagService {
LambdaQueryWrapper<TagDO> wrapper = Conditions.eq(TagDO::getType, type);
list = tagDAO.of(wrapper).list(TagConvert.MAPPER::toCache);
// 设置屏障 防止穿透
RedisLists.checkBarrier(list, TagCacheDTO::new);
CacheBarriers.checkBarrier(list, TagCacheDTO::new);
// 设置到缓存
RedisLists.pushAllJson(cacheKey, list);
RedisLists.setExpire(cacheKey, TagCacheKeyDefine.TAG_NAME);
RedisLists.pushAllJson(cacheKey, TagCacheKeyDefine.TAG_NAME, list);
}
// 删除屏障
RedisLists.removeBarrier(list);
CacheBarriers.removeBarrier(list);
// 转换
return list.stream()
.map(TagConvert.MAPPER::to)

View File

@@ -23,8 +23,7 @@ public class TipsServiceImpl implements TipsService {
public void tipped(String tippedKey) {
Long userId = SecurityUtils.getLoginUserId();
String key = TipsCacheKeyDefine.TIPS.format(userId);
RedisLists.push(key, tippedKey);
RedisLists.setExpire(key, TipsCacheKeyDefine.TIPS);
RedisLists.push(key, TipsCacheKeyDefine.TIPS, tippedKey);
}
@Override