refactor: 重构缓存逻辑.

This commit is contained in:
lijiahangmax
2023-11-15 02:04:55 +08:00
parent 34ea140004
commit 30f0496328
38 changed files with 235 additions and 169 deletions

View File

@@ -1,5 +1,6 @@
package ${currentPackage};
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serializable;
import lombok.*;
@@ -19,7 +20,7 @@ import java.math.*;
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "${type}CacheDTO", description = "$!{table.comment} 缓存对象")
public class ${type}CacheDTO implements Serializable {
public class ${type}CacheDTO implements LongCacheIdModel, Serializable {
private static final long serialVersionUID = 1L;
#foreach($field in ${table.fields})

View File

@@ -1,7 +1,8 @@
package ${currentPackage};
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
#foreach($pkg in ${customModuleFilePackages})
import ${pkg}.*;
#end
@@ -21,6 +22,7 @@ public interface ${type}CacheKeyDefine {
.key("$meta.cacheKey")
.desc("$meta.cacheDesc")
.type(${type}CacheDTO.class)
.struct(RedisCacheStruct.HASH)
#if($meta.cacheExpired)
.timeout($meta.cacheExpireTime, TimeUnit.$meta.cacheExpireUnit.name())
#end

View File

@@ -8,9 +8,6 @@ import com.orion.lang.utils.collect.Lists;
#if($meta.enableExport)
import com.orion.office.excel.writer.exporting.ExcelExport;
#end
#if($meta.enableCache)
import com.orion.ops.framework.common.constant.Const;
#end
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.FileNames;
import com.orion.ops.framework.common.utils.Valid;
@@ -32,7 +29,6 @@ import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
/**
* $!{table.comment} 服务实现类
@@ -140,21 +136,16 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
if (list.isEmpty()) {
// 查询数据库
list = ${typeLower}DAO.of().list(${type}Convert.MAPPER::toCache);
// 添加默认值 防止穿透
if (list.isEmpty()) {
list.add(${type}CacheDTO.builder()
.id(Const.NONE_ID)
.build());
}
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, ${type}CacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(${type}CacheKeyDefine.${typeConst}.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(${type}CacheKeyDefine.${typeConst});
}
// 删除默认值
return list.stream()
.filter(s -> !s.getId().equals(Const.NONE_ID))
.map(${type}Convert.MAPPER::to)
.collect(Collectors.toList());
// 删除屏障
RedisMaps.removeBarrier(list);
// 转换
return Lists.map(list, ${type}Convert.MAPPER::to);
}
#end

View File

@@ -0,0 +1,64 @@
package com.orion.ops.framework.redis.core.utils;
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 {
protected CacheUtils() {
}
/**
* 创建屏障对象 防止穿透
*
* @param supplier supplier
* @param <T> T
* @return barrier
*/
public static <T extends LongCacheIdModel> T createBarrier(Supplier<T> supplier) {
T val = supplier.get();
val.setId(Const.NONE_ID);
return val;
}
/**
* 检测是否需要 创建屏障对象 防止穿透
*
* @param list list
* @param supplier supplier
* @param <T> T
*/
public static <T extends LongCacheIdModel> void checkBarrier(List<T> list, Supplier<T> supplier) {
if (list != null && list.isEmpty()) {
// 添加屏障对象
list.add(createBarrier(supplier));
}
}
/**
* 移除屏障对象
*
* @param list list
* @param <T> T
*/
public static <T extends LongCacheIdModel> void removeBarrier(Collection<T> list) {
if (!Lists.isEmpty(list)) {
list.removeIf(s -> Const.NONE_ID.equals(s.getId()));
}
}
}

View File

@@ -1,7 +1,7 @@
package com.orion.ops.framework.redis.core.utils;
import com.alibaba.fastjson.JSON;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.constant.Const;

View File

@@ -1,7 +1,7 @@
package com.orion.ops.framework.redis.core.utils;
import com.alibaba.fastjson.JSON;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.function.Functions;
import com.orion.lang.utils.Objects1;
import com.orion.lang.utils.collect.Lists;

View File

@@ -3,7 +3,7 @@ package com.orion.ops.framework.redis.core.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.utils.Strings;
import java.util.ArrayList;

View File

@@ -1,6 +1,6 @@
package com.orion.ops.framework.redis.core.utils;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.utils.Arrays1;
import com.orion.lang.utils.io.Streams;
import org.springframework.data.redis.core.Cursor;
@@ -20,7 +20,7 @@ import java.util.Set;
* @version 1.0.0
* @since 2021/11/6 11:09
*/
public class RedisUtils {
public class RedisUtils extends CacheUtils {
protected static RedisTemplate<String, String> redisTemplate;

View File

@@ -1,7 +1,8 @@
package com.orion.ops.module.asset.define.cache;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import com.orion.ops.module.asset.entity.dto.HostCacheDTO;
import com.orion.ops.module.asset.entity.dto.HostIdentityCacheDTO;
import com.orion.ops.module.asset.entity.dto.HostKeyCacheDTO;
@@ -21,6 +22,7 @@ public interface HostCacheKeyDefine {
.key("host:info:list")
.desc("主机列表")
.type(HostCacheDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(1, TimeUnit.DAYS)
.build();
@@ -28,6 +30,7 @@ public interface HostCacheKeyDefine {
.key("host:key:list")
.desc("主机秘钥列表")
.type(HostKeyCacheDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(1, TimeUnit.DAYS)
.build();
@@ -35,6 +38,7 @@ public interface HostCacheKeyDefine {
.key("host:identity:list")
.desc("主机身份列表")
.type(HostIdentityCacheDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(1, TimeUnit.DAYS)
.build();

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.asset.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -20,7 +21,7 @@ import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "HostCacheDTO", description = "主机 缓存对象")
public class HostCacheDTO implements Serializable {
public class HostCacheDTO implements LongCacheIdModel, Serializable {
@Schema(description = "id")
private Long id;

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.asset.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -20,7 +21,7 @@ import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "HostIdentityCacheDTO", description = "主机身份缓存")
public class HostIdentityCacheDTO implements Serializable {
public class HostIdentityCacheDTO implements LongCacheIdModel, Serializable {
@Schema(description = "id")
private Long id;

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.asset.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -20,7 +21,7 @@ import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "HostKeyCacheDTO", description = "主机秘钥缓存")
public class HostKeyCacheDTO implements Serializable {
public class HostKeyCacheDTO implements LongCacheIdModel, Serializable {
@Schema(description = "id")
private Long id;

View File

@@ -64,4 +64,11 @@ public interface HostService {
*/
Integer deleteHostById(Long id);
/**
* 通过 id 删除主机引用
*
* @param id id
*/
void deleteHostRelByIdAsync(Long id);
}

View File

@@ -6,8 +6,8 @@ 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.Const;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.security.PasswordModifier;
import com.orion.ops.framework.common.utils.Valid;
@@ -117,21 +117,16 @@ public class HostIdentityServiceImpl implements HostIdentityService {
if (list.isEmpty()) {
// 查询数据库
list = hostIdentityDAO.of().list(HostIdentityConvert.MAPPER::toCache);
// 添加默认值 防止穿透
if (list.isEmpty()) {
list.add(HostIdentityCacheDTO.builder()
.id(Const.NONE_ID)
.build());
}
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, HostIdentityCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_IDENTITY.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(HostCacheKeyDefine.HOST_IDENTITY);
}
// 删除默认值
return list.stream()
.filter(s -> !s.getId().equals(Const.NONE_ID))
.map(HostIdentityConvert.MAPPER::to)
.collect(Collectors.toList());
// 删除屏障
RedisMaps.removeBarrier(list);
// 转换
return Lists.map(list, HostIdentityConvert.MAPPER::to);
}
@Override

View File

@@ -4,8 +4,8 @@ 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.Const;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.security.PasswordModifier;
import com.orion.ops.framework.common.utils.CryptoUtils;
@@ -29,7 +29,6 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
* 主机秘钥 服务实现类
@@ -131,21 +130,16 @@ public class HostKeyServiceImpl implements HostKeyService {
if (list.isEmpty()) {
// 查询数据库
list = hostKeyDAO.of().list(HostKeyConvert.MAPPER::toCache);
// 添加默认值 防止穿透
if (list.isEmpty()) {
list.add(HostKeyCacheDTO.builder()
.id(Const.NONE_ID)
.build());
}
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, HostKeyCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_KEY.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(HostCacheKeyDefine.HOST_KEY);
}
// 删除默认值
return list.stream()
.filter(s -> !s.getId().equals(Const.NONE_ID))
.map(HostKeyConvert.MAPPER::to)
.collect(Collectors.toList());
// 删除屏障
RedisMaps.removeBarrier(list);
// 转换
return Lists.map(list, HostKeyConvert.MAPPER::to);
}
@Override

View File

@@ -7,7 +7,6 @@ import com.orion.lang.utils.Booleans;
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.RedisMaps;
@@ -31,6 +30,7 @@ import com.orion.ops.module.infra.entity.dto.tag.TagDTO;
import com.orion.ops.module.infra.enums.DataGroupTypeEnum;
import com.orion.ops.module.infra.enums.FavoriteTypeEnum;
import com.orion.ops.module.infra.enums.TagTypeEnum;
import com.orion.spring.SpringHolder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -83,7 +83,7 @@ public class HostServiceImpl implements HostService {
log.info("HostService-createHost effect: {}", effect);
Long id = record.getId();
// 插入 tag
tagRelApi.addTagRelAsync(TagTypeEnum.HOST, id, request.getTags());
tagRelApi.addTagRel(TagTypeEnum.HOST, id, request.getTags());
// 引用分组
List<Long> groupIdList = request.getGroupIdList();
if (!Lists.isEmpty(groupIdList)) {
@@ -119,10 +119,10 @@ public class HostServiceImpl implements HostService {
log.info("HostService-updateHostById effect: {}", effect);
// 引用分组
dataGroupRelApi.updateGroupRel(DataGroupTypeEnum.HOST, request.getGroupIdList(), id);
// 更新 tag
tagRelApi.setTagRel(TagTypeEnum.HOST, id, request.getTags());
// 删除缓存
RedisMaps.delete(HostCacheKeyDefine.HOST_INFO);
// 更新 tag
tagRelApi.setTagRelAsync(TagTypeEnum.HOST, id, request.getTags());
return effect;
}
@@ -154,21 +154,16 @@ public class HostServiceImpl implements HostService {
if (list.isEmpty()) {
// 查询数据库
list = hostDAO.of().list(HostConvert.MAPPER::toCache);
// 添加默认值 防止穿透
if (list.isEmpty()) {
list.add(HostCacheDTO.builder()
.id(Const.NONE_ID)
.build());
}
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, HostCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(HostCacheKeyDefine.HOST_INFO.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(HostCacheKeyDefine.HOST_INFO);
}
// 删除默认值
return list.stream()
.filter(s -> !s.getId().equals(Const.NONE_ID))
.map(HostConvert.MAPPER::to)
.collect(Collectors.toList());
// 删除屏障
RedisMaps.removeBarrier(list);
// 转换
return Lists.map(list, HostConvert.MAPPER::to);
}
@Override
@@ -198,17 +193,24 @@ public class HostServiceImpl implements HostService {
// 删除
int effect = hostDAO.deleteById(id);
log.info("HostService-deleteHostById effect: {}", effect);
// 删除缓存
RedisMaps.delete(HostCacheKeyDefine.HOST_INFO, id);
// 删除主机引用
SpringHolder.getBean(HostService.class)
.deleteHostRelByIdAsync(id);
return effect;
}
@Override
public void deleteHostRelByIdAsync(Long id) {
// 删除配置
hostConfigDAO.deleteByHostId(id);
// 删除分组
dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.HOST, id);
// 删除缓存
RedisMaps.delete(HostCacheKeyDefine.HOST_INFO, id);
// 删除 tag 引用
tagRelApi.deleteRelIdAsync(TagTypeEnum.HOST, id);
tagRelApi.deleteRelId(TagTypeEnum.HOST, id);
// 删除收藏引用
favoriteApi.deleteByRelIdAsync(FavoriteTypeEnum.HOST, id);
return effect;
favoriteApi.deleteByRelId(FavoriteTypeEnum.HOST, id);
}
/**

View File

@@ -28,7 +28,7 @@ public interface FavoriteApi {
* @param type type
* @param relId relId
*/
void deleteByRelIdAsync(FavoriteTypeEnum type, Long relId);
void deleteByRelId(FavoriteTypeEnum type, Long relId);
/**
* 通过 relId 删除收藏
@@ -36,6 +36,6 @@ public interface FavoriteApi {
* @param type type
* @param relIdList relIdList
*/
void deleteByRelIdListAsync(FavoriteTypeEnum type, List<Long> relIdList);
void deleteByRelIdList(FavoriteTypeEnum type, List<Long> relIdList);
}

View File

@@ -22,7 +22,7 @@ public interface TagRelApi {
* @param relId relId
* @param tagIdList tagIdList
*/
void addTagRelAsync(TagTypeEnum type, Long relId, List<Long> tagIdList);
void addTagRel(TagTypeEnum type, Long relId, List<Long> tagIdList);
/**
* 设置标签引用 先删除后新增
@@ -31,7 +31,7 @@ public interface TagRelApi {
* @param relId relId
* @param tagIdList tagIdList
*/
void setTagRelAsync(TagTypeEnum type, Long relId, List<Long> tagIdList);
void setTagRel(TagTypeEnum type, Long relId, List<Long> tagIdList);
/**
* 获取引用 tag
@@ -73,7 +73,7 @@ public interface TagRelApi {
* @param type type
* @param relId relId
*/
void deleteRelIdAsync(TagTypeEnum type, Long relId);
void deleteRelId(TagTypeEnum type, Long relId);
/**
* 通过 relIdList 删除
@@ -81,6 +81,6 @@ public interface TagRelApi {
* @param type type
* @param relIdList relIdList
*/
void deleteRelIdListAsync(TagTypeEnum type, List<Long> relIdList);
void deleteRelIdList(TagTypeEnum type, List<Long> relIdList);
}

View File

@@ -39,15 +39,13 @@ public class FavoriteApiImpl implements FavoriteApi {
}
@Override
@Async("asyncExecutor")
public void deleteByRelIdAsync(FavoriteTypeEnum type, Long relId) {
public void deleteByRelId(FavoriteTypeEnum type, Long relId) {
Valid.allNotNull(type, relId);
favoriteDAO.deleteFavoriteByRelId(type.name(), relId);
}
@Override
@Async("asyncExecutor")
public void deleteByRelIdListAsync(FavoriteTypeEnum type, List<Long> relIdList) {
public void deleteByRelIdList(FavoriteTypeEnum type, List<Long> relIdList) {
Valid.notNull(type);
Valid.notEmpty(relIdList);
favoriteDAO.deleteFavoriteByRelIdList(type.name(), relIdList);

View File

@@ -31,8 +31,7 @@ public class TagRelApiImpl implements TagRelApi {
private TagRelService tagRelService;
@Override
@Async("asyncExecutor")
public void addTagRelAsync(TagTypeEnum type, Long relId, List<Long> tagIdList) {
public void addTagRel(TagTypeEnum type, Long relId, List<Long> tagIdList) {
Valid.notNull(relId);
if (Lists.isEmpty(tagIdList)) {
return;
@@ -41,8 +40,7 @@ public class TagRelApiImpl implements TagRelApi {
}
@Override
@Async("asyncExecutor")
public void setTagRelAsync(TagTypeEnum type, Long relId, List<Long> tagIdList) {
public void setTagRel(TagTypeEnum type, Long relId, List<Long> tagIdList) {
Valid.notNull(relId);
tagRelService.setTagRel(type.name(), relId, tagIdList);
}
@@ -73,14 +71,12 @@ public class TagRelApiImpl implements TagRelApi {
}
@Override
@Async("asyncExecutor")
public void deleteRelIdAsync(TagTypeEnum type, Long relId) {
public void deleteRelId(TagTypeEnum type, Long relId) {
tagRelService.deleteRelId(type.name(), relId);
}
@Override
@Async("asyncExecutor")
public void deleteRelIdListAsync(TagTypeEnum type, List<Long> relIdList) {
public void deleteRelIdList(TagTypeEnum type, List<Long> relIdList) {
tagRelService.deleteRelIdList(type.name(), relIdList);
}

View File

@@ -28,10 +28,10 @@ public interface TagConvert {
TagVO to(TagDO domain);
TagVO to(TagCacheDTO cache);
TagCacheDTO toCache(TagDO domain);
List<TagVO> to(List<TagDO> list);
List<TagVO> toList(List<TagCacheDTO> cache);
}

View File

@@ -1,7 +1,8 @@
package com.orion.ops.module.infra.define.cache;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import com.orion.ops.module.infra.entity.dto.DataGroupCacheDTO;
import com.orion.ops.module.infra.entity.dto.DataGroupRelCacheDTO;
@@ -20,6 +21,7 @@ public interface DataGroupCacheKeyDefine {
.key("data:group-list:{}")
.desc("数据分组列表结构 ${type}")
.type(DataGroupCacheDTO.class)
.struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS)
.build();
@@ -27,6 +29,7 @@ public interface DataGroupCacheKeyDefine {
.key("data:group-tree:{}")
.desc("数据分组树结构 ${type}")
.type(DataGroupCacheDTO.class)
.struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS)
.build();
@@ -34,6 +37,7 @@ public interface DataGroupCacheKeyDefine {
.key("data:group-rel:group:{}")
.desc("数据分组数据关联-分组 ${groupId}")
.type(DataGroupRelCacheDTO.class)
.struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS)
.build();
@@ -41,6 +45,7 @@ public interface DataGroupCacheKeyDefine {
.key("data:group-rel:type:{}")
.desc("数据分组数据关联-类型 ${type}")
.type(DataGroupRelCacheDTO.class)
.struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS)
.build();

View File

@@ -1,8 +1,9 @@
package com.orion.ops.module.infra.define.cache;
import com.alibaba.fastjson.JSONObject;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import com.orion.ops.module.infra.entity.dto.DictKeyCacheDTO;
import java.util.concurrent.TimeUnit;
@@ -20,6 +21,7 @@ public interface DictCacheKeyDefine {
.key("dict:keys")
.desc("字典配置项")
.type(DictKeyCacheDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(1, TimeUnit.DAYS)
.build();
@@ -27,6 +29,7 @@ public interface DictCacheKeyDefine {
.key("dict:schema:{}")
.desc("字典配置项 schema ${key}")
.type(JSONObject.class)
.struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS)
.build();
@@ -34,6 +37,7 @@ public interface DictCacheKeyDefine {
.key("dict:values:{}")
.desc("字典配置值 ${key}")
.type(JSONObject.class)
.struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS)
.build();

View File

@@ -1,7 +1,8 @@
package com.orion.ops.module.infra.define.cache;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import java.util.concurrent.TimeUnit;
@@ -18,6 +19,7 @@ public interface FavoriteCacheKeyDefine {
.key("favorite:{}:{}")
.desc("收藏信息 ${type} ${userId}")
.type(Long.class)
.struct(RedisCacheStruct.LIST)
.timeout(3, TimeUnit.DAYS)
.build();

View File

@@ -1,8 +1,9 @@
package com.orion.ops.module.infra.define.cache;
import com.alibaba.fastjson.JSONObject;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import java.util.concurrent.TimeUnit;
@@ -19,6 +20,7 @@ public interface PreferenceCacheKeyDefine {
.key("user:preference:{}:{}")
.desc("用户偏好 ${userId} ${type}")
.type(JSONObject.class)
.struct(RedisCacheStruct.STRING)
.timeout(1, TimeUnit.DAYS)
.build();

View File

@@ -1,7 +1,8 @@
package com.orion.ops.module.infra.define.cache;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import com.orion.ops.module.infra.entity.dto.TagCacheDTO;
import java.util.concurrent.TimeUnit;
@@ -19,6 +20,7 @@ public interface TagCacheKeyDefine {
.key("tag:name:{}")
.desc("tag 名称 ${type}")
.type(TagCacheDTO.class)
.struct(RedisCacheStruct.LIST)
.timeout(8, TimeUnit.HOURS)
.build();
@@ -26,6 +28,7 @@ public interface TagCacheKeyDefine {
.key("tag:rel:{}:{}")
.desc("tag 引用 ${type} ${relId}")
.type(TagCacheDTO.class)
.struct(RedisCacheStruct.STRING)
.timeout(8, TimeUnit.HOURS)
.build();

View File

@@ -1,7 +1,8 @@
package com.orion.ops.module.infra.define.cache;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import java.util.concurrent.TimeUnit;
@@ -18,6 +19,7 @@ public interface TipsCacheKeyDefine {
.key("user:tips:{}")
.desc("user:tips ${userId} 90天不会重复提示")
.type(String.class)
.struct(RedisCacheStruct.LIST)
.timeout(90, TimeUnit.DAYS)
.build();

View File

@@ -1,7 +1,8 @@
package com.orion.ops.module.infra.define.cache;
import com.orion.lang.define.cache.CacheKeyBuilder;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import com.orion.ops.framework.common.security.LoginUser;
import com.orion.ops.module.infra.entity.dto.LoginTokenDTO;
import com.orion.ops.module.infra.entity.dto.UserInfoDTO;
@@ -21,12 +22,14 @@ public interface UserCacheKeyDefine {
.key("user:info:{}")
.desc("用户信息 ${id}")
.type(LoginUser.class)
.struct(RedisCacheStruct.STRING)
.build();
CacheKeyDefine USER_LIST = new CacheKeyBuilder()
.key("user:base:list")
.desc("用户列表")
.type(UserInfoDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(1, TimeUnit.DAYS)
.build();
@@ -34,6 +37,7 @@ public interface UserCacheKeyDefine {
.key("user:login-failed:{}")
.desc("用户登录失败次数 ${username}")
.type(Integer.class)
.struct(RedisCacheStruct.STRING)
.timeout(3, TimeUnit.DAYS)
.build();
@@ -41,6 +45,7 @@ public interface UserCacheKeyDefine {
.key("user:token:{}:{}")
.desc("用户登录 token ${id} ${time}")
.type(LoginTokenDTO.class)
.struct(RedisCacheStruct.STRING)
.timeout(24, TimeUnit.HOURS)
.build();
@@ -48,6 +53,7 @@ public interface UserCacheKeyDefine {
.key("user:refresh:{}:{}")
.desc("用户刷新 token ${id} ${time}")
.type(LoginTokenDTO.class)
.struct(RedisCacheStruct.STRING)
.timeout(28, TimeUnit.HOURS)
.build();

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.infra.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -21,7 +22,7 @@ import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "DataGroupCacheDTO", description = "数据分组 缓存对象")
public class DataGroupCacheDTO implements Serializable {
public class DataGroupCacheDTO implements LongCacheIdModel, Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.infra.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -20,7 +21,7 @@ import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "DataGroupRelCacheDTO", description = "数据分组关联 缓存对象")
public class DataGroupRelCacheDTO implements Serializable {
public class DataGroupRelCacheDTO implements LongCacheIdModel, Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.infra.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -21,7 +22,7 @@ import java.util.Date;
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "DictKeyCacheDTO", description = "字典配置项 缓存对象")
public class DictKeyCacheDTO implements Serializable {
public class DictKeyCacheDTO implements LongCacheIdModel, Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -1,8 +1,11 @@
package com.orion.ops.module.infra.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.io.Serializable;
/**
* @author Jiahang Li
* @version 1.0.0
@@ -14,7 +17,7 @@ import lombok.*;
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Schema(name = "TagCacheDTO", description = "菜单 缓存业务对象")
public class TagCacheDTO {
public class TagCacheDTO implements LongCacheIdModel, Serializable {
@EqualsAndHashCode.Include
@Schema(description = "id")

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.infra.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -20,7 +21,7 @@ import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "UserInfoDTO", description = "用户信息 缓存对象")
public class UserInfoDTO implements Serializable {
public class UserInfoDTO implements LongCacheIdModel, Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -1,10 +1,9 @@
package com.orion.ops.module.infra.service.impl;
import com.orion.lang.define.cache.CacheKeyDefine;
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.RedisStrings;
@@ -229,17 +228,13 @@ public class DataGroupRelServiceImpl implements DataGroupRelService {
if (Lists.isEmpty(list)) {
// 查询数据库
list = valueSupplier.get();
// 添加默认值 防止穿透
if (Lists.isEmpty(list)) {
list.add(DataGroupRelCacheDTO.builder()
.id(Const.NONE_ID)
.build());
}
// 设置屏障 防止穿透
RedisStrings.checkBarrier(list, DataGroupRelCacheDTO::new);
// 设置缓存
RedisStrings.setJson(key, define, list);
}
// 删除默认值
list.removeIf(s -> s.getId().equals(Const.NONE_ID));
// 删除屏障
RedisStrings.removeBarrier(list);
return list;
}

View File

@@ -161,17 +161,13 @@ public class DataGroupServiceImpl implements DataGroupService {
.eq(DataGroupDO::getType, type)
.then()
.list(DataGroupConvert.MAPPER::toCache);
// 添加默认值 防止穿透
if (list.isEmpty()) {
list.add(DataGroupCacheDTO.builder()
.id(Const.NONE_ID)
.build());
}
// 设置屏障 防止穿透
RedisStrings.checkBarrier(list, DataGroupCacheDTO::new);
// 设置缓存
RedisStrings.setJson(key, DataGroupCacheKeyDefine.DATA_GROUP_LIST, list);
}
// 删除默认值
list.removeIf(s -> s.getId().equals(Const.NONE_ID));
// 删除屏障
RedisStrings.removeBarrier(list);
return list;
}
@@ -183,12 +179,9 @@ public class DataGroupServiceImpl implements DataGroupService {
if (Lists.isEmpty(treeData)) {
// 查询列表缓存
List<DataGroupCacheDTO> rows = this.getDataGroupListByCache(type);
// 添加默认值 防止穿透
if (Lists.isEmpty(rows)) {
treeData = Lists.of(DataGroupCacheDTO.builder()
.id(Const.NONE_ID)
.build());
} else {
// 设置屏障 防止穿透
RedisStrings.checkBarrier(rows, DataGroupCacheDTO::new);
if (!Lists.isEmpty(rows)) {
// 构建树
DataGroupCacheDTO rootNode = DataGroupCacheDTO.builder()
.id(Const.ROOT_PARENT_ID)
@@ -200,8 +193,8 @@ public class DataGroupServiceImpl implements DataGroupService {
// 设置缓存
RedisStrings.setJson(key, DataGroupCacheKeyDefine.DATA_GROUP_LIST, treeData);
}
// 删除默认值
treeData.removeIf(s -> s.getId().equals(Const.NONE_ID));
// 删除屏障
RedisStrings.removeBarrier(treeData);
return treeData;
}

View File

@@ -106,19 +106,16 @@ public class DictKeyServiceImpl implements DictKeyService {
if (list.isEmpty()) {
// 查询数据库
list = dictKeyDAO.of().list(DictKeyConvert.MAPPER::toCache);
// 添加默认值 防止穿透
if (list.isEmpty()) {
list.add(DictKeyCacheDTO.builder()
.id(Const.NONE_ID)
.build());
}
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, DictKeyCacheDTO::new);
// 设置缓存
RedisMaps.putAllJson(DictCacheKeyDefine.DICT_KEY.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(DictCacheKeyDefine.DICT_KEY);
}
// 删除默认值
// 删除屏障
RedisMaps.removeBarrier(list);
// 转换
return list.stream()
.filter(s -> !s.getId().equals(Const.NONE_ID))
.map(DictKeyConvert.MAPPER::to)
.sorted(Comparator.comparing(DictKeyVO::getId).reversed())
.collect(Collectors.toList());

View File

@@ -6,7 +6,6 @@ import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.utils.collect.Lists;
import com.orion.lang.utils.crypto.Signatures;
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.ErrorCode;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.security.LoginUser;
@@ -164,21 +163,16 @@ public class SystemUserServiceImpl implements SystemUserService {
if (list.isEmpty()) {
// 查询数据库
list = systemUserDAO.of().list(SystemUserConvert.MAPPER::toUserInfo);
// 添加默认值 防止穿透
if (list.isEmpty()) {
list.add(UserInfoDTO.builder()
.id(Const.NONE_ID)
.build());
}
// 设置屏障 防止穿透
RedisMaps.checkBarrier(list, UserInfoDTO::new);
// 设置缓存
RedisMaps.putAllJson(UserCacheKeyDefine.USER_LIST.getKey(), s -> s.getId().toString(), list);
RedisMaps.setExpire(UserCacheKeyDefine.USER_LIST);
}
// 删除默认值
return list.stream()
.filter(s -> !s.getId().equals(Const.NONE_ID))
.map(SystemUserConvert.MAPPER::to)
.collect(Collectors.toList());
// 删除屏障
RedisMaps.removeBarrier(list);
// 转换
return Lists.map(list, SystemUserConvert.MAPPER::to);
}
@Override

View File

@@ -2,6 +2,7 @@ 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;
@@ -61,23 +62,21 @@ public class TagServiceImpl implements TagService {
public List<TagVO> getTagList(String type) {
// 查询缓存
String cacheKey = TagCacheKeyDefine.TAG_NAME.format(type);
List<TagCacheDTO> cacheValues = RedisLists.rangeJson(cacheKey, TagCacheKeyDefine.TAG_NAME);
if (cacheValues.isEmpty()) {
List<TagCacheDTO> list = RedisLists.rangeJson(cacheKey, TagCacheKeyDefine.TAG_NAME);
if (list.isEmpty()) {
// 为空则需要查询缓存
LambdaQueryWrapper<TagDO> wrapper = Conditions.eq(TagDO::getType, type);
cacheValues = tagDAO.of(wrapper).list(TagConvert.MAPPER::toCache);
// 添加默认值 防止穿透
if (cacheValues.isEmpty()) {
cacheValues.add(TagCacheDTO.builder().id(Const.NONE_ID).build());
}
list = tagDAO.of(wrapper).list(TagConvert.MAPPER::toCache);
// 设置屏障 防止穿透
RedisLists.checkBarrier(list, TagCacheDTO::new);
// 设置到缓存
RedisLists.pushAllJson(cacheKey, cacheValues);
RedisLists.pushAllJson(cacheKey, list);
RedisLists.setExpire(cacheKey, TagCacheKeyDefine.TAG_NAME);
}
// 删除防止穿透的 key
cacheValues.removeIf(s -> Const.NONE_ID.equals(s.getId()));
// 删除屏障
RedisLists.removeBarrier(list);
// 转换
return TagConvert.MAPPER.toList(cacheValues);
return Lists.map(list, TagConvert.MAPPER::to);
}
@Override