spring缓存集成

master
chen 2 years ago
parent 1dc81a8051
commit 37bd21ccfc
  1. 6
      pom.xml
  2. 4
      src/main/java/com/huoran/iasf/common/config/MyBatisPlusConfig.java
  3. 48
      src/main/java/com/huoran/iasf/common/config/MyCacheConfig.java
  4. 29
      src/main/java/com/huoran/iasf/common/config/MyThreadConfig.java
  5. 51
      src/main/java/com/huoran/iasf/controller/PermissionController.java
  6. 8
      src/main/java/com/huoran/iasf/service/impl/DeptServiceImpl.java
  7. 8
      src/main/java/com/huoran/iasf/service/impl/PermissionServiceImpl.java
  8. 23
      src/main/resources/application-dev.yml

@ -38,6 +38,12 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

@ -18,6 +18,8 @@ public class MyBatisPlusConfig {
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
PaginationInterceptor page = new PaginationInterceptor();
page.setDialectType("mysql");
return page;
}
}

@ -0,0 +1,48 @@
package com.huoran.iasf.common.config;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @Author chen
* @DATE 2021/12/28 10:42
* @Version 1.0
*/
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class MyCacheConfig {
@Bean
RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//修改缓存key、value的序列化机制,返回json数据格式
config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
//读取redis配置文件
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}
}

@ -0,0 +1,29 @@
package com.huoran.iasf.common.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Author chen
* @DATE 2021/12/28 17:48
* @Version 1.0
*/
@Configuration
public class MyThreadConfig {
@Bean
public ThreadPoolExecutor threadPoolExecutor(){
return new ThreadPoolExecutor(20,
300,
10,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
}
}

@ -14,7 +14,6 @@ import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
/**
* 菜单权限管理
@ -31,7 +30,7 @@ public class PermissionController {
@Resource
private PermissionService permissionService;
@PostMapping("/permission")
/* @PostMapping("/permission")
@ApiOperation(value = "新增菜单权限接口")
@LogAnnotation(title = "菜单权限管理", action = "新增菜单权限")
@RequiresPermissions("sys:permission:add")
@ -40,59 +39,15 @@ public class PermissionController {
vo.setStatus(1);
permissionService.save(vo);
return R.success();
}
}*/
@DeleteMapping("/permission/{id}")
/* @DeleteMapping("/permission/{id}")
@ApiOperation(value = "删除菜单权限接口")
@LogAnnotation(title = "菜单权限管理", action = "删除菜单权限")
@RequiresPermissions("sys:permission:deleted")
public R deleted(@PathVariable("id") String id) {
permissionService.deleted(id);
return R.success();
}
/* @PutMapping("/permission")
@ApiOperation(value = "更新菜单权限接口")
@LogAnnotation(title = "菜单权限管理", action = "更新菜单权限")
@RequiresPermissions("sys:permission:update")
public R updatePermission(@RequestBody @Valid SysPermission vo) {
if (StringUtils.isEmpty(vo.getId())) {
return R.fail("id不能为空");
}
SysPermission sysPermission = permissionService.getById(vo.getId());
if (null == sysPermission) {
throw new BusinessException(BaseResponseCode.DATA_ERROR);
}
// 只有类型变更或者所属菜单变更
if (sysPermission.getType().equals(vo.getType()) || !sysPermission.getPid().equals(vo.getPid())) {
verifyFormPid(vo);
}
permissionService.updatePermission(vo);
return R.success();
}*/
/* @GetMapping("/permission/{id}")
@ApiOperation(value = "查询菜单权限接口")
@LogAnnotation(title = "菜单权限管理", action = "查询菜单权限")
@RequiresPermissions("sys:permission:detail")
public R detailInfo(@PathVariable("id") String id) {
return R.success(permissionService.getById(id));
}*/
/*@GetMapping("/permissions")
@ApiOperation(value = "获取所有菜单权限接口")
@LogAnnotation(title = "菜单权限管理", action = "获取所有菜单权限")
@RequiresPermissions("sys:permission:list")
public R getAllMenusPermission() {
return R.success(permissionService.selectAll());
}*/
/* @GetMapping("/permission/tree")
@ApiOperation(value = "获取所有目录菜单树接口")
@LogAnnotation(title = "菜单权限管理", action = "获取所有目录菜单树")
@RequiresPermissions(value = {"sys:permission:update", "sys:permission:add"}, logical = Logical.OR)
public R getAllMenusPermissionTree(@RequestParam(required = false) String permissionId) {
return R.success(permissionService.selectAllMenuByTree(permissionId));
}*/
@GetMapping("/permission/tree/all")

@ -89,7 +89,7 @@ public class DeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impleme
if (!"0".equals(vo.getPid()) && null == parent) {
throw new BusinessException(BaseResponseCode.DATA_ERROR);
}
SysDept oldParent = sysDeptMapper.selectById(sysDept.getPid());
/*SysDept oldParent = sysDeptMapper.selectById(sysDept.getPid());
String oldRelationCode;
String newRelationCode;
//根目录降到其他目录
@ -102,15 +102,15 @@ public class DeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impleme
} else {
oldRelationCode = oldParent.getRelationCode();
newRelationCode = parent.getRelationCode();
}
LambdaQueryWrapper<SysDept> wrapper = Wrappers.lambdaQuery();
}*/
/*LambdaQueryWrapper<SysDept> wrapper = Wrappers.lambdaQuery();
wrapper.likeLeft(SysDept::getDeptNo, sysDept.getDeptNo());
List<SysDept> list = sysDeptMapper.selectList(wrapper);
list.parallelStream().forEach(entity -> {
String relationCode = entity.getRelationCode().replace(oldRelationCode, newRelationCode);
entity.setRelationCode(relationCode);
sysDeptMapper.updateById(entity);
});
});*/
}
}

