diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/cache/MenuClearListener.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/cache/MenuClearListener.java new file mode 100644 index 000000000..6b5369339 --- /dev/null +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/cache/MenuClearListener.java @@ -0,0 +1,21 @@ +package cn.stylefeng.roses.kernel.sys.modular.menu.cache; + +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * 监听菜单的清空 + * + * @author fengshuonan + * @since 2023/7/15 0:08 + */ +@Service +public class MenuClearListener { + + @Resource(name = "menuCodeCache") + private CacheOperatorApi menuCodeCache; + + +} diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/cache/menucode/MenuCodeMemoryCache.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/cache/menucode/MenuCodeMemoryCache.java new file mode 100644 index 000000000..b53217144 --- /dev/null +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/cache/menucode/MenuCodeMemoryCache.java @@ -0,0 +1,24 @@ +package cn.stylefeng.roses.kernel.sys.modular.menu.cache.menucode; + +import cn.hutool.cache.impl.TimedCache; +import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator; +import cn.stylefeng.roses.kernel.sys.modular.menu.constants.MenuConstants; + +/** + * 菜单编码的缓存 + * + * @author fengshuonan + * @since 2023/7/15 0:08 + */ +public class MenuCodeMemoryCache extends AbstractMemoryCacheOperator { + + public MenuCodeMemoryCache(TimedCache timedCache) { + super(timedCache); + } + + @Override + public String getCommonKeyPrefix() { + return MenuConstants.MENU_CODE_CACHE_PREFIX; + } + +} \ No newline at end of file diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/cache/menucode/MenuCodeRedisCache.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/cache/menucode/MenuCodeRedisCache.java new file mode 100644 index 000000000..fb592e294 --- /dev/null +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/cache/menucode/MenuCodeRedisCache.java @@ -0,0 +1,24 @@ +package cn.stylefeng.roses.kernel.sys.modular.menu.cache.menucode; + +import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator; +import cn.stylefeng.roses.kernel.sys.modular.menu.constants.MenuConstants; +import org.springframework.data.redis.core.RedisTemplate; + +/** + * 菜单编码的缓存 + * + * @author fengshuonan + * @since 2023/7/15 0:11 + */ +public class MenuCodeRedisCache extends AbstractRedisCacheOperator { + + public MenuCodeRedisCache(RedisTemplate redisTemplate) { + super(redisTemplate); + } + + @Override + public String getCommonKeyPrefix() { + return MenuConstants.MENU_CODE_CACHE_PREFIX; + } + +} diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/constants/MenuConstants.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/constants/MenuConstants.java new file mode 100644 index 000000000..d1f5923d2 --- /dev/null +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/constants/MenuConstants.java @@ -0,0 +1,16 @@ +package cn.stylefeng.roses.kernel.sys.modular.menu.constants; + +/** + * 菜单的常量 + * + * @author fengshuonan + * @since 2023/7/15 0:09 + */ +public interface MenuConstants { + + /** + * 菜单编码的前缀 + */ + String MENU_CODE_CACHE_PREFIX = "MENU:CODE:"; + +} diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/service/impl/SysMenuServiceImpl.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/service/impl/SysMenuServiceImpl.java index 3edd12a1c..e5ab03719 100644 --- a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/service/impl/SysMenuServiceImpl.java +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/menu/service/impl/SysMenuServiceImpl.java @@ -4,12 +4,14 @@ import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.db.api.DbOperatorApi; import cn.stylefeng.roses.kernel.rule.constants.SymbolConstant; import cn.stylefeng.roses.kernel.rule.constants.TreeConstants; import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException; import cn.stylefeng.roses.kernel.rule.tree.buildpids.PidStructureBuildUtil; import cn.stylefeng.roses.kernel.sys.api.callback.RemoveMenuCallbackApi; +import cn.stylefeng.roses.kernel.sys.api.constants.SysConstants; import cn.stylefeng.roses.kernel.sys.api.pojo.menu.UserAppMenuInfo; import cn.stylefeng.roses.kernel.sys.modular.app.service.SysAppService; import cn.stylefeng.roses.kernel.sys.modular.menu.entity.SysMenu; @@ -45,6 +47,9 @@ public class SysMenuServiceImpl extends ServiceImpl impl @Resource private DbOperatorApi dbOperatorApi; + @Resource(name = "menuCodeCache") + private CacheOperatorApi menuCodeCache; + @Override public void add(SysMenuRequest sysMenuRequest) { @@ -197,16 +202,43 @@ public class SysMenuServiceImpl extends ServiceImpl impl @Override public List getMenuCodeList(List menuIdList) { + List result = new ArrayList<>(); + if (ObjectUtil.isEmpty(menuIdList)) { - return new ArrayList<>(); + return result; } - LambdaQueryWrapper sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>(); - sysMenuLambdaQueryWrapper.in(SysMenu::getMenuId, menuIdList); - sysMenuLambdaQueryWrapper.select(SysMenu::getMenuCode); - List sysMenuList = this.list(sysMenuLambdaQueryWrapper); + for (Long menuId : menuIdList) { - return sysMenuList.stream().map(SysMenu::getMenuCode).collect(Collectors.toList()); + String menuIdKey = menuId.toString(); + + // 先从缓存查询,是否有菜单编码 + String menuCode = menuCodeCache.get(menuIdKey); + + if (ObjectUtil.isNotEmpty(menuCode)) { + result.add(menuCode); + continue; + } + + // 查询库中的菜单编码 + LambdaQueryWrapper sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>(); + sysMenuLambdaQueryWrapper.eq(SysMenu::getMenuId, menuIdList); + sysMenuLambdaQueryWrapper.select(SysMenu::getMenuCode); + SysMenu sysMenu = this.getOne(sysMenuLambdaQueryWrapper, false); + + if (sysMenu != null) { + String menuCodeQueryResult = sysMenu.getMenuCode(); + if (ObjectUtil.isNotEmpty(menuCodeQueryResult)) { + + result.add(menuCodeQueryResult); + + // 添加到缓存一份 + menuCodeCache.put(menuIdKey, menuCodeQueryResult, SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS); + } + } + } + + return result; } @Override