mirror of https://github.com/elunez/eladmin
refactor: 移除缓存注解,优化Redis缓存使用
parent
fe8d271557
commit
10b43563aa
|
@ -103,6 +103,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateIsPause(QuartzJob quartzJob) {
|
public void updateIsPause(QuartzJob quartzJob) {
|
||||||
|
// 置换暂停状态
|
||||||
if (quartzJob.getIsPause()) {
|
if (quartzJob.getIsPause()) {
|
||||||
quartzManage.resumeJob(quartzJob);
|
quartzManage.resumeJob(quartzJob);
|
||||||
quartzJob.setIsPause(false);
|
quartzJob.setIsPause(false);
|
||||||
|
|
|
@ -23,23 +23,23 @@ import me.zhengjie.modules.system.service.DeptService;
|
||||||
import me.zhengjie.modules.system.service.RoleService;
|
import me.zhengjie.modules.system.service.RoleService;
|
||||||
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
|
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
|
||||||
import me.zhengjie.modules.system.service.dto.UserDto;
|
import me.zhengjie.modules.system.service.dto.UserDto;
|
||||||
|
import me.zhengjie.utils.CacheKey;
|
||||||
|
import me.zhengjie.utils.RedisUtils;
|
||||||
import me.zhengjie.utils.enums.DataScopeEnum;
|
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.stereotype.Service;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Zheng Jie
|
* @author Zheng Jie
|
||||||
* @website https://eladmin.vip
|
|
||||||
* @description 数据权限服务实现
|
* @description 数据权限服务实现
|
||||||
* @date 2020-05-07
|
* @date 2020-05-07
|
||||||
**/
|
**/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@CacheConfig(cacheNames = "data", keyGenerator = "keyGenerator")
|
|
||||||
public class DataServiceImpl implements DataService {
|
public class DataServiceImpl implements DataService {
|
||||||
|
|
||||||
|
private final RedisUtils redisUtils;
|
||||||
private final RoleService roleService;
|
private final RoleService roleService;
|
||||||
private final DeptService deptService;
|
private final DeptService deptService;
|
||||||
|
|
||||||
|
@ -49,27 +49,32 @@ public class DataServiceImpl implements DataService {
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'user:' + #p0.id")
|
|
||||||
public List<Long> getDeptIds(UserDto user) {
|
public List<Long> getDeptIds(UserDto user) {
|
||||||
// 用于存储部门id
|
String key = CacheKey.DATA_USER + user.getId();
|
||||||
Set<Long> deptIds = new HashSet<>();
|
List<Long> ids = redisUtils.getList(key, Long.class);
|
||||||
// 查询用户角色
|
if (CollUtil.isEmpty(ids)) {
|
||||||
List<RoleSmallDto> roleSet = roleService.findByUsersId(user.getId());
|
// 用于存储部门id
|
||||||
// 获取对应的部门ID
|
Set<Long> deptIds = new HashSet<>();
|
||||||
for (RoleSmallDto role : roleSet) {
|
// 查询用户角色
|
||||||
DataScopeEnum dataScopeEnum = DataScopeEnum.find(role.getDataScope());
|
List<RoleSmallDto> roleSet = roleService.findByUsersId(user.getId());
|
||||||
switch (Objects.requireNonNull(dataScopeEnum)) {
|
// 获取对应的部门ID
|
||||||
case THIS_LEVEL:
|
for (RoleSmallDto role : roleSet) {
|
||||||
deptIds.add(user.getDept().getId());
|
DataScopeEnum dataScopeEnum = DataScopeEnum.find(role.getDataScope());
|
||||||
break;
|
switch (Objects.requireNonNull(dataScopeEnum)) {
|
||||||
case CUSTOMIZE:
|
case THIS_LEVEL:
|
||||||
deptIds.addAll(getCustomize(deptIds, role));
|
deptIds.add(user.getDept().getId());
|
||||||
break;
|
break;
|
||||||
default:
|
case CUSTOMIZE:
|
||||||
return new ArrayList<>();
|
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 new ArrayList<>(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package me.zhengjie.modules.system.service.impl;
|
package me.zhengjie.modules.system.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
@ -30,8 +31,6 @@ import me.zhengjie.modules.system.repository.DeptRepository;
|
||||||
import me.zhengjie.modules.system.service.DeptService;
|
import me.zhengjie.modules.system.service.DeptService;
|
||||||
import me.zhengjie.modules.system.service.mapstruct.DeptMapper;
|
import me.zhengjie.modules.system.service.mapstruct.DeptMapper;
|
||||||
import me.zhengjie.utils.enums.DataScopeEnum;
|
import me.zhengjie.utils.enums.DataScopeEnum;
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
@ -39,6 +38,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,7 +47,6 @@ import java.util.stream.Collectors;
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@CacheConfig(cacheNames = "dept", keyGenerator = "keyGenerator")
|
|
||||||
public class DeptServiceImpl implements DeptService {
|
public class DeptServiceImpl implements DeptService {
|
||||||
|
|
||||||
private final DeptRepository deptRepository;
|
private final DeptRepository deptRepository;
|
||||||
|
@ -88,10 +87,14 @@ public class DeptServiceImpl implements DeptService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'id:' + #p0")
|
|
||||||
public DeptDto findById(Long id) {
|
public DeptDto findById(Long id) {
|
||||||
Dept dept = deptRepository.findById(id).orElseGet(Dept::new);
|
String key = CacheKey.DEPT_ID + id;
|
||||||
ValidationUtil.isNull(dept.getId(),"Dept","id",id);
|
Dept dept = redisUtils.get(key, Dept.class);
|
||||||
|
if(dept == null){
|
||||||
|
dept = deptRepository.findById(id).orElseGet(Dept::new);
|
||||||
|
ValidationUtil.isNull(dept.getId(),"Dept","id",id);
|
||||||
|
redisUtils.set(key, dept, 1, TimeUnit.DAYS);
|
||||||
|
}
|
||||||
return deptMapper.toDto(dept);
|
return deptMapper.toDto(dept);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,7 +169,7 @@ public class DeptServiceImpl implements DeptService {
|
||||||
for (Dept dept : menuList) {
|
for (Dept dept : menuList) {
|
||||||
deptDtos.add(deptMapper.toDto(dept));
|
deptDtos.add(deptMapper.toDto(dept));
|
||||||
List<Dept> depts = deptRepository.findByPid(dept.getId());
|
List<Dept> depts = deptRepository.findByPid(dept.getId());
|
||||||
if(depts!=null && depts.size()!=0){
|
if(CollUtil.isNotEmpty(depts)){
|
||||||
getDeleteDepts(depts, deptDtos);
|
getDeleteDepts(depts, deptDtos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,7 +182,7 @@ public class DeptServiceImpl implements DeptService {
|
||||||
deptList.forEach(dept -> {
|
deptList.forEach(dept -> {
|
||||||
if (dept!=null && dept.getEnabled()) {
|
if (dept!=null && dept.getEnabled()) {
|
||||||
List<Dept> depts = deptRepository.findByPid(dept.getId());
|
List<Dept> depts = deptRepository.findByPid(dept.getId());
|
||||||
if (depts.size() != 0) {
|
if (CollUtil.isNotEmpty(depts)) {
|
||||||
list.addAll(getDeptChildren(depts));
|
list.addAll(getDeptChildren(depts));
|
||||||
}
|
}
|
||||||
list.add(dept.getId());
|
list.add(dept.getId());
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package me.zhengjie.modules.system.service.impl;
|
package me.zhengjie.modules.system.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import me.zhengjie.utils.PageResult;
|
import me.zhengjie.utils.PageResult;
|
||||||
import me.zhengjie.modules.system.domain.Dict;
|
import me.zhengjie.modules.system.domain.Dict;
|
||||||
|
@ -26,13 +27,12 @@ import me.zhengjie.modules.system.repository.DictDetailRepository;
|
||||||
import me.zhengjie.modules.system.service.DictDetailService;
|
import me.zhengjie.modules.system.service.DictDetailService;
|
||||||
import me.zhengjie.modules.system.service.dto.DictDetailDto;
|
import me.zhengjie.modules.system.service.dto.DictDetailDto;
|
||||||
import me.zhengjie.modules.system.service.mapstruct.DictDetailMapper;
|
import me.zhengjie.modules.system.service.mapstruct.DictDetailMapper;
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Zheng Jie
|
* @author Zheng Jie
|
||||||
|
@ -40,7 +40,6 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@CacheConfig(cacheNames = "dict", keyGenerator = "keyGenerator")
|
|
||||||
public class DictDetailServiceImpl implements DictDetailService {
|
public class DictDetailServiceImpl implements DictDetailService {
|
||||||
|
|
||||||
private final DictRepository dictRepository;
|
private final DictRepository dictRepository;
|
||||||
|
@ -74,9 +73,14 @@ public class DictDetailServiceImpl implements DictDetailService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'name:' + #p0")
|
|
||||||
public List<DictDetailDto> getDictByName(String name) {
|
public List<DictDetailDto> getDictByName(String name) {
|
||||||
return dictDetailMapper.toDto(dictDetailRepository.findByDictName(name));
|
String key = CacheKey.DICT_NAME + name;
|
||||||
|
List<DictDetail> dictDetails = redisUtils.getList(key, DictDetail.class);
|
||||||
|
if(CollUtil.isEmpty(dictDetails)){
|
||||||
|
dictDetails = dictDetailRepository.findByDictName(name);
|
||||||
|
redisUtils.set(key, dictDetails, 1 , TimeUnit.DAYS);
|
||||||
|
}
|
||||||
|
return dictDetailMapper.toDto(dictDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -26,7 +26,6 @@ import me.zhengjie.modules.system.repository.DictRepository;
|
||||||
import me.zhengjie.modules.system.service.DictService;
|
import me.zhengjie.modules.system.service.DictService;
|
||||||
import me.zhengjie.modules.system.service.dto.DictDto;
|
import me.zhengjie.modules.system.service.dto.DictDto;
|
||||||
import me.zhengjie.modules.system.service.mapstruct.DictMapper;
|
import me.zhengjie.modules.system.service.mapstruct.DictMapper;
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -41,7 +40,6 @@ import java.util.*;
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@CacheConfig(cacheNames = "dict", keyGenerator = "keyGenerator")
|
|
||||||
public class DictServiceImpl implements DictService {
|
public class DictServiceImpl implements DictService {
|
||||||
|
|
||||||
private final DictRepository dictRepository;
|
private final DictRepository dictRepository;
|
||||||
|
|
|
@ -27,9 +27,6 @@ import me.zhengjie.modules.system.repository.JobRepository;
|
||||||
import me.zhengjie.modules.system.service.JobService;
|
import me.zhengjie.modules.system.service.JobService;
|
||||||
import me.zhengjie.modules.system.service.dto.JobDto;
|
import me.zhengjie.modules.system.service.dto.JobDto;
|
||||||
import me.zhengjie.modules.system.service.mapstruct.JobMapper;
|
import me.zhengjie.modules.system.service.mapstruct.JobMapper;
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
|
||||||
import org.springframework.cache.annotation.CacheEvict;
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -37,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Zheng Jie
|
* @author Zheng Jie
|
||||||
|
@ -44,7 +42,6 @@ import java.util.*;
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@CacheConfig(cacheNames = "job", keyGenerator = "keyGenerator")
|
|
||||||
public class JobServiceImpl implements JobService {
|
public class JobServiceImpl implements JobService {
|
||||||
|
|
||||||
private final JobRepository jobRepository;
|
private final JobRepository jobRepository;
|
||||||
|
@ -65,10 +62,14 @@ public class JobServiceImpl implements JobService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'id:' + #p0")
|
|
||||||
public JobDto findById(Long id) {
|
public JobDto findById(Long id) {
|
||||||
Job job = jobRepository.findById(id).orElseGet(Job::new);
|
String key = CacheKey.JOB_ID + id;
|
||||||
ValidationUtil.isNull(job.getId(),"Job","id",id);
|
Job job = redisUtils.get(key, Job.class);
|
||||||
|
if(job == null){
|
||||||
|
job = jobRepository.findById(id).orElseGet(Job::new);
|
||||||
|
ValidationUtil.isNull(job.getId(),"Job","id",id);
|
||||||
|
redisUtils.set(key, job, 1, TimeUnit.DAYS);
|
||||||
|
}
|
||||||
return jobMapper.toDto(job);
|
return jobMapper.toDto(job);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +84,6 @@ public class JobServiceImpl implements JobService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@CacheEvict(key = "'id:' + #p0.id")
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void update(Job resources) {
|
public void update(Job resources) {
|
||||||
Job job = jobRepository.findById(resources.getId()).orElseGet(Job::new);
|
Job job = jobRepository.findById(resources.getId()).orElseGet(Job::new);
|
||||||
|
@ -94,6 +94,8 @@ public class JobServiceImpl implements JobService {
|
||||||
ValidationUtil.isNull( job.getId(),"Job","id",resources.getId());
|
ValidationUtil.isNull( job.getId(),"Job","id",resources.getId());
|
||||||
resources.setId(job.getId());
|
resources.setId(job.getId());
|
||||||
jobRepository.save(resources);
|
jobRepository.save(resources);
|
||||||
|
// 删除缓存
|
||||||
|
delCaches(resources.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -123,4 +125,12 @@ public class JobServiceImpl implements JobService {
|
||||||
throw new BadRequestException("所选的岗位中存在用户关联,请解除关联再试!");
|
throw new BadRequestException("所选的岗位中存在用户关联,请解除关联再试!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除缓存
|
||||||
|
* @param id /
|
||||||
|
*/
|
||||||
|
public void delCaches(Long id){
|
||||||
|
redisUtils.del(CacheKey.JOB_ID + id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package me.zhengjie.modules.system.service.impl;
|
package me.zhengjie.modules.system.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
@ -34,8 +35,6 @@ import me.zhengjie.modules.system.service.dto.MenuQueryCriteria;
|
||||||
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
|
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
|
||||||
import me.zhengjie.modules.system.service.mapstruct.MenuMapper;
|
import me.zhengjie.modules.system.service.mapstruct.MenuMapper;
|
||||||
import me.zhengjie.utils.*;
|
import me.zhengjie.utils.*;
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
@ -43,6 +42,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,7 +50,6 @@ import java.util.stream.Collectors;
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@CacheConfig(cacheNames = "menu", keyGenerator = "keyGenerator")
|
|
||||||
public class MenuServiceImpl implements MenuService {
|
public class MenuServiceImpl implements MenuService {
|
||||||
|
|
||||||
private final MenuRepository menuRepository;
|
private final MenuRepository menuRepository;
|
||||||
|
@ -88,10 +87,14 @@ public class MenuServiceImpl implements MenuService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'id:' + #p0")
|
|
||||||
public MenuDto findById(long id) {
|
public MenuDto findById(long id) {
|
||||||
Menu menu = menuRepository.findById(id).orElseGet(Menu::new);
|
String key = CacheKey.MENU_ID + id;
|
||||||
ValidationUtil.isNull(menu.getId(),"Menu","id",id);
|
Menu menu = redisUtils.get(key, Menu.class);
|
||||||
|
if(menu == null){
|
||||||
|
menu = menuRepository.findById(id).orElseGet(Menu::new);
|
||||||
|
ValidationUtil.isNull(menu.getId(),"Menu","id",id);
|
||||||
|
redisUtils.set(key, menu, 1, TimeUnit.DAYS);
|
||||||
|
}
|
||||||
return menuMapper.toDto(menu);
|
return menuMapper.toDto(menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,11 +104,16 @@ public class MenuServiceImpl implements MenuService {
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'user:' + #p0")
|
|
||||||
public List<MenuDto> findByUser(Long currentUserId) {
|
public List<MenuDto> findByUser(Long currentUserId) {
|
||||||
List<RoleSmallDto> roles = roleService.findByUsersId(currentUserId);
|
String key = CacheKey.MENU_USER + currentUserId;
|
||||||
Set<Long> roleIds = roles.stream().map(RoleSmallDto::getId).collect(Collectors.toSet());
|
List<Menu> menus = redisUtils.getList(key, Menu.class);
|
||||||
LinkedHashSet<Menu> menus = menuRepository.findByRoleIdsAndTypeNot(roleIds, 2);
|
if (CollUtil.isEmpty(menus)){
|
||||||
|
List<RoleSmallDto> roles = roleService.findByUsersId(currentUserId);
|
||||||
|
Set<Long> roleIds = roles.stream().map(RoleSmallDto::getId).collect(Collectors.toSet());
|
||||||
|
LinkedHashSet<Menu> data = menuRepository.findByRoleIdsAndTypeNot(roleIds, 2);
|
||||||
|
menus = new ArrayList<>(data);
|
||||||
|
redisUtils.set(key, menus, 1, TimeUnit.DAYS);
|
||||||
|
}
|
||||||
return menus.stream().map(menuMapper::toDto).collect(Collectors.toList());
|
return menus.stream().map(menuMapper::toDto).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +202,7 @@ public class MenuServiceImpl implements MenuService {
|
||||||
for (Menu menu : menuList) {
|
for (Menu menu : menuList) {
|
||||||
menuSet.add(menu);
|
menuSet.add(menu);
|
||||||
List<Menu> menus = menuRepository.findByPidOrderByMenuSort(menu.getId());
|
List<Menu> menus = menuRepository.findByPidOrderByMenuSort(menu.getId());
|
||||||
if(menus!=null && menus.size()!=0){
|
if(CollUtil.isNotEmpty(menus)){
|
||||||
getChildMenus(menus, menuSet);
|
getChildMenus(menus, menuSet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -252,7 +260,7 @@ public class MenuServiceImpl implements MenuService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(trees.size() == 0){
|
if(trees.isEmpty()){
|
||||||
trees = menuDtos.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
|
trees = menuDtos.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
return trees;
|
return trees;
|
||||||
|
@ -287,16 +295,7 @@ public class MenuServiceImpl implements MenuService {
|
||||||
menuVo.setChildren(buildMenus(menuDtoList));
|
menuVo.setChildren(buildMenus(menuDtoList));
|
||||||
// 处理是一级菜单并且没有子菜单的情况
|
// 处理是一级菜单并且没有子菜单的情况
|
||||||
} else if(menuDTO.getPid() == null){
|
} else if(menuDTO.getPid() == null){
|
||||||
MenuVo menuVo1 = new MenuVo();
|
MenuVo menuVo1 = getMenuVo(menuDTO, menuVo);
|
||||||
menuVo1.setMeta(menuVo.getMeta());
|
|
||||||
// 非外链
|
|
||||||
if(!menuDTO.getIFrame()){
|
|
||||||
menuVo1.setPath("index");
|
|
||||||
menuVo1.setName(menuVo.getName());
|
|
||||||
menuVo1.setComponent(menuVo.getComponent());
|
|
||||||
} else {
|
|
||||||
menuVo1.setPath(menuDTO.getPath());
|
|
||||||
}
|
|
||||||
menuVo.setName(null);
|
menuVo.setName(null);
|
||||||
menuVo.setMeta(null);
|
menuVo.setMeta(null);
|
||||||
menuVo.setComponent("Layout");
|
menuVo.setComponent("Layout");
|
||||||
|
@ -356,4 +355,24 @@ public class MenuServiceImpl implements MenuService {
|
||||||
}});
|
}});
|
||||||
redisUtils.delByKeys(CacheKey.ROLE_ID, roles.stream().map(Role::getId).collect(Collectors.toSet()));
|
redisUtils.delByKeys(CacheKey.ROLE_ID, roles.stream().map(Role::getId).collect(Collectors.toSet()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建前端路由
|
||||||
|
* @param menuDTO /
|
||||||
|
* @param menuVo /
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
private static MenuVo getMenuVo(MenuDto menuDTO, MenuVo menuVo) {
|
||||||
|
MenuVo menuVo1 = new MenuVo();
|
||||||
|
menuVo1.setMeta(menuVo.getMeta());
|
||||||
|
// 非外链
|
||||||
|
if(!menuDTO.getIFrame()){
|
||||||
|
menuVo1.setPath("index");
|
||||||
|
menuVo1.setName(menuVo.getName());
|
||||||
|
menuVo1.setComponent(menuVo.getComponent());
|
||||||
|
} else {
|
||||||
|
menuVo1.setPath(menuDTO.getPath());
|
||||||
|
}
|
||||||
|
return menuVo1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package me.zhengjie.modules.system.service.impl;
|
package me.zhengjie.modules.system.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import me.zhengjie.exception.BadRequestException;
|
import me.zhengjie.exception.BadRequestException;
|
||||||
|
@ -34,8 +35,6 @@ import me.zhengjie.modules.system.service.dto.UserDto;
|
||||||
import me.zhengjie.modules.system.service.mapstruct.RoleMapper;
|
import me.zhengjie.modules.system.service.mapstruct.RoleMapper;
|
||||||
import me.zhengjie.modules.system.service.mapstruct.RoleSmallMapper;
|
import me.zhengjie.modules.system.service.mapstruct.RoleSmallMapper;
|
||||||
import me.zhengjie.utils.*;
|
import me.zhengjie.utils.*;
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
@ -44,6 +43,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -52,7 +52,6 @@ import java.util.stream.Collectors;
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@CacheConfig(cacheNames = "role", keyGenerator = "keyGenerator")
|
|
||||||
public class RoleServiceImpl implements RoleService {
|
public class RoleServiceImpl implements RoleService {
|
||||||
|
|
||||||
private final RoleRepository roleRepository;
|
private final RoleRepository roleRepository;
|
||||||
|
@ -80,11 +79,14 @@ public class RoleServiceImpl implements RoleService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'id:' + #p0")
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public RoleDto findById(long id) {
|
public RoleDto findById(long id) {
|
||||||
Role role = roleRepository.findById(id).orElseGet(Role::new);
|
String key = CacheKey.ROLE_ID + id;
|
||||||
ValidationUtil.isNull(role.getId(), "Role", "id", id);
|
Role role = redisUtils.get(key, Role.class);
|
||||||
|
if (role == null) {
|
||||||
|
role = roleRepository.findById(id).orElseGet(Role::new);
|
||||||
|
ValidationUtil.isNull(role.getId(), "Role", "id", id);
|
||||||
|
redisUtils.set(key, role, 1, TimeUnit.DAYS);
|
||||||
|
}
|
||||||
return roleMapper.toDto(role);
|
return roleMapper.toDto(role);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +154,7 @@ public class RoleServiceImpl implements RoleService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer findByRoles(Set<Role> roles) {
|
public Integer findByRoles(Set<Role> roles) {
|
||||||
if (roles.size() == 0) {
|
if (roles.isEmpty()) {
|
||||||
return Integer.MAX_VALUE;
|
return Integer.MAX_VALUE;
|
||||||
}
|
}
|
||||||
Set<RoleDto> roleDtos = new HashSet<>();
|
Set<RoleDto> roleDtos = new HashSet<>();
|
||||||
|
@ -163,21 +165,26 @@ public class RoleServiceImpl implements RoleService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'auth:' + #p0.id")
|
|
||||||
public List<AuthorityDto> buildAuthorities(UserDto user) {
|
public List<AuthorityDto> buildAuthorities(UserDto user) {
|
||||||
Set<String> permissions = new HashSet<>();
|
String key = CacheKey.ROLE_AUTH + user.getId();
|
||||||
// 如果是管理员直接返回
|
List<AuthorityDto> authorityDtos = redisUtils.getList(key, AuthorityDto.class);
|
||||||
if (user.getIsAdmin()) {
|
if (CollUtil.isEmpty(authorityDtos)) {
|
||||||
permissions.add("admin");
|
Set<String> permissions = new HashSet<>();
|
||||||
return permissions.stream().map(AuthorityDto::new)
|
// 如果是管理员直接返回
|
||||||
|
if (user.getIsAdmin()) {
|
||||||
|
permissions.add("admin");
|
||||||
|
return permissions.stream().map(AuthorityDto::new)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
Set<Role> roles = roleRepository.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());
|
.collect(Collectors.toList());
|
||||||
|
redisUtils.set(key, authorityDtos, 1, TimeUnit.HOURS);
|
||||||
}
|
}
|
||||||
Set<Role> roles = roleRepository.findByUserId(user.getId());
|
return authorityDtos;
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -30,8 +30,6 @@ import me.zhengjie.modules.system.service.dto.*;
|
||||||
import me.zhengjie.modules.system.service.mapstruct.UserLoginMapper;
|
import me.zhengjie.modules.system.service.mapstruct.UserLoginMapper;
|
||||||
import me.zhengjie.modules.system.service.mapstruct.UserMapper;
|
import me.zhengjie.modules.system.service.mapstruct.UserMapper;
|
||||||
import me.zhengjie.utils.*;
|
import me.zhengjie.utils.*;
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -42,6 +40,7 @@ import javax.validation.constraints.NotBlank;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,7 +49,6 @@ import java.util.stream.Collectors;
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@CacheConfig(cacheNames = "user", keyGenerator = "keyGenerator")
|
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
@ -74,11 +72,15 @@ public class UserServiceImpl implements UserService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "'id:' + #p0")
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public UserDto findById(long id) {
|
public UserDto findById(long id) {
|
||||||
User user = userRepository.findById(id).orElseGet(User::new);
|
String key = CacheKey.USER_ID + id;
|
||||||
ValidationUtil.isNull(user.getId(), "User", "id", id);
|
User user = redisUtils.get(key, User.class);
|
||||||
|
if (user == null) {
|
||||||
|
user = userRepository.findById(id).orElseGet(User::new);
|
||||||
|
ValidationUtil.isNull(user.getId(), "User", "id", id);
|
||||||
|
redisUtils.set(key, user, 1, TimeUnit.DAYS);
|
||||||
|
}
|
||||||
return userMapper.toDto(user);
|
return userMapper.toDto(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue