Skip to content

Commit 6e0f9e4

Browse files
committed
feat: 使用 Cacheable 优化 API 角色关系缓存
1 parent ff846f1 commit 6e0f9e4

4 files changed

Lines changed: 16 additions & 19 deletions

File tree

framework/src/main/java/top/cadecode/uniboot/framework/controller/SysApiController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.swagger.annotations.ApiOperation;
77
import lombok.RequiredArgsConstructor;
88
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.cache.annotation.CacheEvict;
910
import org.springframework.transaction.annotation.Transactional;
1011
import org.springframework.validation.annotation.Validated;
1112
import org.springframework.web.bind.annotation.PostMapping;
@@ -20,6 +21,7 @@
2021
import top.cadecode.uniboot.framework.bean.po.SysApi;
2122
import top.cadecode.uniboot.framework.bean.vo.SysApiVo.SysApiRolesVo;
2223
import top.cadecode.uniboot.framework.bean.vo.SysApiVo.SysApiSwaggerVo;
24+
import top.cadecode.uniboot.framework.consts.KeyPrefix;
2325
import top.cadecode.uniboot.framework.convert.SysApiConvert;
2426
import top.cadecode.uniboot.framework.service.SysApiService;
2527
import top.cadecode.uniboot.framework.service.SysRoleService;
@@ -64,13 +66,15 @@ public PageResult<SysApiRolesVo> pageRolesVo(@RequestBody @Valid SysApiRolesRequ
6466
return new PageResult<>((int) rolesVoPage.getTotal(), rolesVoPage.getList());
6567
}
6668

69+
@CacheEvict(cacheNames = KeyPrefix.API_ROLES, key = "'all'")
6770
@ApiOperation("更新API")
6871
@PostMapping("update")
6972
public boolean update(@RequestBody @Valid SysApiUpdateRequest request) {
7073
SysApi po = SysApiConvert.INSTANCE.requestToPo(request);
7174
return sysApiService.updateById(po);
7275
}
7376

