diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisLists.java b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisLists.java index f78de625..c2279f54 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisLists.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-redis/src/main/java/com/orion/ops/framework/redis/core/utils/RedisLists.java @@ -82,6 +82,18 @@ public class RedisLists extends RedisUtils { .collect(Collectors.toList()); } + /** + * list 添加元素 + * + * @param define define + * @param list list + * @param mapper mapper + * @param T + */ + public static void pushAll(CacheKeyDefine define, List list, Function mapper) { + pushAll(define.getKey(), define, list, mapper); + } + /** * list 添加元素 * @@ -91,7 +103,23 @@ public class RedisLists extends RedisUtils { * @param T */ public static void pushAll(String key, List list, Function mapper) { + pushAll(key, null, list, mapper); + } + + /** + * list 添加元素 + * + * @param key key + * @param define define + * @param list list + * @param mapper mapper + * @param T + */ + public static void pushAll(String key, CacheKeyDefine define, List list, Function mapper) { redisTemplate.opsForList().rightPushAll(key, Lists.map(list, mapper)); + if (define != null) { + setExpire(key, define); + } } /** diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/api/impl/DataGroupRelApiImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/api/impl/DataGroupRelApiImpl.java index 4c8a6311..cbc15c63 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/api/impl/DataGroupRelApiImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/api/impl/DataGroupRelApiImpl.java @@ -14,6 +14,7 @@ import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -73,10 +74,8 @@ public class DataGroupRelApiImpl implements DataGroupRelApi { @Override public Set getGroupRelIdByGroupId(DataGroupTypeEnum type, Long groupId) { - List rows = dataGroupRelService.getGroupRelListByCache(type.name(), groupId); - return rows.stream() - .map(DataGroupRelCacheDTO::getRelId) - .collect(Collectors.toSet()); + List rows = dataGroupRelService.getGroupRelIdListByCache(type.name(), groupId); + return new HashSet<>(rows); } @Override diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/cache/DataGroupCacheKeyDefine.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/cache/DataGroupCacheKeyDefine.java index 3632bb26..27905062 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/cache/DataGroupCacheKeyDefine.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/define/cache/DataGroupCacheKeyDefine.java @@ -36,8 +36,8 @@ public interface DataGroupCacheKeyDefine { CacheKeyDefine DATA_GROUP_REL_GROUP = new CacheKeyBuilder() .key("data:group-rel:group:{}") .desc("数据分组数据关联-分组 ${groupId}") - .type(DataGroupRelCacheDTO.class) - .struct(RedisCacheStruct.STRING) + .type(Long.class) + .struct(RedisCacheStruct.LIST) .timeout(1, TimeUnit.DAYS) .build(); diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupRelService.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupRelService.java index 58ea0907..f8b6c7a4 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupRelService.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/DataGroupRelService.java @@ -62,7 +62,7 @@ public interface DataGroupRelService { * @param groupId groupId * @return rows */ - List getGroupRelListByCache(String type, Long groupId); + List getGroupRelIdListByCache(String type, Long groupId); /** * 通过 relId 查询 groupRel diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupRelServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupRelServiceImpl.java index 0458c35a..530d8b72 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupRelServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupRelServiceImpl.java @@ -1,12 +1,14 @@ package com.orion.ops.module.infra.service.impl; -import com.orion.lang.define.cache.key.CacheKeyDefine; 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.module.infra.convert.DataGroupRelConvert; import com.orion.ops.module.infra.dao.DataGroupDAO; import com.orion.ops.module.infra.dao.DataGroupRelDAO; @@ -26,7 +28,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.function.Supplier; import java.util.stream.Collectors; /** @@ -177,29 +178,51 @@ public class DataGroupRelServiceImpl implements DataGroupRelService { @Override public List getGroupRelListByCache(String type) { - return this.getGroupRelListByCache( - DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE.format(type), - DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE, - () -> dataGroupRelDAO.of() - .createWrapper() - .eq(DataGroupRelDO::getType, type) - .then() - .list(DataGroupRelConvert.MAPPER::toCache) - ); + String key = DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE.format(type); + // 查询缓存 + List list = RedisStrings.getJsonArray(key, DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE); + if (Lists.isEmpty(list)) { + // 查询数据库 + list = dataGroupRelDAO.of() + .createWrapper() + .eq(DataGroupRelDO::getType, type) + .then() + .list(DataGroupRelConvert.MAPPER::toCache); + // 设置屏障 防止穿透 + RedisStrings.checkBarrier(list, DataGroupRelCacheDTO::new); + // 设置缓存 + RedisStrings.setJson(key, DataGroupCacheKeyDefine.DATA_GROUP_REL_TYPE, list); + } + // 删除屏障 + RedisStrings.removeBarrier(list); + return list; } @Override - public List getGroupRelListByCache(String type, Long groupId) { - return this.getGroupRelListByCache( - DataGroupCacheKeyDefine.DATA_GROUP_REL_GROUP.format(groupId), - DataGroupCacheKeyDefine.DATA_GROUP_REL_GROUP, - () -> dataGroupRelDAO.of() - .createWrapper() - .eq(DataGroupRelDO::getType, type) - .eq(DataGroupRelDO::getGroupId, groupId) - .then() - .list(DataGroupRelConvert.MAPPER::toCache) - ); + public List getGroupRelIdListByCache(String type, Long groupId) { + String key = DataGroupCacheKeyDefine.DATA_GROUP_REL_GROUP.format(groupId); + // 查询缓存 + List list = RedisLists.range(key, Long::valueOf); + if (Lists.isEmpty(list)) { + // 查询数据库 + list = dataGroupRelDAO.of() + .createWrapper() + .eq(DataGroupRelDO::getType, type) + .eq(DataGroupRelDO::getGroupId, groupId) + .then() + .stream() + .map(DataGroupRelDO::getRelId) + .collect(Collectors.toList()); + // 添加默认值 防止穿透 + if (list.isEmpty()) { + list.add(Const.NONE_ID); + } + // 设置缓存 + RedisLists.pushAll(key, DataGroupCacheKeyDefine.DATA_GROUP_REL_GROUP, list, Object::toString); + } + // 删除默认值 + list.remove(Const.NONE_ID); + return list; } @Override @@ -212,32 +235,6 @@ public class DataGroupRelServiceImpl implements DataGroupRelService { .list(); } - /** - * 查询分组引用缓存 - * - * @param key key - * @param define define - * @param valueSupplier valueSupplier - * @return values - */ - public List getGroupRelListByCache(String key, - CacheKeyDefine define, - Supplier> valueSupplier) { - // 查询缓存 - List list = RedisStrings.getJsonArray(key, define); - if (Lists.isEmpty(list)) { - // 查询数据库 - list = valueSupplier.get(); - // 设置屏障 防止穿透 - RedisStrings.checkBarrier(list, DataGroupRelCacheDTO::new); - // 设置缓存 - RedisStrings.setJson(key, define, list); - } - // 删除屏障 - RedisStrings.removeBarrier(list); - return list; - } - @Override @Transactional(rollbackFor = Exception.class) public Integer deleteByRelId(String type, Long relId) { @@ -303,7 +300,7 @@ public class DataGroupRelServiceImpl implements DataGroupRelService { .forEach(keyList::add); } // 删除 - RedisStrings.delete(keyList); + RedisUtils.delete(keyList); } } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupServiceImpl.java index de26036d..d61c8c36 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/DataGroupServiceImpl.java @@ -60,8 +60,7 @@ public class DataGroupServiceImpl implements DataGroupService { Long id = record.getId(); log.info("DataGroupService-createDataGroup id: {}, effect: {}", id, effect); // 删除缓存 - RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(request.getType()), - DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(request.getType())); + this.deleteCache(request.getType()); return id; } @@ -85,8 +84,7 @@ public class DataGroupServiceImpl implements DataGroupService { // 更新 int effect = dataGroupDAO.updateById(updateRecord); // 删除缓存 - RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(record.getType()), - DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(record.getType())); + this.deleteCache(record.getType()); return effect; } @@ -140,8 +138,7 @@ public class DataGroupServiceImpl implements DataGroupService { effect = dataGroupDAO.updateById(update); } // 删除缓存 - RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type), - DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type)); + this.deleteCache(type); // 添加日志参数 OperatorLogs.add(OperatorLogs.SOURCE, moveRecord.getName()); OperatorLogs.add(OperatorLogs.TARGET, targetRecord.getName()); @@ -238,8 +235,7 @@ public class DataGroupServiceImpl implements DataGroupService { dataGroupRelService.deleteByGroupIdList(type, deleteIdList); log.info("DataGroupService-deleteDataGroupById id: {}, effect: {}", id, effect); // 删除缓存 - RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type), - DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type)); + this.deleteCache(type); // 添加日志参数 OperatorLogs.add(OperatorLogs.GROUP_NAME, record.getName()); return effect; @@ -285,4 +281,14 @@ public class DataGroupServiceImpl implements DataGroupService { Valid.isFalse(present, ErrorMessage.DATA_PRESENT); } + /** + * 删除缓存 + * + * @param type type + */ + private void deleteCache(String type) { + RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type), + DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type)); + } + } diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/FavoriteServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/FavoriteServiceImpl.java index 944ecd96..a4dc2fee 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/FavoriteServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/FavoriteServiceImpl.java @@ -101,11 +101,9 @@ public class FavoriteServiceImpl implements FavoriteService { cacheRelIdList.add(Const.NONE_ID); } // 设置缓存 - RedisLists.pushAll(cacheKey, cacheRelIdList, String::valueOf); - // 设置过期时间 - RedisLists.setExpire(cacheKey, FavoriteCacheKeyDefine.FAVORITE); + RedisLists.pushAll(cacheKey, FavoriteCacheKeyDefine.FAVORITE, cacheRelIdList, String::valueOf); } - // 删除防止穿透的 key + // 删除默认值 cacheRelIdList.remove(Const.NONE_ID); return cacheRelIdList; } diff --git a/orion-ops-ui/src/api/asset/host-group.ts b/orion-ops-ui/src/api/asset/host-group.ts index 215cc6b9..04003ff0 100644 --- a/orion-ops-ui/src/api/asset/host-group.ts +++ b/orion-ops-ui/src/api/asset/host-group.ts @@ -39,7 +39,7 @@ export interface HostGroupQueryResponse { */ export interface HostGroupRelUpdateRequest { groupId?: number; - relIdList?: Array; + hostIdList?: Array; } /** diff --git a/orion-ops-ui/src/views/asset/host-group/components/host-transfer.vue b/orion-ops-ui/src/views/asset/host-group/components/host-transfer.vue index e32357b5..ee3f2da5 100644 --- a/orion-ops-ui/src/views/asset/host-group/components/host-transfer.vue +++ b/orion-ops-ui/src/views/asset/host-group/components/host-transfer.vue @@ -110,7 +110,7 @@ emits('loading', true); await updateHostGroupRel({ groupId: props.group?.key as number, - relIdList: value.value + hostIdList: value.value }); Message.success('保存成功'); } catch (e) {