@ -18,6 +18,7 @@ import com.huoran.iasf.service.UserRoleService;
import com.huoran.iasf.vo.resp.PermissionRespNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
@ -106,7 +107,7 @@ public class PermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysP
queryWrapper.eq("user_id",userId);
List<SysUserRole> userRoles = userRoleService.list(queryWrapper);
String roleIds = "";
if (ObjectUtil.isNotNull(userRoles)){
if (ObjectUtil.isNotNull(userRoles)&& userRoles.size()>0){
for (SysUserRole userRole: userRoles) {
roleIds += userRole.getRoleId() +",";
}
@ -236,11 +237,8 @@ public class PermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysP
* 获取所有菜单权限按钮
*/
@Override
@Cacheable(value = {"permission"},key = "#root.methodName")
public List<SysPermission> selectAllByTree() {
// List<SysPermission> list = selectAll();
// return getTree(list, false);
List<SysPermission> permissionList = baseMapper.selectList(null);
return build(permissionList);
}

@ -6,18 +6,11 @@ spring:
datasource:
master:
username: root
password: HuoRan@2021
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://139.9.47.170:3306/iasf?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8
# hikari:
# connection-timeout: 60000
# validation-timeout: 3000
# idle-timeout: 60000
# login-timeout: 5
# max-lifetime: 60000
# maximum-pool-size: 10
# minimum-idle: 10
# read-only: false
url: jdbc:mysql://127.0.0.1:3306/iasf?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8
cache:
type: redis
redis:
host: localhost # Redis服务器地址
database: 0 # Redis数据库索引(默认为0)
@ -29,11 +22,15 @@ spring:
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
min-idle: 0 # 连接池中的最小空闲连接
timeout: 3000ms # 连接超时时间(毫秒
timeout: 3000ms # 连接超时时间(毫秒)
singleServerConfig:
address: "redis://127.0.0.1:6379"
password:
file:
#文件上传目录 绝对路径 末尾请加 /
path: D:/files/ #windows
path: C:/files/ #windows
#path: /data/files/ #linux
#文件预览url
url: :10000/iasf/sysFiles/preview/

Loading…
Cancel
Save