77+
@CacheEvict(cacheNames = KeyPrefix.API_ROLES, key = "'all'")
7478
@ApiOperation("添加API")
7579
@PostMapping("add")
7680
public boolean add(@RequestBody @Valid SysApiAddRequest request) {

framework/src/main/java/top/cadecode/uniboot/framework/controller/SysRoleController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.swagger.annotations.ApiOperation;
66
import lombok.RequiredArgsConstructor;
77
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.cache.annotation.CacheEvict;
89
import org.springframework.transaction.annotation.Transactional;
910
import org.springframework.validation.annotation.Validated;
1011
import org.springframework.web.bind.annotation.PostMapping;
@@ -16,6 +17,7 @@
1617
import top.cadecode.uniboot.framework.bean.po.SysRole;
1718
import top.cadecode.uniboot.framework.bean.vo.SysRoleVo.SysRoleListVo;
1819
import top.cadecode.uniboot.framework.bean.vo.SysRoleVo.SysRoleUnionVo;
20+
import top.cadecode.uniboot.framework.consts.KeyPrefix;
1921
import top.cadecode.uniboot.framework.convert.SysRoleConvert;
2022
import top.cadecode.uniboot.framework.request.SysRoleRequest;
2123
import top.cadecode.uniboot.framework.request.SysRoleRequest.SysRoleMappingRequest;
@@ -99,13 +101,15 @@ public List<SysRoleUnionVo> listUnionVoByRoleIds(@RequestBody @NotEmpty List<Lon
99101
return sysRoleService.listUnionVoByRoleIds(roleIds);
100102
}
101103

104+
@CacheEvict(cacheNames = KeyPrefix.API_ROLES, key = "'all'")
102105
@ApiOperation("更新角色")
103106
@PostMapping("update")
104107
public boolean update(@RequestBody @Valid SysRoleRequest.SysRoleUpdateRequest request) {
105108
SysRole po = SysRoleConvert.INSTANCE.requestToPo(request);
106109
return sysRoleService.updateById(po);
107110
}
108111

112+
@CacheEvict(cacheNames = KeyPrefix.API_ROLES, key = "'all'")
109113
@ApiOperation("添加角色")
110114
@PostMapping("add")
111115
public boolean add(@RequestBody @Valid SysRoleRequest.SysRoleAddRequest request) {

framework/src/main/java/top/cadecode/uniboot/framework/serviceimpl/SysApiServiceImpl.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package top.cadecode.uniboot.framework.serviceimpl;
22

3-
import cn.hutool.core.util.ObjectUtil;
43
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
5-
import com.fasterxml.jackson.core.type.TypeReference;
64
import com.github.pagehelper.PageHelper;
75
import com.github.pagehelper.PageInfo;
86
import lombok.RequiredArgsConstructor;
97
import org.springframework.cache.annotation.Cacheable;
108
import org.springframework.stereotype.Service;
11-
import top.cadecode.uniboot.common.plugin.cache.util.RedisUtil;
129
import top.cadecode.uniboot.framework.bean.po.SysApi;
1310
import top.cadecode.uniboot.framework.bean.vo.SysApiVo.SysApiRolesVo;
1411
import top.cadecode.uniboot.framework.consts.KeyPrefix;
@@ -17,7 +14,6 @@
1714
import top.cadecode.uniboot.framework.service.SysApiService;
1815

1916
import java.util.List;
20-
import java.util.concurrent.TimeUnit;
2117

2218
/**
2319
* 系统接口服务实现
@@ -31,23 +27,10 @@ public class SysApiServiceImpl extends ServiceImpl<SysApiMapper, SysApi> impleme
3127

3228
private final SysApiMapper sysApiMapper;
3329

34-
@Cacheable(cacheNames = KeyPrefix.API_ROLES, cacheManager = "caffeine5s")
30+
@Cacheable(cacheNames = KeyPrefix.API_ROLES, key = "'all'")
3531
@Override
3632
public List<SysApiRolesVo> listRolesVo() {
37-
List<SysApiRolesVo> sysApiRolesVos = RedisUtil.get(KeyPrefix.API_ROLES, new TypeReference<List<SysApiRolesVo>>() {});
38-
if (ObjectUtil.isNotNull(sysApiRolesVos)) {
39-
return sysApiRolesVos;
40-
}
41-
synchronized (this) {
42-
sysApiRolesVos = RedisUtil.get(KeyPrefix.API_ROLES, new TypeReference<List<SysApiRolesVo>>() {});
43-
if (ObjectUtil.isNotNull(sysApiRolesVos)) {
44-
return sysApiRolesVos;
45-
}
46-
List<SysApiRolesVo> voList = sysApiMapper.selectRolesVo(null);
47-
// 每 5 分钟刷新一次
48-
RedisUtil.set(KeyPrefix.API_ROLES, voList, 5, TimeUnit.MINUTES);
49-
return voList;
50-
}
33+
return sysApiMapper.selectRolesVo(null);
5134
}
5235

5336
@Override

framework/src/main/java/top/cadecode/uniboot/framework/serviceimpl/SysRoleServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import com.github.pagehelper.PageHelper;
55
import com.github.pagehelper.PageInfo;
66
import lombok.RequiredArgsConstructor;
7+
import org.springframework.cache.annotation.CacheEvict;
78
import org.springframework.stereotype.Service;
89
import top.cadecode.uniboot.framework.bean.po.SysRole;
910
import top.cadecode.uniboot.framework.bean.vo.SysRoleVo.SysRoleUnionVo;
11+
import top.cadecode.uniboot.framework.consts.KeyPrefix;
1012
import top.cadecode.uniboot.framework.mapper.SysRoleMapper;
1113
import top.cadecode.uniboot.framework.request.SysRoleRequest.SysRoleMappingRequest;
1214
import top.cadecode.uniboot.framework.request.SysRoleRequest.SysRoleUnionRequest;
@@ -61,11 +63,13 @@ public int removeRoleMenuByRoleIds(List<Long> roleIds) {
6163
return sysRoleMapper.deleteRoleMenuByRoleIds(roleIds);
6264
}
6365

66+
@CacheEvict(cacheNames = KeyPrefix.API_ROLES, key = "'all'")
6467
@Override
6568
public int removeRoleApiByApiIds(List<Long> apiIds) {
6669
return sysRoleMapper.deleteRoleApiByApiIds(apiIds);
6770
}
6871

72+
@CacheEvict(cacheNames = KeyPrefix.API_ROLES, key = "'all'")
6973
@Override
7074
public int removeRoleApiByRoleIds(List<Long> roleIds) {
7175
return sysRoleMapper.deleteRoleApiByRoleIds(roleIds);
@@ -81,6 +85,7 @@ public int removeRoleMenu(List<SysRoleMappingRequest> list) {
8185
return sysRoleMapper.deleteRoleMenu(list);
8286
}
8387

88+
@CacheEvict(cacheNames = KeyPrefix.API_ROLES, key = "'all'")
8489
@Override
8590
public int removeRoleApi(List<SysRoleMappingRequest> list) {
8691
return sysRoleMapper.deleteRoleApi(list);
@@ -96,6 +101,7 @@ public int addRoleMenu(List<SysRoleMappingRequest> list) {
96101
return sysRoleMapper.insertRoleMenu(list);
97102
}
98103

104+
@CacheEvict(cacheNames = KeyPrefix.API_ROLES, key = "'all'")
99105
@Override
100106
public int addRoleApi(List<SysRoleMappingRequest> list) {
101107
return sysRoleMapper.insertRoleApi(list);

0 commit comments

Comments
 (0)