[代码完善](v2.5): v2.5 beta 菜单编辑缓存优化,部门编辑缓存优化

close https://github.com/elunez/eladmin/issues/390
pull/394/head
ZhengJie 2020-06-08 12:00:43 +08:00
parent a5880e701c
commit 0f7cefaf4b
5 changed files with 75 additions and 49 deletions

View File

@ -20,6 +20,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Set;
/**
@ -66,4 +68,13 @@ public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificat
@Query(value = "select count(1) from sys_role r, sys_roles_depts d where " +
"r.role_id = d.role_id and d.dept_id in ?1",nativeQuery = true)
int countByDepts(Set<Long> deptIds);
/**
* Id
* @param menuIds /
* @return /
*/
@Query(value = "SELECT r.* FROM sys_role r, sys_roles_menus m WHERE " +
"r.role_id = m.role_id AND m.menu_id in ?1",nativeQuery = true)
List<Role> findInMenuId(List<Long> menuIds);
}

View File

@ -126,4 +126,11 @@ public interface RoleService {
* @param ids /
*/
void verification(Set<Long> ids);
/**
* Id
* @param menuIds /
* @return /
*/
List<Role> findInMenuId(List<Long> menuIds);
}

View File

@ -106,18 +106,17 @@ public class DeptServiceImpl implements DeptService {
deptRepository.save(resources);
// 计算子节点数目
resources.setSubCount(0);
if(resources.getPid() != null){
// 清理缓存
redisUtils.del("dept::pid:" + resources.getPid());
redisUtils.del("dept::pid:" + (resources.getPid() == null ? 0 : resources.getPid()));
updateSubCnt(resources.getPid());
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(Dept resources) {
// 旧的部门
Long pid = findById(resources.getId()).getPid();
Long oldPid = findById(resources.getId()).getPid();
Long newPid = resources.getPid();
if(resources.getPid() != null && resources.getId().equals(resources.getPid())) {
throw new BadRequestException("上级不能为自己");
}
@ -125,14 +124,11 @@ public class DeptServiceImpl implements DeptService {
ValidationUtil.isNull( dept.getId(),"Dept","id",resources.getId());
resources.setId(dept.getId());
deptRepository.save(resources);
if(resources.getPid() == null){
updateSubCnt(pid);
} else {
pid = resources.getPid();
updateSubCnt(resources.getPid());
}
// 更新父节点中子节点数目
updateSubCnt(oldPid);
updateSubCnt(newPid);
// 清理缓存
delCaches(resources.getId(), pid);
delCaches(resources.getId(), oldPid, newPid);
}
@Override
@ -140,13 +136,11 @@ public class DeptServiceImpl implements DeptService {
public void delete(Set<DeptDto> deptDtos) {
for (DeptDto deptDto : deptDtos) {
// 清理缓存
delCaches(deptDto.getId(), deptDto.getPid());
delCaches(deptDto.getId(), deptDto.getPid(), null);
deptRepository.deleteById(deptDto.getId());
if(deptDto.getPid() != null){
updateSubCnt(deptDto.getPid());
}
}
}
@Override
public void download(List<DeptDto> deptDtos, HttpServletResponse response) throws IOException {
@ -235,11 +229,6 @@ public class DeptServiceImpl implements DeptService {
return map;
}
private void updateSubCnt(Long deptId){
int count = deptRepository.countByPid(deptId);
deptRepository.updateSubCntById(count, deptId);
}
@Override
public void verification(Set<DeptDto> deptDtos) {
Set<Long> deptIds = deptDtos.stream().map(DeptDto::getId).collect(Collectors.toSet());
@ -251,17 +240,25 @@ public class DeptServiceImpl implements DeptService {
}
}
private void updateSubCnt(Long deptId){
if(deptId != null){
int count = deptRepository.countByPid(deptId);
deptRepository.updateSubCntById(count, deptId);
}
}
/**
*
* @param id /
* @param oldPid /
* @param newPid /
*/
public void delCaches(Long id, Long pid){
public void delCaches(Long id, Long oldPid, Long newPid){
List<User> users = userRepository.findByDeptRoleId(id);
// 删除数据权限
redisUtils.delByKeys("data::user:",users.stream().map(User::getId).collect(Collectors.toSet()));
redisUtils.del("dept::id:" + id);
if (pid != null) {
redisUtils.del("dept::pid:" + pid);
}
redisUtils.del("dept::pid:" + (oldPid == null ? 0 : oldPid));
redisUtils.del("dept::pid:" + (newPid == null ? 0 : newPid));
}
}

View File

@ -19,6 +19,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.domain.vo.MenuMetaVo;
import me.zhengjie.modules.system.domain.vo.MenuVo;
@ -125,10 +126,8 @@ public class MenuServiceImpl implements MenuService {
menuRepository.save(resources);
// 计算子节点数目
resources.setSubCount(0);
if(resources.getPid() != null){
// 清理缓存
// 更新父节点菜单数目
updateSubCnt(resources.getPid());
}
redisUtils.del("menu::pid:" + (resources.getPid() == null ? 0 : resources.getPid()));
}
@ -139,8 +138,6 @@ public class MenuServiceImpl implements MenuService {
throw new BadRequestException("上级不能为自己");
}
Menu menu = menuRepository.findById(resources.getId()).orElseGet(Menu::new);
// 记录旧的父节点ID
Long pid = menu.getPid();
ValidationUtil.isNull(menu.getId(),"Permission","id",resources.getId());
if(resources.getIFrame()){
@ -158,6 +155,11 @@ public class MenuServiceImpl implements MenuService {
if(resources.getPid().equals(0L)){
resources.setPid(null);
}
// 记录的父节点ID
Long oldPid = menu.getPid();
Long newPid = resources.getPid();
if(StringUtils.isNotBlank(resources.getComponentName())){
menu1 = menuRepository.findByComponentName(resources.getComponentName());
if(menu1 != null && !menu1.getId().equals(menu.getId())){
@ -177,15 +179,11 @@ public class MenuServiceImpl implements MenuService {
menu.setPermission(resources.getPermission());
menu.setType(resources.getType());
menuRepository.save(menu);
// 计算子节点数目
if(resources.getPid() == null){
updateSubCnt(pid);
} else {
pid = resources.getPid();
updateSubCnt(resources.getPid());
}
// 计算父级菜单节点数目
updateSubCnt(oldPid);
updateSubCnt(newPid);
// 清理缓存
delCaches(resources.getId(), pid);
delCaches(resources.getId(), oldPid, newPid);
}
@Override
@ -206,14 +204,12 @@ public class MenuServiceImpl implements MenuService {
public void delete(Set<Menu> menuSet) {
for (Menu menu : menuSet) {
// 清理缓存
delCaches(menu.getId(), menu.getPid());
delCaches(menu.getId(), menu.getPid(), null);
roleService.untiedMenu(menu.getId());
menuRepository.deleteById(menu.getId());
if(menu.getPid() != null){
updateSubCnt(menu.getPid());
}
}
}
@Override
@Cacheable(key = "'pid:' + #p0")
@ -336,19 +332,29 @@ public class MenuServiceImpl implements MenuService {
}
private void updateSubCnt(Long menuId){
if(menuId != null){
int count = menuRepository.countByPid(menuId);
menuRepository.updateSubCntById(count, menuId);
}
}
/**
*
* @param id ID
* @param pid ID
* @param oldPid ID
* @param newPid ID
*/
public void delCaches(Long id, Long pid){
public void delCaches(Long id, Long oldPid, Long newPid){
List<User> users = userRepository.findByMenuId(id);
redisUtils.del("menu::id:" +id);
redisUtils.delByKeys("menu::user:",users.stream().map(User::getId).collect(Collectors.toSet()));
redisUtils.del("menu::pid:" + (pid == null ? 0 : pid));
redisUtils.del("menu::pid:" + (oldPid == null ? 0 : oldPid));
redisUtils.del("menu::pid:" + (newPid == null ? 0 : newPid));
// 清除 Role 缓存
List<Role> roles = roleService.findInMenuId(new ArrayList<Long>(){{
add(id);
add(newPid == null ? 0 : newPid);
}});
redisUtils.delByKeys("role::id:",roles.stream().map(Role::getId).collect(Collectors.toSet()));
}
}

View File

@ -210,4 +210,9 @@ public class RoleServiceImpl implements RoleService {
throw new BadRequestException("所选角色存在用户关联,请解除关联再试!");
}
}
@Override
public List<Role> findInMenuId(List<Long> menuIds) {
return roleRepository.findInMenuId(menuIds);
}
}