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

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