重构代码.

This commit is contained in:
lijiahangmax
2023-11-21 01:11:44 +08:00
parent 8fe105fc8a
commit 69c71055d9
9 changed files with 98 additions and 70 deletions

View File

@@ -82,6 +82,18 @@ public class RedisLists extends RedisUtils {
.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 添加元素
*
@@ -91,7 +103,23 @@ public class RedisLists extends RedisUtils {
* @param <T> T
*/
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));
if (define != null) {
setExpire(key, define);
}
}
/**

View File

@@ -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<Long> getGroupRelIdByGroupId(DataGroupTypeEnum type, Long groupId) {
List<DataGroupRelCacheDTO> rows = dataGroupRelService.getGroupRelListByCache(type.name(), groupId);
return rows.stream()
.map(DataGroupRelCacheDTO::getRelId)
.collect(Collectors.toSet());
List<Long> rows = dataGroupRelService.getGroupRelIdListByCache(type.name(), groupId);
return new HashSet<>(rows);
}
@Override

View File

@@ -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();

View File

@@ -62,7 +62,7 @@ public interface DataGroupRelService {
* @param groupId groupId
* @return rows
*/
List<DataGroupRelCacheDTO> getGroupRelListByCache(String type, Long groupId);
List<Long> getGroupRelIdListByCache(String type, Long groupId);
/**
* 通过 relId 查询 groupRel

View File

@@ -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<DataGroupRelCacheDTO> 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<DataGroupRelCacheDTO> 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<DataGroupRelCacheDTO> 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<Long> getGroupRelIdListByCache(String type, Long groupId) {
String key = DataGroupCacheKeyDefine.DATA_GROUP_REL_GROUP.format(groupId);
// 查询缓存
List<Long> 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<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
@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);
}
}

View File

@@ -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));
}
}

View File

@@ -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;
}

View File

@@ -39,7 +39,7 @@ export interface HostGroupQueryResponse {
*/
export interface HostGroupRelUpdateRequest {
groupId?: number;
relIdList?: Array<string>;
hostIdList?: Array<string>;
}
/**

View File

@@ -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) {