Skip to content

Commit 0f7cefa

Browse files
committed
[代码完善](v2.5): v2.5 beta 菜单编辑缓存优化,部门编辑缓存优化
close elunez#390
1 parent a5880e7 commit 0f7cefa

File tree

5 files changed

+75
-49
lines changed

5 files changed

+75
-49
lines changed

eladmin-system/src/main/java/me/zhengjie/modules/system/repository/RoleRepository.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
2121
import org.springframework.data.jpa.repository.Modifying;
2222
import org.springframework.data.jpa.repository.Query;
23+
24+
import java.util.List;
2325
import java.util.Set;
2426

2527
/**
@@ -66,4 +68,13 @@ public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificat
6668
@Query(value = "select count(1) from sys_role r, sys_roles_depts d where " +
6769
"r.role_id = d.role_id and d.dept_id in ?1",nativeQuery = true)
6870
int countByDepts(Set<Long> deptIds);
71+
72+
/**
73+
* 根据菜单Id查询
74+
* @param menuIds /
75+
* @return /
76+
*/
77+
@Query(value = "SELECT r.* FROM sys_role r, sys_roles_menus m WHERE " +
78+
"r.role_id = m.role_id AND m.menu_id in ?1",nativeQuery = true)
79+
List<Role> findInMenuId(List<Long> menuIds);
6980
}

eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,11 @@ public interface RoleService {
126126
* @param ids /
127127
*/
128128
void verification(Set<Long> ids);
129+
130+
/**
131+
* 根据菜单Id查询
132+
* @param menuIds /
133+
* @return /
134+
*/
135+
List<Role> findInMenuId(List<Long> menuIds);
129136
}

eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,45 +106,39 @@ public void create(Dept resources) {
106106
deptRepository.save(resources);
107107
// 计算子节点数目
108108
resources.setSubCount(0);
109-
if(resources.getPid() != null){
110-
// 清理缓存
111-
redisUtils.del("dept::pid:" + resources.getPid());
112-
updateSubCnt(resources.getPid());
113-
}
109+
// 清理缓存
110+
redisUtils.del("dept::pid:" + (resources.getPid() == null ? 0 : resources.getPid()));
111+
updateSubCnt(resources.getPid());
114112
}
115113

116114
@Override
117115
@Transactional(rollbackFor = Exception.class)
118116
public void update(Dept resources) {
119117
// 旧的部门
120-
Long pid = findById(resources.getId()).getPid();
118+
Long oldPid = findById(resources.getId()).getPid();
119+
Long newPid = resources.getPid();
121120
if(resources.getPid() != null && resources.getId().equals(resources.getPid())) {
122121
throw new BadRequestException("上级不能为自己");
123122
}
124123
Dept dept = deptRepository.findById(resources.getId()).orElseGet(Dept::new);
125124
ValidationUtil.isNull( dept.getId(),"Dept","id",resources.getId());
126125
resources.setId(dept.getId());
127126
deptRepository.save(resources);
128-
if(resources.getPid() == null){
129-
updateSubCnt(pid);
130-
} else {
131-
pid = resources.getPid();
132-
updateSubCnt(resources.getPid());
133-
}
127+
// 更新父节点中子节点数目
128+
updateSubCnt(oldPid);
129+
updateSubCnt(newPid);
134130
// 清理缓存
135-
delCaches(resources.getId(), pid);
131+
delCaches(resources.getId(), oldPid, newPid);
136132
}
137133

138134
@Override
139135
@Transactional(rollbackFor = Exception.class)
140136
public void delete(Set<DeptDto> deptDtos) {
141137
for (DeptDto deptDto : deptDtos) {
142138
// 清理缓存
143-
delCaches(deptDto.getId(), deptDto.getPid());
139+
delCaches(deptDto.getId(), deptDto.getPid(), null);
144140
deptRepository.deleteById(deptDto.getId());
145-
if(deptDto.getPid() != null){
146-
updateSubCnt(deptDto.getPid());
147-
}
141+
updateSubCnt(deptDto.getPid());
148142
}
149143
}
150144

@@ -235,11 +229,6 @@ public Object buildTree(List<DeptDto> deptDtos) {
235229
return map;
236230
}
237231

238-
private void updateSubCnt(Long deptId){
239-
int count = deptRepository.countByPid(deptId);
240-
deptRepository.updateSubCntById(count, deptId);
241-
}
242-
243232
@Override
244233
public void verification(Set<DeptDto> deptDtos) {
245234
Set<Long> deptIds = deptDtos.stream().map(DeptDto::getId).collect(Collectors.toSet());
@@ -251,17 +240,25 @@ public void verification(Set<DeptDto> deptDtos) {
251240
}
252241
}
253242

243+
private void updateSubCnt(Long deptId){
244+
if(deptId != null){
245+
int count = deptRepository.countByPid(deptId);
246+
deptRepository.updateSubCntById(count, deptId);
247+
}
248+
}
249+
254250
/**
255251
* 清理缓存
256252
* @param id /
253+
* @param oldPid /
254+
* @param newPid /
257255
*/
258-
public void delCaches(Long id, Long pid){
256+
public void delCaches(Long id, Long oldPid, Long newPid){
259257
List<User> users = userRepository.findByDeptRoleId(id);
260258
// 删除数据权限
261259
redisUtils.delByKeys("data::user:",users.stream().map(User::getId).collect(Collectors.toSet()));
262260
redisUtils.del("dept::id:" + id);
263-
if (pid != null) {
264-
redisUtils.del("dept::pid:" + pid);
265-
}
261+
redisUtils.del("dept::pid:" + (oldPid == null ? 0 : oldPid));
262+
redisUtils.del("dept::pid:" + (newPid == null ? 0 : newPid));
266263
}
267264
}

eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MenuServiceImpl.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import cn.hutool.core.util.StrUtil;
2020
import lombok.RequiredArgsConstructor;
2121
import me.zhengjie.modules.system.domain.Menu;
22+
import me.zhengjie.modules.system.domain.Role;
2223
import me.zhengjie.modules.system.domain.User;
2324
import me.zhengjie.modules.system.domain.vo.MenuMetaVo;
2425
import me.zhengjie.modules.system.domain.vo.MenuVo;
@@ -125,10 +126,8 @@ public void create(Menu resources) {
125126
menuRepository.save(resources);
126127
// 计算子节点数目
127128
resources.setSubCount(0);
128-
if(resources.getPid() != null){
129-
// 清理缓存
130-
updateSubCnt(resources.getPid());
131-
}
129+
// 更新父节点菜单数目
130+
updateSubCnt(resources.getPid());
132131
redisUtils.del("menu::pid:" + (resources.getPid() == null ? 0 : resources.getPid()));
133132
}
134133

@@ -139,8 +138,6 @@ public void update(Menu resources) {
139138
throw new BadRequestException("上级不能为自己");
140139
}
141140
Menu menu = menuRepository.findById(resources.getId()).orElseGet(Menu::new);
142-
// 记录旧的父节点ID
143-
Long pid = menu.getPid();
144141
ValidationUtil.isNull(menu.getId(),"Permission","id",resources.getId());
145142

146143
if(resources.getIFrame()){
@@ -158,6 +155,11 @@ public void update(Menu resources) {
158155
if(resources.getPid().equals(0L)){
159156
resources.setPid(null);
160157
}
158+
159+
// 记录的父节点ID
160+
Long oldPid = menu.getPid();
161+
Long newPid = resources.getPid();
162+
161163
if(StringUtils.isNotBlank(resources.getComponentName())){
162164
menu1 = menuRepository.findByComponentName(resources.getComponentName());
163165
if(menu1 != null && !menu1.getId().equals(menu.getId())){
@@ -177,15 +179,11 @@ public void update(Menu resources) {
177179
menu.setPermission(resources.getPermission());
178180
menu.setType(resources.getType());
179181
menuRepository.save(menu);
180-
// 计算子节点数目
181-
if(resources.getPid() == null){
182-
updateSubCnt(pid);
183-
} else {
184-
pid = resources.getPid();
185-
updateSubCnt(resources.getPid());
186-
}
182+
// 计算父级菜单节点数目
183+
updateSubCnt(oldPid);
184+
updateSubCnt(newPid);
187185
// 清理缓存
188-
delCaches(resources.getId(), pid);
186+
delCaches(resources.getId(), oldPid, newPid);
189187
}
190188

191189
@Override
@@ -206,12 +204,10 @@ public Set<Menu> getDeleteMenus(List<Menu> menuList, Set<Menu> menuSet) {
206204
public void delete(Set<Menu> menuSet) {
207205
for (Menu menu : menuSet) {
208206
// 清理缓存
209-
delCaches(menu.getId(), menu.getPid());
207+
delCaches(menu.getId(), menu.getPid(), null);
210208
roleService.untiedMenu(menu.getId());
211209
menuRepository.deleteById(menu.getId());
212-
if(menu.getPid() != null){
213-
updateSubCnt(menu.getPid());
214-
}
210+
updateSubCnt(menu.getPid());
215211
}
216212
}
217213

@@ -336,19 +332,29 @@ public void download(List<MenuDto> menuDtos, HttpServletResponse response) throw
336332
}
337333

338334
private void updateSubCnt(Long menuId){
339-
int count = menuRepository.countByPid(menuId);
340-
menuRepository.updateSubCntById(count, menuId);
335+
if(menuId != null){
336+
int count = menuRepository.countByPid(menuId);
337+
menuRepository.updateSubCntById(count, menuId);
338+
}
341339
}
342340

343341
/**
344342
* 清理缓存
345343
* @param id 菜单ID
346-
* @param pid 菜单父级ID
344+
* @param oldPid 旧的菜单父级ID
345+
* @param newPid 新的菜单父级ID
347346
*/
348-
public void delCaches(Long id, Long pid){
347+
public void delCaches(Long id, Long oldPid, Long newPid){
349348
List<User> users = userRepository.findByMenuId(id);
350349
redisUtils.del("menu::id:" +id);
351350
redisUtils.delByKeys("menu::user:",users.stream().map(User::getId).collect(Collectors.toSet()));
352-
redisUtils.del("menu::pid:" + (pid == null ? 0 : pid));
351+
redisUtils.del("menu::pid:" + (oldPid == null ? 0 : oldPid));
352+
redisUtils.del("menu::pid:" + (newPid == null ? 0 : newPid));
353+
// 清除 Role 缓存
354+
List<Role> roles = roleService.findInMenuId(new ArrayList<Long>(){{
355+
add(id);
356+
add(newPid == null ? 0 : newPid);
357+
}});
358+
redisUtils.delByKeys("role::id:",roles.stream().map(Role::getId).collect(Collectors.toSet()));
353359
}
354360
}

eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,9 @@ public void verification(Set<Long> ids) {
210210
throw new BadRequestException("所选角色存在用户关联,请解除关联再试!");
211211
}
212212
}
213+
214+
@Override
215+
public List<Role> findInMenuId(List<Long> menuIds) {
216+
return roleRepository.findInMenuId(menuIds);
217+
}
213218
}

0 commit comments

Comments
 (0)