Commit 1af4eb89 authored by ZhengJie's avatar ZhengJie
Browse files

[代码完善](v2.5): v2.5 beta 缓存优化、加入RsaUtils工具类解决Rsa解密过慢,多选删除优化,修复菜单缓存问题,修复代码生成表查询分页问题

去除大量基于注解的缓存,去除列表查询的缓存,去除不合理的@CacheEvict(allEntries = true),缓存细腻化。

部分接口多选删除优化。

修复由于升级 hutool 工具版本导致代码生成分页错误的Bug

有问题请提 Issues

2.5 Beta 详情:https://www.ydyno.com/archives/1225.html
parent 2d969b43
...@@ -19,7 +19,6 @@ import me.zhengjie.modules.system.domain.Dict; ...@@ -19,7 +19,6 @@ import me.zhengjie.modules.system.domain.Dict;
import me.zhengjie.modules.system.service.dto.DictDto; import me.zhengjie.modules.system.service.dto.DictDto;
import me.zhengjie.modules.system.service.dto.DictQueryCriteria; import me.zhengjie.modules.system.service.dto.DictQueryCriteria;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
...@@ -47,13 +46,6 @@ public interface DictService { ...@@ -47,13 +46,6 @@ public interface DictService {
*/ */
List<DictDto> queryAll(DictQueryCriteria dict); List<DictDto> queryAll(DictQueryCriteria dict);
/**
* 根据ID查询
* @param id /
* @return /
*/
DictDto findById(Long id);
/** /**
* 创建 * 创建
* @param resources / * @param resources /
......
...@@ -19,7 +19,6 @@ import me.zhengjie.modules.system.domain.Job; ...@@ -19,7 +19,6 @@ import me.zhengjie.modules.system.domain.Job;
import me.zhengjie.modules.system.service.dto.JobDto; import me.zhengjie.modules.system.service.dto.JobDto;
import me.zhengjie.modules.system.service.dto.JobQueryCriteria; import me.zhengjie.modules.system.service.dto.JobQueryCriteria;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
......
...@@ -66,13 +66,6 @@ public interface MenuService { ...@@ -66,13 +66,6 @@ public interface MenuService {
*/ */
Set<Menu> getDeleteMenus(List<Menu> menuList, Set<Menu> menuSet); Set<Menu> getDeleteMenus(List<Menu> menuList, Set<Menu> menuSet);
/**
* 根据pid查询
* @param pid /
* @return /
*/
List<Menu> findByPid(long pid);
/** /**
* 构建菜单树 * 构建菜单树
* @param menuDtos 原始数据 * @param menuDtos 原始数据
...@@ -80,13 +73,6 @@ public interface MenuService { ...@@ -80,13 +73,6 @@ public interface MenuService {
*/ */
List<MenuDto> buildTree(List<MenuDto> menuDtos); List<MenuDto> buildTree(List<MenuDto> menuDtos);
/**
* 根据角色查询
* @param roles /
* @return /
*/
List<MenuDto> findByRoles(List<RoleSmallDto> roles);
/** /**
* 构建菜单树 * 构建菜单树
* @param menuDtos / * @param menuDtos /
...@@ -120,7 +106,7 @@ public interface MenuService { ...@@ -120,7 +106,7 @@ public interface MenuService {
* @param pid / * @param pid /
* @return / * @return /
*/ */
Object getMenus(Long pid); List<MenuDto> getMenus(Long pid);
/** /**
* 根据ID获取同级与上级数据 * 根据ID获取同级与上级数据
...@@ -129,4 +115,11 @@ public interface MenuService { ...@@ -129,4 +115,11 @@ public interface MenuService {
* @return / * @return /
*/ */
List<MenuDto> getSuperior(MenuDto menuDto, List<Menu> objects); List<MenuDto> getSuperior(MenuDto menuDto, List<Menu> objects);
/**
* 根据当前用户获取菜单
* @param currentUserId /
* @return /
*/
List<MenuDto> findByUser(Long currentUserId);
} }
...@@ -23,6 +23,8 @@ import me.zhengjie.modules.system.service.RoleService; ...@@ -23,6 +23,8 @@ 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.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.*;
...@@ -34,12 +36,19 @@ import java.util.*; ...@@ -34,12 +36,19 @@ import java.util.*;
**/ **/
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@CacheConfig(cacheNames = "data")
public class DataServiceImpl implements DataService { public class DataServiceImpl implements DataService {
private final RoleService roleService; private final RoleService roleService;
private final DeptService deptService; private final DeptService deptService;
/**
* 用户角色改变时需清理缓存
* @param user /
* @return /
*/
@Override @Override
@Cacheable(key = "'user:' + #p0.id")
public List<Long> getDeptIds(UserDto user) { public List<Long> getDeptIds(UserDto user) {
// 用于存储部门id // 用于存储部门id
Set<Long> deptIds = new HashSet<>(); Set<Long> deptIds = new HashSet<>();
...@@ -69,35 +78,14 @@ public class DataServiceImpl implements DataService { ...@@ -69,35 +78,14 @@ public class DataServiceImpl implements DataService {
* @return 数据权限ID * @return 数据权限ID
*/ */
public Set<Long> getCustomize(Set<Long> deptIds, RoleSmallDto role){ public Set<Long> getCustomize(Set<Long> deptIds, RoleSmallDto role){
Set<Dept> depts = deptService.findByRoleIds(role.getId()); Set<Dept> depts = deptService.findByRoleId(role.getId());
for (Dept dept : depts) { for (Dept dept : depts) {
deptIds.add(dept.getId()); deptIds.add(dept.getId());
List<Dept> deptChildren = deptService.findByPid(dept.getId()); List<Dept> deptChildren = deptService.findByPid(dept.getId());
if (deptChildren != null && deptChildren.size() != 0) { if (deptChildren != null && deptChildren.size() != 0) {
deptIds.addAll(getDeptChildren(deptChildren)); deptIds.addAll(deptService.getDeptChildren(dept.getId(), deptChildren));
} }
} }
return deptIds; return deptIds;
} }
/**
* 递归获取子级部门
* @param deptList 部门
* @return 数据权限
*/
@Override
public List<Long> getDeptChildren(List<Dept> deptList) {
List<Long> list = new ArrayList<>();
deptList.forEach(dept -> {
if (dept!=null && dept.getEnabled()){
List<Dept> depts = deptService.findByPid(dept.getId());
if(deptList.size() != 0){
list.addAll(getDeptChildren(depts));
}
list.add(dept.getId());
}
}
);
return list;
}
} }
...@@ -20,14 +20,19 @@ import cn.hutool.core.util.ObjectUtil; ...@@ -20,14 +20,19 @@ import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.Dept; import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.dto.DeptDto; import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.modules.system.service.dto.DeptQueryCriteria; import me.zhengjie.modules.system.service.dto.DeptQueryCriteria;
import me.zhengjie.utils.FileUtil; import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.QueryHelp; import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.RedisUtils;
import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.system.repository.DeptRepository; 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 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.Propagation; import org.springframework.transaction.annotation.Propagation;
...@@ -44,11 +49,14 @@ import java.util.stream.Collectors; ...@@ -44,11 +49,14 @@ import java.util.stream.Collectors;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@CacheConfig(cacheNames = "dept")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeptServiceImpl implements DeptService { public class DeptServiceImpl implements DeptService {
private final DeptRepository deptRepository; private final DeptRepository deptRepository;
private final DeptMapper deptMapper; private final DeptMapper deptMapper;
private final UserRepository userRepository;
private final RedisUtils redisUtils;
@Override @Override
public List<DeptDto> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception { public List<DeptDto> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception {
...@@ -74,6 +82,7 @@ public class DeptServiceImpl implements DeptService { ...@@ -74,6 +82,7 @@ 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); Dept dept = deptRepository.findById(id).orElseGet(Dept::new);
ValidationUtil.isNull(dept.getId(),"Dept","id",id); ValidationUtil.isNull(dept.getId(),"Dept","id",id);
...@@ -81,13 +90,14 @@ public class DeptServiceImpl implements DeptService { ...@@ -81,13 +90,14 @@ public class DeptServiceImpl implements DeptService {
} }
@Override @Override
@Cacheable(key = "'pid:' + #p0")
public List<Dept> findByPid(long pid) { public List<Dept> findByPid(long pid) {
return deptRepository.findByPid(pid); return deptRepository.findByPid(pid);
} }
@Override @Override
public Set<Dept> findByRoleIds(Long id) { public Set<Dept> findByRoleId(Long id) {
return deptRepository.findByRoles_Id(id); return deptRepository.findByRoleId(id);
} }
@Override @Override
...@@ -97,6 +107,8 @@ public class DeptServiceImpl implements DeptService { ...@@ -97,6 +107,8 @@ public class DeptServiceImpl implements DeptService {
// 计算子节点数目 // 计算子节点数目
resources.setSubCount(0); resources.setSubCount(0);
if(resources.getPid() != null){ if(resources.getPid() != null){
// 清理缓存
redisUtils.del("dept::pid:" + resources.getPid());
updateSubCnt(resources.getPid()); updateSubCnt(resources.getPid());
} }
} }
...@@ -104,8 +116,8 @@ public class DeptServiceImpl implements DeptService { ...@@ -104,8 +116,8 @@ public class DeptServiceImpl implements DeptService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(Dept resources) { public void update(Dept resources) {
// 旧的菜单 // 旧的部门
DeptDto old = findById(resources.getId()); Long pid = findById(resources.getId()).getPid();
if(resources.getPid() != null && resources.getId().equals(resources.getPid())) { if(resources.getPid() != null && resources.getId().equals(resources.getPid())) {
throw new BadRequestException("上级不能为自己"); throw new BadRequestException("上级不能为自己");
} }
...@@ -114,16 +126,21 @@ public class DeptServiceImpl implements DeptService { ...@@ -114,16 +126,21 @@ public class DeptServiceImpl implements DeptService {
resources.setId(dept.getId()); resources.setId(dept.getId());
deptRepository.save(resources); deptRepository.save(resources);
if(resources.getPid() == null){ if(resources.getPid() == null){
updateSubCnt(old.getPid()); updateSubCnt(pid);
} else { } else {
pid = resources.getPid();
updateSubCnt(resources.getPid()); updateSubCnt(resources.getPid());
} }
// 清理缓存
delCaches(resources.getId(), pid);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(Set<DeptDto> deptDtos) { public void delete(Set<DeptDto> deptDtos) {
for (DeptDto deptDto : deptDtos) { for (DeptDto deptDto : deptDtos) {
// 清理缓存
delCaches(deptDto.getId(), deptDto.getPid());
deptRepository.deleteById(deptDto.getId()); deptRepository.deleteById(deptDto.getId());
if(deptDto.getPid() != null){ if(deptDto.getPid() != null){
updateSubCnt(deptDto.getPid()); updateSubCnt(deptDto.getPid());
...@@ -156,6 +173,22 @@ public class DeptServiceImpl implements DeptService { ...@@ -156,6 +173,22 @@ public class DeptServiceImpl implements DeptService {
return deptDtos; return deptDtos;
} }
@Override
public List<Long> getDeptChildren(Long deptId, List<Dept> deptList) {
List<Long> list = new ArrayList<>();
deptList.forEach(dept -> {
if (dept!=null && dept.getEnabled()){
List<Dept> depts = deptRepository.findByPid(dept.getId());
if(deptList.size() != 0){
list.addAll(getDeptChildren(dept.getId(), depts));
}
list.add(dept.getId());
}
}
);
return list;
}
@Override @Override
public List<DeptDto> getSuperior(DeptDto deptDto, List<Dept> depts) { public List<DeptDto> getSuperior(DeptDto deptDto, List<Dept> depts) {
if(deptDto.getPid() == null){ if(deptDto.getPid() == null){
...@@ -178,7 +211,7 @@ public class DeptServiceImpl implements DeptService { ...@@ -178,7 +211,7 @@ public class DeptServiceImpl implements DeptService {
trees.add(deptDTO); trees.add(deptDTO);
} }
for (DeptDto it : deptDtos) { for (DeptDto it : deptDtos) {
if (deptDTO.getId().equals(it.getPid())) { if (it.getPid() != null && deptDTO.getId().equals(it.getPid())) {
isChild = true; isChild = true;
if (deptDTO.getChildren() == null) { if (deptDTO.getChildren() == null) {
deptDTO.setChildren(new ArrayList<>()); deptDTO.setChildren(new ArrayList<>());
...@@ -188,7 +221,7 @@ public class DeptServiceImpl implements DeptService { ...@@ -188,7 +221,7 @@ public class DeptServiceImpl implements DeptService {
} }
if(isChild) { if(isChild) {
depts.add(deptDTO); depts.add(deptDTO);
} else if(!deptNames.contains(deptRepository.findNameById(deptDTO.getPid()))) { } else if(deptDTO.getPid() != null && !deptNames.contains(findById(deptDTO.getPid()).getName())) {
depts.add(deptDTO); depts.add(deptDTO);
} }
} }
...@@ -196,11 +229,8 @@ public class DeptServiceImpl implements DeptService { ...@@ -196,11 +229,8 @@ public class DeptServiceImpl implements DeptService {
if (CollectionUtil.isEmpty(trees)) { if (CollectionUtil.isEmpty(trees)) {
trees = depts; trees = depts;
} }
Integer totalElements = deptDtos.size();
Map<String,Object> map = new HashMap<>(2); Map<String,Object> map = new HashMap<>(2);
map.put("totalElements",totalElements); map.put("totalElements",deptDtos.size());
map.put("content",CollectionUtil.isEmpty(trees)? deptDtos :trees); map.put("content",CollectionUtil.isEmpty(trees)? deptDtos :trees);
return map; return map;
} }
...@@ -209,4 +239,18 @@ public class DeptServiceImpl implements DeptService { ...@@ -209,4 +239,18 @@ public class DeptServiceImpl implements DeptService {
int count = deptRepository.countByPid(deptId); int count = deptRepository.countByPid(deptId);
deptRepository.updateSubCntById(count, deptId); deptRepository.updateSubCntById(count, deptId);
} }
/**
* 清理缓存
* @param id /
*/
public void delCaches(Long id, Long pid){
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);
}
}
} }
\ No newline at end of file
...@@ -16,20 +16,26 @@ ...@@ -16,20 +16,26 @@
package me.zhengjie.modules.system.service.impl; package me.zhengjie.modules.system.service.impl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.Dict;
import me.zhengjie.modules.system.domain.DictDetail; import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.repository.DictRepository;
import me.zhengjie.modules.system.service.dto.DictDetailQueryCriteria; import me.zhengjie.modules.system.service.dto.DictDetailQueryCriteria;
import me.zhengjie.utils.PageUtil; import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp; import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.RedisUtils;
import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.system.repository.DictDetailRepository; 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.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
...@@ -38,11 +44,14 @@ import java.util.Map; ...@@ -38,11 +44,14 @@ import java.util.Map;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@CacheConfig(cacheNames = "dict")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DictDetailServiceImpl implements DictDetailService { public class DictDetailServiceImpl implements DictDetailService {
private final DictRepository dictRepository;
private final DictDetailRepository dictDetailRepository; private final DictDetailRepository dictDetailRepository;
private final DictDetailMapper dictDetailMapper; private final DictDetailMapper dictDetailMapper;
private final RedisUtils redisUtils;
@Override @Override
public Map<String,Object> queryAll(DictDetailQueryCriteria criteria, Pageable pageable) { public Map<String,Object> queryAll(DictDetailQueryCriteria criteria, Pageable pageable) {
...@@ -50,17 +59,12 @@ public class DictDetailServiceImpl implements DictDetailService { ...@@ -50,17 +59,12 @@ public class DictDetailServiceImpl implements DictDetailService {
return PageUtil.toPage(page.map(dictDetailMapper::toDto)); return PageUtil.toPage(page.map(dictDetailMapper::toDto));
} }
@Override
public DictDetailDto findById(Long id) {
DictDetail dictDetail = dictDetailRepository.findById(id).orElseGet(DictDetail::new);
ValidationUtil.isNull(dictDetail.getId(),"DictDetail","id",id);
return dictDetailMapper.toDto(dictDetail);
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void create(DictDetail resources) { public void create(DictDetail resources) {
dictDetailRepository.save(resources); dictDetailRepository.save(resources);
// 清理缓存
delCaches(resources);
} }
@Override @Override
...@@ -70,11 +74,27 @@ public class DictDetailServiceImpl implements DictDetailService { ...@@ -70,11 +74,27 @@ public class DictDetailServiceImpl implements DictDetailService {
ValidationUtil.isNull( dictDetail.getId(),"DictDetail","id",resources.getId()); ValidationUtil.isNull( dictDetail.getId(),"DictDetail","id",resources.getId());
resources.setId(dictDetail.getId()); resources.setId(dictDetail.getId());
dictDetailRepository.save(resources); dictDetailRepository.save(resources);
// 清理缓存
delCaches(resources);
}
@Override
@Cacheable(key = "'name:' + #p0")
public List<DictDetailDto> getDictByName(String name) {
return dictDetailMapper.toDto(dictDetailRepository.findByDictName(name));
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(Long id) { public void delete(Long id) {
DictDetail dictDetail = dictDetailRepository.findById(id).orElseGet(DictDetail::new);
// 清理缓存
delCaches(dictDetail);
dictDetailRepository.deleteById(id); dictDetailRepository.deleteById(id);
} }
public void delCaches(DictDetail dictDetail){
Dict dict = dictRepository.findById(dictDetail.getDict().getId()).orElseGet(Dict::new);
redisUtils.del("dept::name:" + dict.getName());
}
} }
\ No newline at end of file
...@@ -25,6 +25,8 @@ import me.zhengjie.modules.system.repository.DictRepository; ...@@ -25,6 +25,8 @@ 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.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;
...@@ -40,6 +42,7 @@ import java.util.*; ...@@ -40,6 +42,7 @@ import java.util.*;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@CacheConfig(cacheNames = "dict")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DictServiceImpl implements DictService { public class DictServiceImpl implements DictService {
...@@ -59,13 +62,6 @@ public class DictServiceImpl implements DictService { ...@@ -59,13 +62,6 @@ public class DictServiceImpl implements DictService {
return dictMapper.toDto(list); return dictMapper.toDto(list);
} }
@Override
public DictDto findById(Long id) {
Dict dict = dictRepository.findById(id).orElseGet(Dict::new);
ValidationUtil.isNull(dict.getId(),"Dict","id",id);
return dictMapper.toDto(dict);
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void create(Dict resources) { public void create(Dict resources) {
...@@ -75,6 +71,8 @@ public class DictServiceImpl implements DictService { ...@@ -75,6 +71,8 @@ public class DictServiceImpl implements DictService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(Dict resources) { public void update(Dict resources) {
// 清理缓存
delCaches(resources);
Dict dict = dictRepository.findById(resources.getId()).orElseGet(Dict::new); Dict dict = dictRepository.findById(resources.getId()).orElseGet(Dict::new);
ValidationUtil.isNull( dict.getId(),"Dict","id",resources.getId()); ValidationUtil.isNull( dict.getId(),"Dict","id",resources.getId());
resources.setId(dict.getId()); resources.setId(dict.getId());
...@@ -84,8 +82,12 @@ public class DictServiceImpl implements DictService { ...@@ -84,8 +82,12 @@ public class DictServiceImpl implements DictService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(Set<Long> ids) { public void delete(Set<Long> ids) {
// 清理缓存
List<Dict> dicts = dictRepository.findByIdIn(ids);
for (Dict dict : dicts) {
delCaches(dict);
}
dictRepository.deleteByIdIn(ids); dictRepository.deleteByIdIn(ids);
redisUtils.delByKeys("dict::", ids);
} }
@Override @Override
...@@ -114,4 +116,8 @@ public class DictServiceImpl implements DictService { ...@@ -114,4 +116,8 @@ public class DictServiceImpl implements DictService {
} }
FileUtil.downloadExcel(list, response); FileUtil.downloadExcel(list, response);
} }
public void delCaches(Dict dict){
redisUtils.del("dept::name:" + dict.getName());
}
} }
\ No newline at end of file
...@@ -24,6 +24,9 @@ import me.zhengjie.modules.system.repository.JobRepository; ...@@ -24,6 +24,9 @@ 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;
...@@ -39,6 +42,7 @@ import java.util.*; ...@@ -39,6 +42,7 @@ import java.util.*;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@CacheConfig(cacheNames = "job")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class JobServiceImpl implements JobService { public class JobServiceImpl implements JobService {
...@@ -59,6 +63,7 @@ public class JobServiceImpl implements JobService { ...@@ -59,6 +63,7 @@ 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); Job job = jobRepository.findById(id).orElseGet(Job::new);
ValidationUtil.isNull(job.getId(),"Job","id",id); ValidationUtil.isNull(job.getId(),"Job","id",id);
...@@ -76,6 +81,7 @@ public class JobServiceImpl implements JobService { ...@@ -76,6 +81,7 @@ 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);
...@@ -91,11 +97,9 @@ public class JobServiceImpl implements JobService { ...@@ -91,11 +97,9 @@ public class JobServiceImpl implements JobService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(Set<Long> ids) { public void delete(Set<Long> ids) {
for (Long id : ids) { jobRepository.deleteAllByIdIn(ids);
jobRepository.deleteById(id); // 删除缓存
// 删除缓存 redisUtils.delByKeys("job::id:", ids);
redisUtils.del("job::"+id);
}
} }
@Override @Override
......
...@@ -19,21 +19,22 @@ import cn.hutool.core.util.ObjectUtil; ...@@ -19,21 +19,22 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.Menu; import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.domain.vo.MenuMetaVo; import me.zhengjie.modules.system.domain.vo.MenuMetaVo;
import me.zhengjie.modules.system.domain.vo.MenuVo; import me.zhengjie.modules.system.domain.vo.MenuVo;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.exception.EntityExistException; import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.system.repository.MenuRepository; import me.zhengjie.modules.system.repository.MenuRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.MenuService; import me.zhengjie.modules.system.service.MenuService;
import me.zhengjie.modules.system.service.RoleService; import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.dto.MenuDto; import me.zhengjie.modules.system.service.dto.MenuDto;
import me.zhengjie.modules.system.service.dto.MenuQueryCriteria; 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.FileUtil; import me.zhengjie.utils.*;
import me.zhengjie.utils.QueryHelp; import org.springframework.cache.annotation.CacheConfig;
import me.zhengjie.utils.StringUtils; import org.springframework.cache.annotation.Cacheable;
import me.zhengjie.utils.ValidationUtil;
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.Propagation; import org.springframework.transaction.annotation.Propagation;
...@@ -49,12 +50,15 @@ import java.util.stream.Collectors; ...@@ -49,12 +50,15 @@ import java.util.stream.Collectors;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@CacheConfig(cacheNames = "menu")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MenuServiceImpl implements MenuService { public class MenuServiceImpl implements MenuService {
private final MenuRepository menuRepository; private final MenuRepository menuRepository;
private final UserRepository userRepository;
private final MenuMapper menuMapper; private final MenuMapper menuMapper;
private final RoleService roleService; private final RoleService roleService;
private final RedisUtils redisUtils;
@Override @Override
public List<MenuDto> queryAll(MenuQueryCriteria criteria, Boolean isQuery) throws Exception { public List<MenuDto> queryAll(MenuQueryCriteria criteria, Boolean isQuery) throws Exception {
...@@ -79,16 +83,24 @@ public class MenuServiceImpl implements MenuService { ...@@ -79,16 +83,24 @@ 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); Menu menu = menuRepository.findById(id).orElseGet(Menu::new);
ValidationUtil.isNull(menu.getId(),"Menu","id",id); ValidationUtil.isNull(menu.getId(),"Menu","id",id);
return menuMapper.toDto(menu); return menuMapper.toDto(menu);
} }
/**
* 用户角色改变时需清理缓存
* @param currentUserId /
* @return /
*/
@Override @Override
public List<MenuDto> findByRoles(List<RoleSmallDto> roles) { @Cacheable(key = "'user:' + #p0")
public List<MenuDto> findByUser(Long currentUserId) {
List<RoleSmallDto> roles = roleService.findByUsersId(currentUserId);
Set<Long> roleIds = roles.stream().map(RoleSmallDto::getId).collect(Collectors.toSet()); Set<Long> roleIds = roles.stream().map(RoleSmallDto::getId).collect(Collectors.toSet());
LinkedHashSet<Menu> menus = menuRepository.findByRoles_IdInAndTypeNotOrderByMenuSortAsc(roleIds, 2); LinkedHashSet<Menu> menus = menuRepository.findByRoleIdsAndTypeNot(roleIds, 2);
return menus.stream().map(menuMapper::toDto).collect(Collectors.toList()); return menus.stream().map(menuMapper::toDto).collect(Collectors.toList());
} }
...@@ -116,6 +128,8 @@ public class MenuServiceImpl implements MenuService { ...@@ -116,6 +128,8 @@ public class MenuServiceImpl implements MenuService {
// 计算子节点数目 // 计算子节点数目
resources.setSubCount(0); resources.setSubCount(0);
if(resources.getPid() != null){ if(resources.getPid() != null){
// 清理缓存
redisUtils.del("menu::pid:" + resources.getPid());
updateSubCnt(resources.getPid()); updateSubCnt(resources.getPid());
} }
} }
...@@ -128,7 +142,7 @@ public class MenuServiceImpl implements MenuService { ...@@ -128,7 +142,7 @@ public class MenuServiceImpl implements MenuService {
} }
Menu menu = menuRepository.findById(resources.getId()).orElseGet(Menu::new); Menu menu = menuRepository.findById(resources.getId()).orElseGet(Menu::new);
// 记录旧的父节点ID // 记录旧的父节点ID
Long oldPid = menu.getPid(); Long pid = menu.getPid();
ValidationUtil.isNull(menu.getId(),"Permission","id",resources.getId()); ValidationUtil.isNull(menu.getId(),"Permission","id",resources.getId());
if(resources.getIFrame()){ if(resources.getIFrame()){
...@@ -167,10 +181,13 @@ public class MenuServiceImpl implements MenuService { ...@@ -167,10 +181,13 @@ public class MenuServiceImpl implements MenuService {
menuRepository.save(menu); menuRepository.save(menu);
// 计算子节点数目 // 计算子节点数目
if(resources.getPid() == null){ if(resources.getPid() == null){
updateSubCnt(oldPid); updateSubCnt(pid);
} else { } else {
pid = resources.getPid();
updateSubCnt(resources.getPid()); updateSubCnt(resources.getPid());
} }
// 清理缓存
delCaches(resources.getId(), pid);
} }
@Override @Override
...@@ -192,6 +209,8 @@ public class MenuServiceImpl implements MenuService { ...@@ -192,6 +209,8 @@ public class MenuServiceImpl implements MenuService {
for (Menu menu : menuSet) { for (Menu menu : menuSet) {
roleService.untiedMenu(menu.getId()); roleService.untiedMenu(menu.getId());
menuRepository.deleteById(menu.getId()); menuRepository.deleteById(menu.getId());
// 清理缓存
delCaches(menu.getId(), menu.getPid());
if(menu.getPid() != null){ if(menu.getPid() != null){
updateSubCnt(menu.getPid()); updateSubCnt(menu.getPid());
} }
...@@ -199,7 +218,8 @@ public class MenuServiceImpl implements MenuService { ...@@ -199,7 +218,8 @@ public class MenuServiceImpl implements MenuService {
} }
@Override @Override
public Object getMenus(Long pid) { @Cacheable(key = "'pid:' + #p0")
public List<MenuDto> getMenus(Long pid) {
List<Menu> menus; List<Menu> menus;
if(pid != null && !pid.equals(0L)){ if(pid != null && !pid.equals(0L)){
menus = menuRepository.findByPid(pid); menus = menuRepository.findByPid(pid);
...@@ -219,11 +239,6 @@ public class MenuServiceImpl implements MenuService { ...@@ -219,11 +239,6 @@ public class MenuServiceImpl implements MenuService {
return getSuperior(findById(menuDto.getPid()), menus); return getSuperior(findById(menuDto.getPid()), menus);
} }
@Override
public List<Menu> findByPid(long pid) {
return menuRepository.findByPid(pid);
}
@Override @Override
public List<MenuDto> buildTree(List<MenuDto> menuDtos) { public List<MenuDto> buildTree(List<MenuDto> menuDtos) {
List<MenuDto> trees = new ArrayList<>(); List<MenuDto> trees = new ArrayList<>();
...@@ -326,4 +341,18 @@ public class MenuServiceImpl implements MenuService { ...@@ -326,4 +341,18 @@ public class MenuServiceImpl implements MenuService {
int count = menuRepository.countByPid(menuId); int count = menuRepository.countByPid(menuId);
menuRepository.updateSubCntById(count, menuId); menuRepository.updateSubCntById(count, menuId);
} }
/**
* 清理缓存
* @param id 菜单ID
* @param pid 菜单父级ID
*/
public void delCaches(Long id, Long pid){
List<User> users = userRepository.findByMenuId(id);
redisUtils.del("menu::id:" +id);
redisUtils.delByKeys("menu::user:",users.stream().map(User::getId).collect(Collectors.toSet()));
if(pid != null){
redisUtils.del("menu::pid:" + pid);
}
}
} }
...@@ -15,14 +15,13 @@ ...@@ -15,14 +15,13 @@
*/ */
package me.zhengjie.modules.system.service.impl; package me.zhengjie.modules.system.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.modules.system.domain.Menu; import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.Role; import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.exception.EntityExistException; import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.system.repository.DeptRepository; import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.RoleRepository; import me.zhengjie.modules.system.repository.RoleRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.RoleService; import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.dto.RoleDto; import me.zhengjie.modules.system.service.dto.RoleDto;
import me.zhengjie.modules.system.service.dto.RoleQueryCriteria; import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
...@@ -31,7 +30,8 @@ import me.zhengjie.modules.system.service.dto.UserDto; ...@@ -31,7 +30,8 @@ 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 me.zhengjie.utils.enums.DataScopeEnum; 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;
...@@ -51,14 +51,15 @@ import java.util.stream.Collectors; ...@@ -51,14 +51,15 @@ import java.util.stream.Collectors;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@CacheConfig(cacheNames = "role")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RoleServiceImpl implements RoleService { public class RoleServiceImpl implements RoleService {
private final RoleRepository roleRepository; private final RoleRepository roleRepository;
private final RoleMapper roleMapper; private final RoleMapper roleMapper;
private final RoleSmallMapper roleSmallMapper; private final RoleSmallMapper roleSmallMapper;
private final DeptRepository deptRepository;
private final RedisUtils redisUtils; private final RedisUtils redisUtils;
private final UserRepository userRepository;
@Override @Override
public List<RoleDto> queryAll() { public List<RoleDto> queryAll() {
...@@ -78,6 +79,7 @@ public class RoleServiceImpl implements RoleService { ...@@ -78,6 +79,7 @@ public class RoleServiceImpl implements RoleService {
} }
@Override @Override
@Cacheable(key = "'id:' + #p0")
public RoleDto findById(long id) { public RoleDto findById(long id) {
Role role = roleRepository.findById(id).orElseGet(Role::new); Role role = roleRepository.findById(id).orElseGet(Role::new);
ValidationUtil.isNull(role.getId(),"Role","id",id); ValidationUtil.isNull(role.getId(),"Role","id",id);
...@@ -90,7 +92,6 @@ public class RoleServiceImpl implements RoleService { ...@@ -90,7 +92,6 @@ public class RoleServiceImpl implements RoleService {
if(roleRepository.findByName(resources.getName()) != null){ if(roleRepository.findByName(resources.getName()) != null){
throw new EntityExistException(Role.class,"username",resources.getName()); throw new EntityExistException(Role.class,"username",resources.getName());
} }
checkDataScope(resources);
roleRepository.save(resources); roleRepository.save(resources);
} }
...@@ -105,54 +106,52 @@ public class RoleServiceImpl implements RoleService { ...@@ -105,54 +106,52 @@ public class RoleServiceImpl implements RoleService {
if(role1 != null && !role1.getId().equals(role.getId())){ if(role1 != null && !role1.getId().equals(role.getId())){
throw new EntityExistException(Role.class,"username",resources.getName()); throw new EntityExistException(Role.class,"username",resources.getName());
} }
checkDataScope(resources);
role.setName(resources.getName()); role.setName(resources.getName());
role.setDescription(resources.getDescription()); role.setDescription(resources.getDescription());
role.setDataScope(resources.getDataScope()); role.setDataScope(resources.getDataScope());
role.setDepts(resources.getDepts()); role.setDepts(resources.getDepts());
role.setLevel(resources.getLevel()); role.setLevel(resources.getLevel());
roleRepository.save(role); roleRepository.save(role);
} // 更新相关缓存
delCaches(role.getId());
private void checkDataScope(Role resources){
if(CollectionUtil.isNotEmpty(resources.getDepts()) && resources.getDepts().size() == 1){
for (Dept dept : resources.getDepts()) {
dept = deptRepository.findById(dept.getId()).orElseGet(Dept::new);
if(dept.getPid() == 0 || dept.getPid() == null){
resources.setDepts(null);
resources.setDataScope(DataScopeEnum.ALL.getValue());
}
}
}
} }
@Override @Override
public void updateMenu(Role resources, RoleDto roleDTO) { public void updateMenu(Role resources, RoleDto roleDTO) {
Role role = roleMapper.toEntity(roleDTO); Role role = roleMapper.toEntity(roleDTO);
// 清理缓存
List<User> users = userRepository.findByRoleId(role.getId());
Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
redisUtils.delByKeys("menu::user:",userIds);
// 更新菜单
role.setMenus(resources.getMenus()); role.setMenus(resources.getMenus());
roleRepository.save(role); roleRepository.save(role);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void untiedMenu(Long id) { public void untiedMenu(Long menuId) {
roleRepository.untiedMenu(id); // 清理缓存
List<User> users = userRepository.findByMenuId(menuId);
Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
redisUtils.delByKeys("menu::user:",userIds);
// 更新菜单
roleRepository.untiedMenu(menuId);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(Set<Long> ids) { public void delete(Set<Long> ids) {
for (Long id : ids) { for (Long id : ids) {
roleRepository.deleteById(id); // 更新相关缓存
// 删除缓存 delCaches(id);
redisUtils.del("role::"+id);
} }
roleRepository.deleteAllByIdIn(ids);
} }
@Override @Override
public List<RoleSmallDto> findByUsersId(Long id) { public List<RoleSmallDto> findByUsersId(Long id) {
return roleSmallMapper.toDto(new ArrayList<>(roleRepository.findByUsers_Id(id))); return roleSmallMapper.toDto(new ArrayList<>(roleRepository.findByUserId(id)));
} }
@Override @Override
...@@ -165,6 +164,7 @@ public class RoleServiceImpl implements RoleService { ...@@ -165,6 +164,7 @@ public class RoleServiceImpl implements RoleService {
} }
@Override @Override
@Cacheable(key = "'auth:' + #p0")
public List<GrantedAuthority> mapToGrantedAuthorities(UserDto user) { public List<GrantedAuthority> mapToGrantedAuthorities(UserDto user) {
Set<String> permissions = new HashSet<>(); Set<String> permissions = new HashSet<>();
// 如果是管理员直接返回 // 如果是管理员直接返回
...@@ -173,7 +173,7 @@ public class RoleServiceImpl implements RoleService { ...@@ -173,7 +173,7 @@ public class RoleServiceImpl implements RoleService {
return permissions.stream().map(SimpleGrantedAuthority::new) return permissions.stream().map(SimpleGrantedAuthority::new)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
Set<Role> roles = roleRepository.findByUsers_Id(user.getId()); Set<Role> roles = roleRepository.findByUserId(user.getId());
permissions = roles.stream().flatMap(role -> role.getMenus().stream()) permissions = roles.stream().flatMap(role -> role.getMenus().stream())
.filter(menu -> StringUtils.isNotBlank(menu.getPermission())) .filter(menu -> StringUtils.isNotBlank(menu.getPermission()))
.map(Menu::getPermission).collect(Collectors.toSet()); .map(Menu::getPermission).collect(Collectors.toSet());
...@@ -194,4 +194,16 @@ public class RoleServiceImpl implements RoleService { ...@@ -194,4 +194,16 @@ public class RoleServiceImpl implements RoleService {
} }
FileUtil.downloadExcel(list, response); FileUtil.downloadExcel(list, response);
} }
/**
* 清理缓存
* @param id /
*/
public void delCaches(Long id){
List<User> users = userRepository.findByRoleId(id);
Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
redisUtils.delByKeys("data::user:",userIds);
redisUtils.delByKeys("menu::user:",userIds);
redisUtils.delByKeys("role::auth:",userIds);
}
} }
...@@ -28,6 +28,8 @@ import me.zhengjie.modules.system.service.dto.UserDto; ...@@ -28,6 +28,8 @@ import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.modules.system.service.dto.UserQueryCriteria; import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
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;
...@@ -46,13 +48,14 @@ import java.util.stream.Collectors; ...@@ -46,13 +48,14 @@ import java.util.stream.Collectors;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@CacheConfig(cacheNames = "user")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService { public class UserServiceImpl implements UserService {
private final UserRepository userRepository; private final UserRepository userRepository;
private final UserMapper userMapper; private final UserMapper userMapper;
private final RedisUtils redisUtils;
private final FileProperties properties; private final FileProperties properties;
private final RedisUtils redisUtils;
@Override @Override
public Object queryAll(UserQueryCriteria criteria, Pageable pageable) { public Object queryAll(UserQueryCriteria criteria, Pageable pageable) {
...@@ -67,6 +70,7 @@ public class UserServiceImpl implements UserService { ...@@ -67,6 +70,7 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
@Cacheable(key = "'id:' + #p0")
public UserDto findById(long id) { public UserDto findById(long id) {
User user = userRepository.findById(id).orElseGet(User::new); User user = userRepository.findById(id).orElseGet(User::new);
ValidationUtil.isNull(user.getId(),"User","id",id); ValidationUtil.isNull(user.getId(),"User","id",id);
...@@ -100,15 +104,12 @@ public class UserServiceImpl implements UserService { ...@@ -100,15 +104,12 @@ public class UserServiceImpl implements UserService {
if(user2!=null&&!user.getId().equals(user2.getId())){ if(user2!=null&&!user.getId().equals(user2.getId())){
throw new EntityExistException(User.class,"email",resources.getEmail()); throw new EntityExistException(User.class,"email",resources.getEmail());
} }
// 如果用户的角色改变
// 如果用户的角色改变了,需要手动清理下缓存
if (!resources.getRoles().equals(user.getRoles())) { if (!resources.getRoles().equals(user.getRoles())) {
String key = "role::permission:" + user.getUsername(); redisUtils.del("data::user:" + resources.getId());
redisUtils.del(key); redisUtils.del("menu::user:" + resources.getId());
key = "role::user:" + user.getId(); redisUtils.del("role::auth:" + resources.getId());
redisUtils.del(key);
} }
user.setUsername(resources.getUsername()); user.setUsername(resources.getUsername());
user.setEmail(resources.getEmail()); user.setEmail(resources.getEmail());
user.setEnabled(resources.getEnabled()); user.setEnabled(resources.getEnabled());
...@@ -119,6 +120,8 @@ public class UserServiceImpl implements UserService { ...@@ -119,6 +120,8 @@ public class UserServiceImpl implements UserService {
user.setNickName(resources.getNickName()); user.setNickName(resources.getNickName());
user.setGender(resources.getGender()); user.setGender(resources.getGender());
userRepository.save(user); userRepository.save(user);
// 清除缓存
delCaches(user.getId(), user.getUsername());
} }
@Override @Override
...@@ -129,24 +132,25 @@ public class UserServiceImpl implements UserService { ...@@ -129,24 +132,25 @@ public class UserServiceImpl implements UserService {
user.setPhone(resources.getPhone()); user.setPhone(resources.getPhone());
user.setGender(resources.getGender()); user.setGender(resources.getGender());
userRepository.save(user); userRepository.save(user);
// 清理缓存
delCaches(user.getId(), user.getUsername());
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(Set<Long> ids) { public void delete(Set<Long> ids) {
for (Long id : ids) { for (Long id : ids) {
userRepository.deleteById(id); // 清理缓存
UserDto user = findById(id);
delCaches(user.getId(), user.getUsername());
} }
userRepository.deleteAllByIdIn(ids);
} }
@Override @Override
@Cacheable(key = "'username:' + #p0")
public UserDto findByName(String userName) { public UserDto findByName(String userName) {
User user; User user = userRepository.findByUsername(userName);
if(ValidationUtil.isEmail(userName)){
user = userRepository.findByEmail(userName);
} else {
user = userRepository.findByUsername(userName);
}
if (user == null) { if (user == null) {
throw new EntityNotFoundException(User.class, "name", userName); throw new EntityNotFoundException(User.class, "name", userName);
} else { } else {
...@@ -158,6 +162,7 @@ public class UserServiceImpl implements UserService { ...@@ -158,6 +162,7 @@ public class UserServiceImpl implements UserService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updatePass(String username, String pass) { public void updatePass(String username, String pass) {
userRepository.updatePass(username,pass,new Date()); userRepository.updatePass(username,pass,new Date());
redisUtils.del("user::username:" + username);
} }
@Override @Override
...@@ -172,12 +177,14 @@ public class UserServiceImpl implements UserService { ...@@ -172,12 +177,14 @@ public class UserServiceImpl implements UserService {
if(StringUtils.isNotBlank(oldPath)){ if(StringUtils.isNotBlank(oldPath)){
FileUtil.del(oldPath); FileUtil.del(oldPath);
} }
redisUtils.del("user::username:" + user.getUsername());
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updateEmail(String username, String email) { public void updateEmail(String username, String email) {
userRepository.updateEmail(username,email); userRepository.updateEmail(username,email);
redisUtils.del("user::username:" + username);
} }
@Override @Override
...@@ -199,4 +206,13 @@ public class UserServiceImpl implements UserService { ...@@ -199,4 +206,13 @@ public class UserServiceImpl implements UserService {
} }
FileUtil.downloadExcel(list, response); FileUtil.downloadExcel(list, response);
} }
/**
* 清理缓存
* @param id /
*/
public void delCaches(Long id, String username){
redisUtils.del("user::id:" + id);
redisUtils.del("user::username:" + username);
}
} }
...@@ -53,9 +53,9 @@ jwt: ...@@ -53,9 +53,9 @@ jwt:
# 令牌过期时间 此处单位/毫秒 ,默认4小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html # 令牌过期时间 此处单位/毫秒 ,默认4小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html
token-validity-in-seconds: 14400000 token-validity-in-seconds: 14400000
# 在线用户key # 在线用户key
online-key: online-token online-key: online-token-
# 验证码 # 验证码
code-key: code-key code-key: code-key-
# token 续期检查时间范围(默认30分钟,单位毫秒),在token即将过期的一段时间内用户操作了,则给用户的token续期 # token 续期检查时间范围(默认30分钟,单位毫秒),在token即将过期的一段时间内用户操作了,则给用户的token续期
detect: 1800000 detect: 1800000
# 续期时间范围,默认1小时,单位毫秒 # 续期时间范围,默认1小时,单位毫秒
......
...@@ -55,9 +55,9 @@ jwt: ...@@ -55,9 +55,9 @@ jwt:
# 令牌过期时间 此处单位/毫秒 ,默认2小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html # 令牌过期时间 此处单位/毫秒 ,默认2小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html
token-validity-in-seconds: 7200000 token-validity-in-seconds: 7200000
# 在线用户key # 在线用户key
online-key: online-token online-key: online-token-
# 验证码 # 验证码
code-key: code-key code-key: code-key-
# token 续期检查时间范围(默认30分钟,单位默认毫秒),在token即将过期的一段时间内用户操作了,则给用户的token续期 # token 续期检查时间范围(默认30分钟,单位默认毫秒),在token即将过期的一段时间内用户操作了,则给用户的token续期
detect: 1800000 detect: 1800000
# 续期时间范围,默认 1小时,这里单位毫秒 # 续期时间范围,默认 1小时,这里单位毫秒
......
...@@ -6,12 +6,11 @@ import org.springframework.boot.test.context.SpringBootTest; ...@@ -6,12 +6,11 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EladminSystemApplicationTests { public class EladminSystemApplicationTests {
@Test @Test
public void contextLoads() { public void contextLoads() {
} }
public static void main(String[] args) { public static void main(String[] args) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment