From 49b906bbfe27b31ce52bfe7403740ff5b335462e Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 3 Apr 2022 13:23:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Spring=20Cache=20=E6=A1=86?= =?UTF-8?q?=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao-spring-boot-starter-redis/pom.xml | 6 +++ .../config/YudaoCacheAutoConfiguration.java | 48 +++++++++++++++++++ .../config/YudaoRedisAutoConfiguration.java | 2 - .../main/resources/META-INF/spring.factories | 3 +- .../《芋道 Spring Boot Cache 入门》.md | 1 + .../framework/security/core/LoginUser.java | 5 -- .../admin/test/TestDemoController.http | 19 ++++++++ .../admin/test/TestDemoController.java | 47 +++++++++--------- .../service/test/TestDemoServiceImpl.java | 6 +++ .../system/dal/dataobject/package-info.java | 1 - .../system/dal/mysql/dept/DeptMapper.java | 6 ++- .../dal/redis/auth/LoginUserRedisDAO.java | 2 +- .../src/main/resources/application-local.yaml | 2 - .../src/main/resources/application.yaml | 4 ++ 14 files changed, 115 insertions(+), 37 deletions(-) create mode 100644 yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoCacheAutoConfiguration.java create mode 100644 yudao-framework/yudao-spring-boot-starter-redis/《芋道 Spring Boot Cache 入门》.md create mode 100644 yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.http delete mode 100644 yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/package-info.java diff --git a/yudao-framework/yudao-spring-boot-starter-redis/pom.xml b/yudao-framework/yudao-spring-boot-starter-redis/pom.xml index a1140ac06..750cd4881 100644 --- a/yudao-framework/yudao-spring-boot-starter-redis/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-redis/pom.xml @@ -26,6 +26,12 @@ org.redisson redisson-spring-boot-starter + + + org.springframework.boot + spring-boot-starter-cache + + diff --git a/yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoCacheAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoCacheAutoConfiguration.java new file mode 100644 index 000000000..e8c716203 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoCacheAutoConfiguration.java @@ -0,0 +1,48 @@ +package cn.iocoder.yudao.framework.redis.config; + +import org.springframework.boot.autoconfigure.cache.CacheProperties; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.RedisSerializer; + +/** + * Cache 配置类,基于 Redis 实现 + */ +@Configuration +@EnableCaching +public class YudaoCacheAutoConfiguration { + + /** + * RedisCacheConfiguration Bean + * + * 参考 org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration 的 createConfiguration 方法 + */ + @Bean + @Primary + public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) { + // 设置使用 JSON 序列化方式 + RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); + config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json())); + + // 设置 CacheProperties.Redis 的属性 + CacheProperties.Redis redisProperties = cacheProperties.getRedis(); + if (redisProperties.getTimeToLive() != null) { + config = config.entryTtl(redisProperties.getTimeToLive()); + } + if (redisProperties.getKeyPrefix() != null) { + config = config.prefixCacheNameWith(redisProperties.getKeyPrefix()); + } + if (!redisProperties.isCacheNullValues()) { + config = config.disableCachingNullValues(); + } + if (!redisProperties.isUseKeyPrefix()) { + config = config.disableKeyPrefix(); + } + return config; + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoRedisAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoRedisAutoConfiguration.java index 8f4b5ad6c..5a74a2bb2 100644 --- a/yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoRedisAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoRedisAutoConfiguration.java @@ -1,6 +1,5 @@ package cn.iocoder.yudao.framework.redis.config; -import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; @@ -11,7 +10,6 @@ import org.springframework.data.redis.serializer.RedisSerializer; * Redis 配置类 */ @Configuration -@Slf4j public class YudaoRedisAutoConfiguration { /** diff --git a/yudao-framework/yudao-spring-boot-starter-redis/src/main/resources/META-INF/spring.factories b/yudao-framework/yudao-spring-boot-starter-redis/src/main/resources/META-INF/spring.factories index c76daf1a7..e4cefbab1 100644 --- a/yudao-framework/yudao-spring-boot-starter-redis/src/main/resources/META-INF/spring.factories +++ b/yudao-framework/yudao-spring-boot-starter-redis/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration + cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration,\ + cn.iocoder.yudao.framework.redis.config.YudaoCacheAutoConfiguration diff --git a/yudao-framework/yudao-spring-boot-starter-redis/《芋道 Spring Boot Cache 入门》.md b/yudao-framework/yudao-spring-boot-starter-redis/《芋道 Spring Boot Cache 入门》.md new file mode 100644 index 000000000..5c2708316 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-redis/《芋道 Spring Boot Cache 入门》.md @@ -0,0 +1 @@ + diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java index 3612e4905..84c7f94c4 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java +++ b/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; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import java.util.*; @@ -61,10 +60,6 @@ public class LoginUser implements UserDetails { * 部门编号 */ private Long deptId; - /** - * 所属岗位 - */ - private Set postIds; // ========== 上下文 ========== /** diff --git a/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.http b/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.http new file mode 100644 index 000000000..ed65d0b86 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.http @@ -0,0 +1,19 @@ +### 请求 /infra/test-demo/get 接口 => 成功 +GET {{baseUrl}}/infra/test-demo/get?id=106 +Authorization: Bearer {{token}} +tenant-id: {{adminTenentId}} + +### 请求 /infra/test-demo/update 接口 => 成功 +PUT {{baseUrl}}/infra/test-demo/update +Authorization: Bearer {{token}} +tenant-id: {{adminTenentId}} +Content-Type: application/json + + +{ + "id": 106, + "name": "测试", + "status": "0", + "type": 1, + "category": 1 +} diff --git a/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.java b/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.java index 352cb35b7..a534b7daf 100755 --- a/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.java +++ b/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/test/TestDemoController.java @@ -1,30 +1,29 @@ package cn.iocoder.yudao.module.infra.controller.admin.test; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; +import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; import cn.iocoder.yudao.module.infra.controller.admin.test.vo.*; import cn.iocoder.yudao.module.infra.convert.test.TestDemoConvert; import cn.iocoder.yudao.module.infra.dal.dataobject.test.TestDemoDO; -import org.springframework.web.bind.annotation.*; -import javax.annotation.Resource; -import org.springframework.validation.annotation.Validated; -import org.springframework.security.access.prepost.PreAuthorize; -import io.swagger.annotations.*; - -import javax.validation.*; -import javax.servlet.http.*; -import java.util.*; -import java.io.IOException; - -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.common.pojo.CommonResult; -import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; - -import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; - -import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; -import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*; - -import cn.iocoder.yudao.module.infra.controller.admin.test.vo.*; import cn.iocoder.yudao.module.infra.service.test.TestDemoService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiOperation; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import javax.validation.Valid; +import java.io.IOException; +import java.util.Collection; +import java.util.List; + +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; @Api(tags = "管理后台 - 字典类型") @RestController @@ -37,13 +36,15 @@ public class TestDemoController { @PostMapping("/create") @ApiOperation("创建字典类型") - @PreAuthorize("@ss.hasPermission('infra:test-demo:create')") public CommonResult createTestDemo(@Valid @RequestBody TestDemoCreateReqVO createReqVO) { + @PreAuthorize("@ss.hasPermission('infra:test-demo:create')") + public CommonResult createTestDemo(@Valid @RequestBody TestDemoCreateReqVO createReqVO) { return success(testDemoService.createTestDemo(createReqVO)); } @PutMapping("/update") @ApiOperation("更新字典类型") - @PreAuthorize("@ss.hasPermission('infra:test-demo:update')") public CommonResult updateTestDemo(@Valid @RequestBody TestDemoUpdateReqVO updateReqVO) { + @PreAuthorize("@ss.hasPermission('infra:test-demo:update')") + public CommonResult updateTestDemo(@Valid @RequestBody TestDemoUpdateReqVO updateReqVO) { testDemoService.updateTestDemo(updateReqVO); return success(true); } diff --git a/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/test/TestDemoServiceImpl.java b/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/test/TestDemoServiceImpl.java index 3014ad464..9be9ddf74 100755 --- a/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/test/TestDemoServiceImpl.java +++ b/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 import cn.iocoder.yudao.module.infra.convert.test.TestDemoConvert; import cn.iocoder.yudao.module.infra.dal.dataobject.test.TestDemoDO; import cn.iocoder.yudao.module.infra.dal.mysql.test.TestDemoMapper; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -31,6 +33,7 @@ public class TestDemoServiceImpl implements TestDemoService { private TestDemoMapper testDemoMapper; @Override + @CacheEvict(value = "test", key = "#createReqVO.id") public Long createTestDemo(TestDemoCreateReqVO createReqVO) { // 插入 TestDemoDO testDemo = TestDemoConvert.INSTANCE.convert(createReqVO); @@ -40,6 +43,7 @@ public class TestDemoServiceImpl implements TestDemoService { } @Override + @CacheEvict(value = "test", key = "#updateReqVO.id") public void updateTestDemo(TestDemoUpdateReqVO updateReqVO) { // 校验存在 this.validateTestDemoExists(updateReqVO.getId()); @@ -49,6 +53,7 @@ public class TestDemoServiceImpl implements TestDemoService { } @Override + @CacheEvict(value = "test", key = "'test:' + #id") public void deleteTestDemo(Long id) { // 校验存在 this.validateTestDemoExists(id); @@ -63,6 +68,7 @@ public class TestDemoServiceImpl implements TestDemoService { } @Override + @Cacheable(cacheNames = "test", key = "#id") public TestDemoDO getTestDemo(Long id) { return testDemoMapper.selectById(id); } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/package-info.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/package-info.java deleted file mode 100644 index 000c252fc..000000000 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package cn.iocoder.yudao.module.system.dal.dataobject; diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java index 56f6bdf4d..398ca3c8c 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java +++ b/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; public interface DeptMapper extends BaseMapperX { default List selectList(DeptListReqVO reqVO) { - return selectList(new LambdaQueryWrapperX().likeIfPresent(DeptDO::getName, reqVO.getName()) + return selectList(new LambdaQueryWrapperX() + .likeIfPresent(DeptDO::getName, reqVO.getName()) .eqIfPresent(DeptDO::getStatus, reqVO.getStatus())); } default DeptDO selectByParentIdAndName(Long parentId, String name) { - return selectOne(new LambdaQueryWrapper().eq(DeptDO::getParentId, parentId) + return selectOne(new LambdaQueryWrapper() + .eq(DeptDO::getParentId, parentId) .eq(DeptDO::getName, name)); } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/redis/auth/LoginUserRedisDAO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/redis/auth/LoginUserRedisDAO.java index 8132a882e..735015917 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/redis/auth/LoginUserRedisDAO.java +++ b/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 { } private static String formatKey(String sessionId) { - return String.format(LOGIN_USER.getKeyTemplate(), sessionId); + return LOGIN_USER.formatKey(sessionId); } } diff --git a/yudao-server/src/main/resources/application-local.yaml b/yudao-server/src/main/resources/application-local.yaml index 81d698300..1b4b038df 100644 --- a/yudao-server/src/main/resources/application-local.yaml +++ b/yudao-server/src/main/resources/application-local.yaml @@ -44,14 +44,12 @@ spring: datasource: master: name: ruoyi-vue-pro -# name: ruoyi-vue-pro-flowable 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 driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 slave: # 模拟从库,可根据自己需要修改 name: ruoyi-vue-pro -# name: ruoyi-vue-pro-flowable 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 driver-class-name: com.mysql.jdbc.Driver username: root diff --git a/yudao-server/src/main/resources/application.yaml b/yudao-server/src/main/resources/application.yaml index 7c99c5f5d..0bd91b999 100644 --- a/yudao-server/src/main/resources/application.yaml +++ b/yudao-server/src/main/resources/application.yaml @@ -20,6 +20,10 @@ spring: write-durations-as-timestamps: true # 设置 Duration 的格式,使用时间戳 fail-on-empty-beans: false # 允许序列化无属性的 Bean + # Cache 配置项 + cache: + type: REDIS + # 工作流 Activiti 配置 activiti: # 1. false: 默认值,activiti启动时,对比数据库表中保存的版本,如果不匹配。将抛出异常