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