Commit 49b906bbfe27b31ce52bfe7403740ff5b335462e

Authored by YunaiV
1 parent 3263b0ab

增加 Spring Cache 框架

Showing 14 changed files with 111 additions and 33 deletions
yudao-framework/yudao-spring-boot-starter-redis/pom.xml
... ... @@ -26,6 +26,12 @@
26 26 <groupId>org.redisson</groupId>
27 27 <artifactId>redisson-spring-boot-starter</artifactId>
28 28 </dependency>
  29 +
  30 + <dependency>
  31 + <groupId>org.springframework.boot</groupId>
  32 + <artifactId>spring-boot-starter-cache</artifactId> <!-- 实现对 Caches 的自动化配置 -->
  33 + </dependency>
  34 +
29 35 </dependencies>
30 36  
31 37 </project>
... ...
yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoCacheAutoConfiguration.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.redis.config;
  2 +
  3 +import org.springframework.boot.autoconfigure.cache.CacheProperties;
  4 +import org.springframework.cache.annotation.EnableCaching;
  5 +import org.springframework.context.annotation.Bean;
  6 +import org.springframework.context.annotation.Configuration;
  7 +import org.springframework.context.annotation.Primary;
  8 +import org.springframework.data.redis.cache.RedisCacheConfiguration;
  9 +import org.springframework.data.redis.serializer.RedisSerializationContext;
  10 +import org.springframework.data.redis.serializer.RedisSerializer;
  11 +
  12 +/**
  13 + * Cache 配置类,基于 Redis 实现
  14 + */
  15 +@Configuration
  16 +@EnableCaching
  17 +public class YudaoCacheAutoConfiguration {
  18 +
  19 + /**
  20 + * RedisCacheConfiguration Bean
  21 + *
  22 + * 参考 org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration 的 createConfiguration 方法
  23 + */
  24 + @Bean
  25 + @Primary
  26 + public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
  27 + // 设置使用 JSON 序列化方式
  28 + RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
  29 + config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
  30 +
  31 + // 设置 CacheProperties.Redis 的属性
  32 + CacheProperties.Redis redisProperties = cacheProperties.getRedis();
  33 + if (redisProperties.getTimeToLive() != null) {
  34 + config = config.entryTtl(redisProperties.getTimeToLive());
  35 + }
  36 + if (redisProperties.getKeyPrefix() != null) {
  37 + config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
  38 + }
  39 + if (!redisProperties.isCacheNullValues()) {
  40 + config = config.disableCachingNullValues();
  41 + }
  42 + if (!redisProperties.isUseKeyPrefix()) {
  43 + config = config.disableKeyPrefix();
  44 + }
  45 + return config;
  46 + }
  47 +
  48 +}
