Skip to content

Commit

Permalink
增加 Spring Cache 框架
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Apr 3, 2022
1 parent 3263b0a commit 49b906b
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 33 deletions.
6 changes: 6 additions & 0 deletions yudao-framework/yudao-spring-boot-starter-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId> <!-- 实现对 Caches 的自动化配置 -->
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,7 +10,6 @@
* Redis 配置类
*/
@Configuration
@Slf4j
public class YudaoRedisAutoConfiguration {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Cache/?yudao>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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.*;
Expand Down Expand Up @@ -61,10 +60,6 @@ public class LoginUser implements UserDetails {
* 部门编号
*/
private Long deptId;
/**
* 所属岗位
*/
private Set<Long> postIds;

// ========== 上下文 ==========
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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 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 io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
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 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 static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 字典类型")
@RestController
Expand All @@ -37,13 +36,15 @@ public class TestDemoController {

@PostMapping("/create")
@ApiOperation("创建字典类型")
@PreAuthorize("@ss.hasPermission('infra:test-demo:create')") public CommonResult<Long> createTestDemo(@Valid @RequestBody TestDemoCreateReqVO createReqVO) {
@PreAuthorize("@ss.hasPermission('infra:test-demo:create')")
public CommonResult<Long> createTestDemo(@Valid @RequestBody TestDemoCreateReqVO createReqVO) {
return success(testDemoService.createTestDemo(createReqVO));
}

@PutMapping("/update")
@ApiOperation("更新字典类型")
@PreAuthorize("@ss.hasPermission('infra:test-demo:update')") public CommonResult<Boolean> updateTestDemo(@Valid @RequestBody TestDemoUpdateReqVO updateReqVO) {
@PreAuthorize("@ss.hasPermission('infra:test-demo:update')")
public CommonResult<Boolean> updateTestDemo(@Valid @RequestBody TestDemoUpdateReqVO updateReqVO) {
testDemoService.updateTestDemo(updateReqVO);
return success(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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;

Expand All @@ -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);
Expand All @@ -40,6 +43,7 @@ public Long createTestDemo(TestDemoCreateReqVO createReqVO) {
}

@Override
@CacheEvict(value = "test", key = "#updateReqVO.id")
public void updateTestDemo(TestDemoUpdateReqVO updateReqVO) {
// 校验存在
this.validateTestDemoExists(updateReqVO.getId());
Expand All @@ -49,6 +53,7 @@ public void updateTestDemo(TestDemoUpdateReqVO updateReqVO) {
}

@Override
@CacheEvict(value = "test", key = "'test:' + #id")
public void deleteTestDemo(Long id) {
// 校验存在
this.validateTestDemoExists(id);
Expand All @@ -63,6 +68,7 @@ private void validateTestDemoExists(Long id) {
}

@Override
@Cacheable(cacheNames = "test", key = "#id")
public TestDemoDO getTestDemo(Long id) {
return testDemoMapper.selectById(id);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
public interface DeptMapper extends BaseMapperX<DeptDO> {

default List<DeptDO> selectList(DeptListReqVO reqVO) {
return selectList(new LambdaQueryWrapperX<DeptDO>().likeIfPresent(DeptDO::getName, reqVO.getName())
return selectList(new LambdaQueryWrapperX<DeptDO>()
.likeIfPresent(DeptDO::getName, reqVO.getName())
.eqIfPresent(DeptDO::getStatus, reqVO.getStatus()));
}

default DeptDO selectByParentIdAndName(Long parentId, String name) {
return selectOne(new LambdaQueryWrapper<DeptDO>().eq(DeptDO::getParentId, parentId)
return selectOne(new LambdaQueryWrapper<DeptDO>()
.eq(DeptDO::getParentId, parentId)
.eq(DeptDO::getName, name));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void delete(String sessionId) {
}

private static String formatKey(String sessionId) {
return String.format(LOGIN_USER.getKeyTemplate(), sessionId);
return LOGIN_USER.formatKey(sessionId);
}

}
2 changes: 0 additions & 2 deletions yudao-server/src/main/resources/application-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions yudao-server/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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启动时,对比数据库表中保存的版本,如果不匹配。将抛出异常
Expand Down

0 comments on commit 49b906b

Please sign in to comment.