Skip to content

Commit

Permalink
管理员用户权限不再放在永久缓存,而是放在临时缓存(5分钟),由于访问频率较低,在加上涉及的业务太复杂,所以去除了之前对权限的永久缓存以及模…
Browse files Browse the repository at this point in the history
…糊删除用户权限缓存相关代码
  • Loading branch information
chopper711 committed Apr 26, 2022
1 parent cbd201c commit 0e0a8b0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
7 changes: 3 additions & 4 deletions framework/src/main/java/cn/lili/cache/CachePrefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,9 @@ public enum CachePrefix {
STORE_CATEGORY,
/**
* 用户菜单
*/
MENU_USER_ID,
/**
* 用户菜单
* <p>
* 这个缓存并非永久缓存,而是300秒缓存,也就是说修改用户关联的部门,关联的角色,
* 部门关联的角色,角色关联的菜单等等,最多需要5分钟才能生效
*/
USER_MENU,
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.UserEnums;
import cn.lili.common.vo.SearchVO;
import cn.lili.modules.permission.entity.dos.Menu;
import cn.lili.modules.permission.entity.dos.RoleMenu;
Expand Down Expand Up @@ -66,11 +67,12 @@ public List<MenuVO> findUserTree() {

@Override
public List<Menu> findUserList(String userId) {
String cacheKey = CachePrefix.MENU_USER_ID.getPrefix() + userId;
String cacheKey = CachePrefix.USER_MENU.getPrefix(UserEnums.MANAGER) + userId;
List<Menu> menuList = cache.get(cacheKey);
if (menuList == null) {
menuList = this.baseMapper.findByUserId(userId);
cache.put(cacheKey, menuList);
//每5分钟重新确认用户权限
cache.put(cacheKey, menuList, 300L);
}
return menuList;
}
Expand All @@ -84,8 +86,7 @@ public List<Menu> findUserList(String userId) {
@Override
public boolean saveOrUpdateMenu(Menu menu) {
if (CharSequenceUtil.isNotEmpty(menu.getId())) {
cache.vagueDel(CachePrefix.MENU_USER_ID.getPrefix());
cache.vagueDel(CachePrefix.USER_MENU.getPrefix());

}
return this.saveOrUpdate(menu);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public void updateRoleMenu(String roleId, List<RoleMenu> roleMenus) {
this.deleteRoleMenu(roleId);
//重新保存角色菜单关系
this.saveBatch(roleMenus);
cache.vagueDel(CachePrefix.MENU_USER_ID.getPrefix());
cache.vagueDel(CachePrefix.USER_MENU.getPrefix());

} catch (Exception e) {
log.error("修改用户权限错误", e);
}
Expand All @@ -72,8 +71,7 @@ public void deleteRoleMenu(String roleId) {
QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("role_id", roleId);
this.remove(queryWrapper);
cache.vagueDel(CachePrefix.MENU_USER_ID.getPrefix());
cache.vagueDel(CachePrefix.USER_MENU.getPrefix());

}

@Override
Expand All @@ -82,7 +80,6 @@ public void deleteRoleMenu(List<String> roleId) {
QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
queryWrapper.in("role_id", roleId);
this.remove(queryWrapper);
cache.vagueDel(CachePrefix.MENU_USER_ID.getPrefix());
cache.vagueDel(CachePrefix.USER_MENU.getPrefix());

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cn.lili.modules.system.token;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.enums.PermissionEnum;
import cn.lili.common.security.enums.UserEnums;
Expand All @@ -11,7 +9,6 @@
import cn.lili.common.security.token.base.AbstractTokenGenerate;
import cn.lili.modules.permission.entity.dos.AdminUser;
import cn.lili.modules.permission.entity.vo.UserMenuVO;
import cn.lili.modules.permission.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -32,21 +29,10 @@ public class ManagerTokenGenerate extends AbstractTokenGenerate<AdminUser> {

@Autowired
private TokenUtil tokenUtil;
@Autowired
private RoleMenuService roleMenuService;
@Autowired
private Cache cache;


@Override
public Token createToken(AdminUser adminUser, Boolean longTerm) {
AuthUser authUser = new AuthUser(adminUser.getUsername(), adminUser.getId(), adminUser.getAvatar(), UserEnums.MANAGER, adminUser.getNickName(), adminUser.getIsSuper());


List<UserMenuVO> userMenuVOList = roleMenuService.findAllMenu(authUser.getId());
//缓存权限列表
cache.put(CachePrefix.PERMISSION_LIST.getPrefix(UserEnums.MANAGER) + authUser.getId(), this.permissionList(userMenuVOList));

return tokenUtil.createToken(adminUser.getUsername(), authUser, longTerm, UserEnums.MANAGER);
}

Expand Down

0 comments on commit 0e0a8b0

Please sign in to comment.