... ...
yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoRedisAutoConfiguration.java
1 1 package cn.iocoder.yudao.framework.redis.config;
2 2  
3   -import lombok.extern.slf4j.Slf4j;
4 3 import org.springframework.context.annotation.Bean;
5 4 import org.springframework.context.annotation.Configuration;
6 5 import org.springframework.data.redis.connection.RedisConnectionFactory;
... ... @@ -11,7 +10,6 @@ import org.springframework.data.redis.serializer.RedisSerializer;
11 10 * Redis 配置类
12 11 */
13 12 @Configuration
14   -@Slf4j
15 13 public class YudaoRedisAutoConfiguration {
16 14  
17 15 /**
... ...
yudao-framework/yudao-spring-boot-starter-redis/src/main/resources/META-INF/spring.factories
1 1 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2   - cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration
  2 + cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration,\
  3 + cn.iocoder.yudao.framework.redis.config.YudaoCacheAutoConfiguration
... ...
yudao-framework/yudao-spring-boot-starter-redis/《芋道 Spring Boot Cache 入门》.md 0 → 100644
  1 +<http://www.iocoder.cn/Spring-Boot/Cache/?yudao>
... ...
yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java
... ... @@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
6 6 import com.fasterxml.jackson.annotation.JsonIgnore;
7 7 import lombok.Data;
8 8 import org.springframework.security.core.GrantedAuthority;
9   -import org.springframework.security.core.authority.SimpleGrantedAuthority;
10 9 import org.springframework.security.core.userdetails.UserDetails;
11 10  
12 11 import java.util.*;
... ... @@ -61,10 +60,6 @@ public class LoginUser implements UserDetails {
61 60 * 部门编号
62 61 */
63 62 private Long deptId;
64   - /**
65   - * 所属岗位
66   - */
67   - private Set<Long> postIds;
68 63  
69 64 // ========== 上下文 ==========
70 65 /**
... ...
yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.http 0 → 100644
  1 +### 请求 /infra/test-demo/get 接口 => 成功
  2 +GET {{baseUrl}}/infra/test-demo/get?id=106
  3 +Authorization: Bearer {{token}}
  4 +tenant-id: {{adminTenentId}}
  5 +
  6 +### 请求 /infra/test-demo/update 接口 => 成功
  7 +PUT {{baseUrl}}/infra/test-demo/update
  8 +Authorization: Bearer {{token}}
  9 +tenant-id: {{adminTenentId}}
  10 +Content-Type: application/json
  11 +
  12 +
  13 +{
  14 + "id": 106,
  15 + "name": "测试",
  16 + "status": "0",
  17 + "type": 1,
  18 + "category": 1
  19 +}
... ...
yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.java
1 1 package cn.iocoder.yudao.module.infra.controller.admin.test;
2 2  
  3 +import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  4 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  5 +import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
  6 +import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
3 7 import cn.iocoder.yudao.module.infra.controller.admin.test.vo.*;
4 8 import cn.iocoder.yudao.module.infra.convert.test.TestDemoConvert;
5 9 import cn.iocoder.yudao.module.infra.dal.dataobject.test.TestDemoDO;
6   -import org.springframework.web.bind.annotation.*;
7   -import javax.annotation.Resource;
8   -import org.springframework.validation.annotation.Validated;
  10 +import cn.iocoder.yudao.module.infra.service.test.TestDemoService;
  11 +import io.swagger.annotations.Api;
  12 +import io.swagger.annotations.ApiImplicitParam;
  13 +import io.swagger.annotations.ApiOperation;
9 14 import org.springframework.security.access.prepost.PreAuthorize;
10   -import io.swagger.annotations.*;
  15 +import org.springframework.validation.annotation.Validated;
  16 +import org.springframework.web.bind.annotation.*;
11 17  
12   -import javax.validation.*;
13   -import javax.servlet.http.*;
14   -import java.util.*;
  18 +import javax.annotation.Resource;
  19 +import javax.servlet.http.HttpServletResponse;
  20 +import javax.validation.Valid;
15 21 import java.io.IOException;
  22 +import java.util.Collection;
  23 +import java.util.List;
16 24  
17   -import cn.iocoder.yudao.framework.common.pojo.PageResult;
18   -import cn.iocoder.yudao.framework.common.pojo.CommonResult;
19 25 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
20   -
21   -import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
22   -
23   -import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
24   -import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
25   -
26   -import cn.iocoder.yudao.module.infra.controller.admin.test.vo.*;
27   -import cn.iocoder.yudao.module.infra.service.test.TestDemoService;
  26 +import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
28 27  
29 28 @Api(tags = "管理后台 - 字典类型")
30 29 @RestController
... ... @@ -37,13 +36,15 @@ public class TestDemoController {
37 36  
38 37 @PostMapping("/create")
39 38 @ApiOperation("创建字典类型")
40   - @PreAuthorize("@ss.hasPermission('infra:test-demo:create')") public CommonResult<Long> createTestDemo(@Valid @RequestBody TestDemoCreateReqVO createReqVO) {
  39 + @PreAuthorize("@ss.hasPermission('infra:test-demo:create')")
  40 + public CommonResult<Long> createTestDemo(@Valid @RequestBody TestDemoCreateReqVO createReqVO) {
41 41 return success(testDemoService.createTestDemo(createReqVO));
42 42 }
43 43  
44 44 @PutMapping("/update")
45 45 @ApiOperation("更新字典类型")
46   - @PreAuthorize("@ss.hasPermission('infra:test-demo:update')") public CommonResult<Boolean> updateTestDemo(@Valid @RequestBody TestDemoUpdateReqVO updateReqVO) {
  46 + @PreAuthorize("@ss.hasPermission('infra:test-demo:update')")
  47 + public CommonResult<Boolean> updateTestDemo(@Valid @RequestBody TestDemoUpdateReqVO updateReqVO) {
47 48 testDemoService.updateTestDemo(updateReqVO);
48 49 return success(true);
49 50 }
... ...
yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/test/TestDemoServiceImpl.java
... ... @@ -8,6 +8,8 @@ import cn.iocoder.yudao.module.infra.controller.admin.test.vo.TestDemoUpdateReqV
8 8 import cn.iocoder.yudao.module.infra.convert.test.TestDemoConvert;
9 9 import cn.iocoder.yudao.module.infra.dal.dataobject.test.TestDemoDO;
10 10 import cn.iocoder.yudao.module.infra.dal.mysql.test.TestDemoMapper;
  11 +import org.springframework.cache.annotation.CacheEvict;
  12 +import org.springframework.cache.annotation.Cacheable;
11 13 import org.springframework.stereotype.Service;
12 14 import org.springframework.validation.annotation.Validated;
13 15  
... ... @@ -31,6 +33,7 @@ public class TestDemoServiceImpl implements TestDemoService {
31 33 private TestDemoMapper testDemoMapper;
32 34  
33 35 @Override
  36 + @CacheEvict(value = "test", key = "#createReqVO.id")
34 37 public Long createTestDemo(TestDemoCreateReqVO createReqVO) {
35 38 // 插入
36 39 TestDemoDO testDemo = TestDemoConvert.INSTANCE.convert(createReqVO);
... ... @@ -40,6 +43,7 @@ public class TestDemoServiceImpl implements TestDemoService {
40 43 }
41 44  
42 45 @Override
  46 + @CacheEvict(value = "test", key = "#updateReqVO.id")
43 47 public void updateTestDemo(TestDemoUpdateReqVO updateReqVO) {
44 48 // 校验存在
45 49 this.validateTestDemoExists(updateReqVO.getId());
... ... @@ -49,6 +53,7 @@ public class TestDemoServiceImpl implements TestDemoService {
49 53 }
50 54  
51 55 @Override
  56 + @CacheEvict(value = "test", key = "'test:' + #id")
52 57 public void deleteTestDemo(Long id) {
53 58 // 校验存在
54 59 this.validateTestDemoExists(id);
... ... @@ -63,6 +68,7 @@ public class TestDemoServiceImpl implements TestDemoService {
63 68 }
64 69  
65 70 @Override
  71 + @Cacheable(cacheNames = "test", key = "#id")
66 72 public TestDemoDO getTestDemo(Long id) {
67 73 return testDemoMapper.selectById(id);
68 74 }
... ...
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/package-info.java deleted
1   -package cn.iocoder.yudao.module.system.dal.dataobject;
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java
... ... @@ -15,12 +15,14 @@ import java.util.List;
15 15 public interface DeptMapper extends BaseMapperX<DeptDO> {
16 16  
17 17 default List<DeptDO> selectList(DeptListReqVO reqVO) {
18   - return selectList(new LambdaQueryWrapperX<DeptDO>().likeIfPresent(DeptDO::getName, reqVO.getName())
  18 + return selectList(new LambdaQueryWrapperX<DeptDO>()
  19 + .likeIfPresent(DeptDO::getName, reqVO.getName())
19 20 .eqIfPresent(DeptDO::getStatus, reqVO.getStatus()));
20 21 }
21 22  
22 23 default DeptDO selectByParentIdAndName(Long parentId, String name) {
23   - return selectOne(new LambdaQueryWrapper<DeptDO>().eq(DeptDO::getParentId, parentId)
  24 + return selectOne(new LambdaQueryWrapper<DeptDO>()
  25 + .eq(DeptDO::getParentId, parentId)
24 26 .eq(DeptDO::getName, name));
25 27 }
26 28  
... ...
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/redis/auth/LoginUserRedisDAO.java
... ... @@ -41,7 +41,7 @@ public class LoginUserRedisDAO {
41 41 }
42 42  
43 43 private static String formatKey(String sessionId) {
44   - return String.format(LOGIN_USER.getKeyTemplate(), sessionId);
  44 + return LOGIN_USER.formatKey(sessionId);
45 45 }
46 46  
47 47 }
... ...
yudao-server/src/main/resources/application-local.yaml
... ... @@ -44,14 +44,12 @@ spring:
44 44 datasource:
45 45 master:
46 46 name: ruoyi-vue-pro
47   -# name: ruoyi-vue-pro-flowable
48 47 url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
49 48 driver-class-name: com.mysql.jdbc.Driver
50 49 username: root
51 50 password: 123456
52 51 slave: # 模拟从库,可根据自己需要修改
53 52 name: ruoyi-vue-pro
54   -# name: ruoyi-vue-pro-flowable
55 53 url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
56 54 driver-class-name: com.mysql.jdbc.Driver
57 55 username: root
... ...
yudao-server/src/main/resources/application.yaml
... ... @@ -20,6 +20,10 @@ spring:
20 20 write-durations-as-timestamps: true # 设置 Duration 的格式,使用时间戳
21 21 fail-on-empty-beans: false # 允许序列化无属性的 Bean
22 22  
  23 + # Cache 配置项
  24 + cache:
  25 + type: REDIS
  26 +
23 27 # 工作流 Activiti 配置
24 28 activiti:
25 29 # 1. false: 默认值,activiti启动时,对比数据库表中保存的版本,如果不匹配。将抛出异常
... ...