feat(system): 添加字典和菜单缓存清除接口,并优化字典的缓存逻辑

- 在 DictController 中添加清除字典缓存接口
- 在 MenuController 中添加清除菜单缓存接口
- 更新数据库变更脚本,添加相关权限
- 优化字典的缓存逻辑
This commit is contained in:
2025-03-12 22:11:38 +08:00
parent 2e76caa35e
commit 093d2d3c8a
6 changed files with 35 additions and 2 deletions

View File

@@ -16,7 +16,9 @@
package top.continew.admin.system.mapper;
import com.alicp.jetcache.anno.Cached;
import org.apache.ibatis.annotations.Param;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.system.model.entity.DictItemDO;
import top.continew.starter.data.mp.base.BaseMapper;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
@@ -37,5 +39,6 @@ public interface DictItemMapper extends BaseMapper<DictItemDO> {
* @param dictCode 字典编码
* @return 字典项列表
*/
@Cached(key = "#dictCode", name = CacheConstants.DICT_KEY_PREFIX)
List<LabelValueResp> listByDictCode(@Param("dictCode") String dictCode);
}

View File

@@ -19,9 +19,9 @@ package top.continew.admin.system.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import com.alicp.jetcache.anno.Cached;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.system.mapper.DictItemMapper;
@@ -48,6 +48,7 @@ import java.util.stream.Collectors;
* @author Charles7c
* @since 2023/9/11 21:29
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictItemDO, DictItemResp, DictItemResp, DictItemQuery, DictItemReq> implements DictItemService {
@@ -70,7 +71,6 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
}
@Override
@Cached(key = "#dictCode", name = CacheConstants.DICT_KEY_PREFIX)
public List<LabelValueResp> listByDictCode(String dictCode) {
return Optional.ofNullable(ENUM_DICT_CACHE.get(dictCode.toLowerCase()))
.orElseGet(() -> baseMapper.listByDictCode(dictCode));
@@ -129,5 +129,6 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
ENUM_DICT_CACHE.putAll(classSet.stream()
.collect(Collectors.toMap(cls -> StrUtil.toUnderlineCase(cls.getSimpleName())
.toLowerCase(), this::toEnumDict)));
log.debug("枚举字典已缓存到内存:{}", ENUM_DICT_CACHE.keySet());
}
}

View File

@@ -16,13 +16,19 @@
package top.continew.admin.controller.system;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.controller.BaseController;
import top.continew.admin.system.model.query.DictQuery;
import top.continew.admin.system.model.req.DictReq;
import top.continew.admin.system.model.resp.DictResp;
import top.continew.admin.system.service.DictService;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.enums.Api;
@@ -36,4 +42,11 @@ import top.continew.starter.extension.crud.enums.Api;
@RestController
@CrudRequestMapping(value = "/system/dict", api = {Api.LIST, Api.DETAIL, Api.ADD, Api.UPDATE, Api.DELETE})
public class DictController extends BaseController<DictService, DictResp, DictResp, DictQuery, DictReq> {
@Operation(summary = "清除缓存", description = "清除缓存")
@SaCheckPermission("system:dict:item:clearCache")
@DeleteMapping("/cache/{code}")
public void clearCache(@PathVariable String code) {
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + code);
}
}

View File

@@ -16,15 +16,20 @@
package top.continew.admin.controller.system;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.controller.BaseController;
import top.continew.admin.system.model.query.MenuQuery;
import top.continew.admin.system.model.req.MenuReq;
import top.continew.admin.system.model.resp.MenuResp;
import top.continew.admin.system.service.MenuService;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.util.URLUtils;
import top.continew.starter.core.validation.ValidationUtils;
@@ -45,6 +50,13 @@ import java.lang.reflect.Method;
@CrudRequestMapping(value = "/system/menu", api = {Api.TREE, Api.DETAIL, Api.ADD, Api.UPDATE, Api.DELETE})
public class MenuController extends BaseController<MenuService, MenuResp, MenuResp, MenuQuery, MenuReq> {
@Operation(summary = "清除缓存", description = "清除缓存")
@SaCheckPermission("system:menu:clearCache")
@DeleteMapping("/cache")
public void clearCache() {
RedisUtils.deleteByPattern(CacheConstants.ROLE_MENU_KEY_PREFIX + StringConstants.ASTERISK);
}
@Override
public void preHandle(CrudApi crudApi, Object[] args, Method targetMethod, Class<?> targetClass) throws Exception {
super.preHandle(crudApi, args, targetMethod, targetClass);

View File

@@ -34,6 +34,7 @@ VALUES
(1053, '新增', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:add', 3, 1, 1, NOW()),
(1054, '修改', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:update', 4, 1, 1, NOW()),
(1055, '删除', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:delete', 5, 1, 1, NOW()),
(1056, '清除缓存', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:clearCache', 6, 1, 1, NOW()),
(1060, '部门管理', 1000, 2, '/system/dept', 'SystemDept', 'system/dept/index', NULL, 'mind-mapping', b'0', b'0', b'0', NULL, 4, 1, 1, NOW()),
(1061, '列表', 1060, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dept:list', 1, 1, 1, NOW()),
@@ -55,6 +56,7 @@ VALUES
(1083, '新增', 1080, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dict:item:add', 3, 1, 1, NOW()),
(1084, '修改', 1080, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dict:item:update', 4, 1, 1, NOW()),
(1085, '删除', 1080, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dict:item:delete', 5, 1, 1, NOW()),
(1086, '清除缓存', 1080, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dict:item:clearCache', 6, 1, 1, NOW()),
(1090, '通知公告', 1000, 2, '/system/notice', 'SystemNotice', 'system/notice/index', NULL, 'notification', b'0', b'0', b'0', NULL, 6, 1, 1, NOW()),
(1091, '列表', 1090, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:notice:list', 1, 1, 1, NOW()),

View File

@@ -34,6 +34,7 @@ VALUES
(1053, '新增', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:add', 3, 1, 1, NOW()),
(1054, '修改', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:update', 4, 1, 1, NOW()),
(1055, '删除', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:delete', 5, 1, 1, NOW()),
(1056, '清除缓存', 1050, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:menu:clearCache', 6, 1, 1, NOW()),
(1060, '部门管理', 1000, 2, '/system/dept', 'SystemDept', 'system/dept/index', NULL, 'mind-mapping', false, false, false, NULL, 4, 1, 1, NOW()),
(1061, '列表', 1060, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dept:list', 1, 1, 1, NOW()),
@@ -55,6 +56,7 @@ VALUES
(1083, '新增', 1080, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dict:item:add', 3, 1, 1, NOW()),
(1084, '修改', 1080, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dict:item:update', 4, 1, 1, NOW()),
(1085, '删除', 1080, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dict:item:delete', 5, 1, 1, NOW()),
(1086, '清除缓存', 1080, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:dict:item:clearCache', 6, 1, 1, NOW()),
(1090, '通知公告', 1000, 2, '/system/notice', 'SystemNotice', 'system/notice/index', NULL, 'notification', false, false, false, NULL, 6, 1, 1, NOW()),
(1091, '列表', 1090, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'system:notice:list', 1, 1, 1, NOW()),