refactor: 优化系统模块缓存使用
This commit is contained in:
parent
60cce85763
commit
79f56796d8
@ -780,11 +780,6 @@ public class RedisUtils {
|
||||
keys.addAll(redisTemplate.keys(new StringBuffer(prefix).append(id).toString()));
|
||||
}
|
||||
long count = redisTemplate.delete(keys);
|
||||
// 此处提示可自行删除
|
||||
log.debug("--------------------------------------------");
|
||||
log.debug("成功删除缓存:" + keys.toString());
|
||||
log.debug("缓存删除数量:" + count + "个");
|
||||
log.debug("--------------------------------------------");
|
||||
}
|
||||
|
||||
// ============================incr=============================
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package me.zhengjie.modules.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.modules.system.domain.Dept;
|
||||
import me.zhengjie.modules.system.domain.Role;
|
||||
@ -22,11 +23,12 @@ import me.zhengjie.modules.system.domain.User;
|
||||
import me.zhengjie.modules.system.service.DataService;
|
||||
import me.zhengjie.modules.system.service.DeptService;
|
||||
import me.zhengjie.modules.system.service.RoleService;
|
||||
import me.zhengjie.utils.CacheKey;
|
||||
import me.zhengjie.utils.RedisUtils;
|
||||
import me.zhengjie.utils.enums.DataScopeEnum;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
@ -35,9 +37,9 @@ import java.util.*;
|
||||
**/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@CacheConfig(cacheNames = "data", keyGenerator = "keyGenerator")
|
||||
public class DataServiceImpl implements DataService {
|
||||
|
||||
private final RedisUtils redisUtils;
|
||||
private final RoleService roleService;
|
||||
private final DeptService deptService;
|
||||
|
||||
@ -47,27 +49,31 @@ public class DataServiceImpl implements DataService {
|
||||
* @return /
|
||||
*/
|
||||
@Override
|
||||
@Cacheable(key = "'user:' + #p0.id")
|
||||
public List<Long> getDeptIds(User user) {
|
||||
// 用于存储部门id
|
||||
Set<Long> deptIds = new HashSet<>();
|
||||
// 查询用户角色
|
||||
List<Role> roleList = roleService.findByUsersId(user.getId());
|
||||
// 获取对应的部门ID
|
||||
for (Role role : roleList) {
|
||||
DataScopeEnum dataScopeEnum = DataScopeEnum.find(role.getDataScope());
|
||||
switch (Objects.requireNonNull(dataScopeEnum)) {
|
||||
case THIS_LEVEL:
|
||||
deptIds.add(user.getDept().getId());
|
||||
break;
|
||||
case CUSTOMIZE:
|
||||
deptIds.addAll(getCustomize(deptIds, role));
|
||||
break;
|
||||
default:
|
||||
return new ArrayList<>();
|
||||
String key = CacheKey.DATA_USER + user.getId();
|
||||
List<Long> ids = redisUtils.getList(key, Long.class);
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
Set<Long> deptIds = new HashSet<>();
|
||||
// 查询用户角色
|
||||
List<Role> roleList = roleService.findByUsersId(user.getId());
|
||||
// 获取对应的部门ID
|
||||
for (Role role : roleList) {
|
||||
DataScopeEnum dataScopeEnum = DataScopeEnum.find(role.getDataScope());
|
||||
switch (Objects.requireNonNull(dataScopeEnum)) {
|
||||
case THIS_LEVEL:
|
||||
deptIds.add(user.getDept().getId());
|
||||
break;
|
||||
case CUSTOMIZE:
|
||||
deptIds.addAll(getCustomize(deptIds, role));
|
||||
break;
|
||||
default:
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
ids = new ArrayList<>(deptIds);
|
||||
redisUtils.set(key, ids, 1, TimeUnit.DAYS);
|
||||
}
|
||||
return new ArrayList<>(deptIds);
|
||||
return ids;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -81,7 +87,7 @@ public class DataServiceImpl implements DataService {
|
||||
for (Dept dept : depts) {
|
||||
deptIds.add(dept.getId());
|
||||
List<Dept> deptChildren = deptService.findByPid(dept.getId());
|
||||
if (deptChildren != null && deptChildren.size() != 0) {
|
||||
if (CollUtil.isNotEmpty(deptChildren)) {
|
||||
deptIds.addAll(deptService.getDeptChildren(deptChildren));
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package me.zhengjie.modules.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@ -29,14 +30,13 @@ import me.zhengjie.utils.*;
|
||||
import me.zhengjie.modules.system.mapper.DeptMapper;
|
||||
import me.zhengjie.modules.system.service.DeptService;
|
||||
import me.zhengjie.utils.enums.DataScopeEnum;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -45,7 +45,6 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@CacheConfig(cacheNames = "dept", keyGenerator = "keyGenerator")
|
||||
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
|
||||
|
||||
private final DeptMapper deptMapper;
|
||||
@ -86,9 +85,14 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements De
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "'id:' + #p0")
|
||||
public Dept findById(Long id) {
|
||||
return getById(id);
|
||||
String key = CacheKey.DEPT_ID + id;
|
||||
Dept dept = redisUtils.get(key, Dept.class);
|
||||
if(dept == null){
|
||||
dept = deptMapper.selectById(id);
|
||||
redisUtils.set(key, dept, 1, TimeUnit.DAYS);
|
||||
}
|
||||
return dept;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -159,7 +163,7 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements De
|
||||
for (Dept dept : menuList) {
|
||||
deptSet.add(dept);
|
||||
List<Dept> depts = deptMapper.findByPid(dept.getId());
|
||||
if(depts!=null && depts.size()!=0){
|
||||
if(CollUtil.isNotEmpty(depts)){
|
||||
getDeleteDepts(depts, deptSet);
|
||||
}
|
||||
}
|
||||
@ -172,7 +176,7 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements De
|
||||
deptList.forEach(dept -> {
|
||||
if (dept!=null && dept.getEnabled()) {
|
||||
List<Dept> depts = deptMapper.findByPid(dept.getId());
|
||||
if (depts.size() != 0) {
|
||||
if (CollUtil.isNotEmpty(depts)) {
|
||||
list.addAll(getDeptChildren(depts));
|
||||
}
|
||||
list.add(dept.getId());
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package me.zhengjie.modules.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -25,11 +26,10 @@ import me.zhengjie.modules.system.domain.dto.DictDetailQueryCriteria;
|
||||
import me.zhengjie.utils.*;
|
||||
import me.zhengjie.modules.system.mapper.DictDetailMapper;
|
||||
import me.zhengjie.modules.system.service.DictDetailService;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
@ -37,7 +37,6 @@ import java.util.List;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@CacheConfig(cacheNames = "dict", keyGenerator = "keyGenerator")
|
||||
public class DictDetailServiceImpl extends ServiceImpl<DictDetailMapper, DictDetail> implements DictDetailService {
|
||||
|
||||
private final DictMapper dictMapper;
|
||||
@ -70,9 +69,14 @@ public class DictDetailServiceImpl extends ServiceImpl<DictDetailMapper, DictDet
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "'name:' + #p0")
|
||||
public List<DictDetail> getDictByName(String name) {
|
||||
return dictDetailMapper.findByDictName(name);
|
||||
String key = CacheKey.DICT_NAME + name;
|
||||
List<DictDetail> dictDetails = redisUtils.getList(key, DictDetail.class);
|
||||
if(CollUtil.isEmpty(dictDetails)){
|
||||
dictDetails = dictDetailMapper.findByDictName(name);
|
||||
redisUtils.set(key, dictDetails, 1 , TimeUnit.DAYS);
|
||||
}
|
||||
return dictDetails;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -27,7 +27,6 @@ import me.zhengjie.modules.system.domain.dto.DictQueryCriteria;
|
||||
import me.zhengjie.utils.*;
|
||||
import me.zhengjie.modules.system.mapper.DictMapper;
|
||||
import me.zhengjie.modules.system.service.DictService;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@ -40,7 +39,6 @@ import java.util.*;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@CacheConfig(cacheNames = "dict", keyGenerator = "keyGenerator")
|
||||
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
|
||||
|
||||
private final DictMapper dictMapper;
|
||||
|
@ -20,20 +20,20 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.exception.EntityExistException;
|
||||
import me.zhengjie.modules.system.domain.Dict;
|
||||
import me.zhengjie.modules.system.domain.Job;
|
||||
import me.zhengjie.modules.system.mapper.UserMapper;
|
||||
import me.zhengjie.modules.system.domain.dto.JobQueryCriteria;
|
||||
import me.zhengjie.utils.*;
|
||||
import me.zhengjie.modules.system.mapper.JobMapper;
|
||||
import me.zhengjie.modules.system.service.JobService;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
@ -41,7 +41,6 @@ import java.util.*;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@CacheConfig(cacheNames = "job", keyGenerator = "keyGenerator")
|
||||
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobService {
|
||||
|
||||
private final JobMapper jobMapper;
|
||||
@ -49,7 +48,6 @@ public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobSe
|
||||
private final UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
@Cacheable
|
||||
public PageResult<Job> queryAll(JobQueryCriteria criteria, Page<Object> page) {
|
||||
return PageUtil.toPage(jobMapper.findAll(criteria, page));
|
||||
}
|
||||
@ -60,9 +58,14 @@ public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobSe
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "'id:' + #p0")
|
||||
public Job findById(Long id) {
|
||||
return getById(id);
|
||||
String key = CacheKey.JOB_ID + id;
|
||||
Job job = redisUtils.get(key, Job.class);
|
||||
if(job == null){
|
||||
job = getById(id);
|
||||
redisUtils.set(key, job, 1, TimeUnit.DAYS);
|
||||
}
|
||||
return job;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -76,7 +79,6 @@ public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobSe
|
||||
}
|
||||
|
||||
@Override
|
||||
@CacheEvict(key = "'id:' + #p0.id")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(Job resources) {
|
||||
Job job = getById(resources.getId());
|
||||
@ -86,6 +88,8 @@ public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobSe
|
||||
}
|
||||
resources.setId(job.getId());
|
||||
saveOrUpdate(resources);
|
||||
// 删除缓存
|
||||
delCaches(resources.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -93,7 +97,7 @@ public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobSe
|
||||
public void delete(Set<Long> ids) {
|
||||
removeBatchByIds(ids);
|
||||
// 删除缓存
|
||||
redisUtils.delByKeys(CacheKey.JOB_ID, ids);
|
||||
ids.forEach(this::delCaches);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -115,4 +119,8 @@ public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobSe
|
||||
throw new BadRequestException("所选的岗位中存在用户关联,请解除关联再试!");
|
||||
}
|
||||
}
|
||||
|
||||
public void delCaches(Long id){
|
||||
redisUtils.del(CacheKey.JOB_ID + id);
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package me.zhengjie.modules.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@ -33,14 +34,13 @@ import me.zhengjie.modules.system.service.MenuService;
|
||||
import me.zhengjie.modules.system.service.RoleService;
|
||||
import me.zhengjie.modules.system.domain.dto.MenuQueryCriteria;
|
||||
import me.zhengjie.utils.*;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -48,7 +48,6 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@CacheConfig(cacheNames = "menu", keyGenerator = "keyGenerator")
|
||||
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
|
||||
|
||||
private final MenuMapper menuMapper;
|
||||
@ -86,9 +85,14 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "'id:' + #p0")
|
||||
public Menu findById(long id) {
|
||||
return getById(id);
|
||||
String key = CacheKey.MENU_ID + id;
|
||||
Menu menu = redisUtils.get(key, Menu.class);
|
||||
if(menu == null){
|
||||
menu = getById(id);
|
||||
redisUtils.set(key, menu, 1, TimeUnit.DAYS);
|
||||
}
|
||||
return menu;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -97,12 +101,16 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
||||
* @return /
|
||||
*/
|
||||
@Override
|
||||
@Cacheable(key = "'user:' + #p0")
|
||||
public List<Menu> findByUser(Long currentUserId) {
|
||||
List<Role> roles = roleService.findByUsersId(currentUserId);
|
||||
Set<Long> roleIds = roles.stream().map(Role::getId).collect(Collectors.toSet());
|
||||
LinkedHashSet<Menu> menus = menuMapper.findByRoleIdsAndTypeNot(roleIds, 2);
|
||||
return new ArrayList<>(menus);
|
||||
String key = CacheKey.MENU_USER + currentUserId;
|
||||
List<Menu> menus = redisUtils.getList(key, Menu.class);
|
||||
if (CollUtil.isEmpty(menus)){
|
||||
List<Role> roles = roleService.findByUsersId(currentUserId);
|
||||
Set<Long> roleIds = roles.stream().map(Role::getId).collect(Collectors.toSet());
|
||||
menus = new ArrayList<>(menuMapper.findByRoleIdsAndTypeNot(roleIds, 2));
|
||||
redisUtils.set(key, menus, 1, TimeUnit.DAYS);
|
||||
}
|
||||
return menus;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -188,7 +196,7 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
||||
for (Menu menu : menuList) {
|
||||
menuSet.add(menu);
|
||||
List<Menu> menus = menuMapper.findByPidOrderByMenuSort(menu.getId());
|
||||
if(menus!=null && menus.size()!=0){
|
||||
if(CollUtil.isNotEmpty(menus)){
|
||||
getChildMenus(menus, menuSet);
|
||||
}
|
||||
}
|
||||
@ -246,7 +254,7 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
||||
}
|
||||
}
|
||||
}
|
||||
if(trees.size() == 0){
|
||||
if(CollUtil.isNotEmpty(trees)){
|
||||
trees = menus.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
|
||||
}
|
||||
return trees;
|
||||
@ -281,16 +289,7 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
||||
menuVo.setChildren(buildMenus(menuList));
|
||||
// 处理是一级菜单并且没有子菜单的情况
|
||||
} else if(menu.getPid() == null){
|
||||
MenuVo menuVo1 = new MenuVo();
|
||||
menuVo1.setMeta(menuVo.getMeta());
|
||||
// 非外链
|
||||
if(!menu.getIFrame()){
|
||||
menuVo1.setPath("index");
|
||||
menuVo1.setName(menuVo.getName());
|
||||
menuVo1.setComponent(menuVo.getComponent());
|
||||
} else {
|
||||
menuVo1.setPath(menu.getPath());
|
||||
}
|
||||
MenuVo menuVo1 = getMenuVo(menu, menuVo);
|
||||
menuVo.setName(null);
|
||||
menuVo.setMeta(null);
|
||||
menuVo.setComponent("Layout");
|
||||
@ -341,4 +340,24 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
||||
List<Role> roles = roleService.findByMenuId(id);
|
||||
redisUtils.delByKeys(CacheKey.ROLE_ID, roles.stream().map(Role::getId).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 MenuVo
|
||||
* @param menu /
|
||||
* @param menuVo /
|
||||
* @return /
|
||||
*/
|
||||
private static MenuVo getMenuVo(Menu menu, MenuVo menuVo) {
|
||||
MenuVo menuVo1 = new MenuVo();
|
||||
menuVo1.setMeta(menuVo.getMeta());
|
||||
// 非外链
|
||||
if(!menu.getIFrame()){
|
||||
menuVo1.setPath("index");
|
||||
menuVo1.setName(menuVo.getName());
|
||||
menuVo1.setComponent(menuVo.getComponent());
|
||||
} else {
|
||||
menuVo1.setPath(menu.getPath());
|
||||
}
|
||||
return menuVo1;
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ package me.zhengjie.modules.system.service.impl;
|
||||
|
||||
import cn.hutool.core.date.BetweenFormatter.Level;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.modules.system.service.MonitorService;
|
||||
import me.zhengjie.utils.ElConstant;
|
||||
import me.zhengjie.utils.FileUtil;
|
||||
@ -37,6 +38,7 @@ import java.util.*;
|
||||
* @author Zheng Jie
|
||||
* @date 2020-05-02
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class MonitorServiceImpl implements MonitorService {
|
||||
|
||||
@ -61,7 +63,7 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
resultMap.put("disk", getDiskInfo(os));
|
||||
resultMap.put("time", DateUtil.format(new Date(), "HH:mm:ss"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
@ -35,13 +35,12 @@ import me.zhengjie.modules.system.mapper.UserMapper;
|
||||
import me.zhengjie.modules.system.service.RoleService;
|
||||
import me.zhengjie.modules.system.domain.dto.RoleQueryCriteria;
|
||||
import me.zhengjie.utils.*;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -50,7 +49,6 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@CacheConfig(cacheNames = "role", keyGenerator = "keyGenerator")
|
||||
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
|
||||
|
||||
private final RoleMapper roleMapper;
|
||||
@ -77,9 +75,14 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "'id:' + #p0")
|
||||
public Role findById(long id) {
|
||||
return roleMapper.findById(id);
|
||||
String key = CacheKey.ROLE_ID + id;
|
||||
Role role = redisUtils.get(key, Role.class);
|
||||
if (role == null) {
|
||||
role = roleMapper.findById(id);
|
||||
redisUtils.set(key, role, 1, TimeUnit.DAYS);
|
||||
}
|
||||
return role;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -164,21 +167,26 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "'auth:' + #p0.id")
|
||||
public List<AuthorityDto> buildAuthorities(User user) {
|
||||
Set<String> permissions = new HashSet<>();
|
||||
// 如果是管理员直接返回
|
||||
if (user.getIsAdmin()) {
|
||||
permissions.add("admin");
|
||||
return permissions.stream().map(AuthorityDto::new)
|
||||
String key = CacheKey.ROLE_AUTH + user.getId();
|
||||
List<AuthorityDto> authorityDtos = redisUtils.getList(key, AuthorityDto.class);
|
||||
if (CollUtil.isEmpty(authorityDtos)) {
|
||||
Set<String> permissions = new HashSet<>();
|
||||
// 如果是管理员直接返回
|
||||
if (user.getIsAdmin()) {
|
||||
permissions.add("admin");
|
||||
return permissions.stream().map(AuthorityDto::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
List<Role> roles = roleMapper.findByUserId(user.getId());
|
||||
permissions = roles.stream().flatMap(role -> role.getMenus().stream())
|
||||
.map(Menu::getPermission)
|
||||
.filter(StringUtils::isNotBlank).collect(Collectors.toSet());
|
||||
authorityDtos = permissions.stream().map(AuthorityDto::new)
|
||||
.collect(Collectors.toList());
|
||||
redisUtils.set(key, authorityDtos, 1, TimeUnit.HOURS);
|
||||
}
|
||||
List<Role> roles = roleMapper.findByUserId(user.getId());
|
||||
permissions = roles.stream().flatMap(role -> role.getMenus().stream())
|
||||
.map(Menu::getPermission)
|
||||
.filter(StringUtils::isNotBlank).collect(Collectors.toSet());
|
||||
return permissions.stream().map(AuthorityDto::new)
|
||||
.collect(Collectors.toList());
|
||||
return authorityDtos;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -33,8 +33,6 @@ import me.zhengjie.modules.system.mapper.UserMapper;
|
||||
import me.zhengjie.modules.system.mapper.UserRoleMapper;
|
||||
import me.zhengjie.modules.system.service.UserService;
|
||||
import me.zhengjie.utils.*;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@ -43,6 +41,7 @@ import javax.validation.constraints.NotBlank;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -51,7 +50,6 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@CacheConfig(cacheNames = "user", keyGenerator = "keyGenerator")
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
|
||||
|
||||
private final UserMapper userMapper;
|
||||
@ -74,10 +72,15 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "'id:' + #p0")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public User findById(long id) {
|
||||
return getById(id);
|
||||
String key = CacheKey.USER_ID + id;
|
||||
User user = redisUtils.get(key, User.class);
|
||||
if (user == null) {
|
||||
user = getById(id);
|
||||
redisUtils.set(key, user, 1, TimeUnit.DAYS);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user