原因
项目原来越慢,为了提升效率加入spring cache 初步想法把数据库的压力减轻一点。
引入
pom 中加入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
项目用的是properties
加入配置
spring.redis.database=4
spring.redis.host=127.0.0.1
spring.redis.port=6380
spring.redis.password=xxxxxxx
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
spring.cache.redis.time-to-live=1800000
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/massage/serviceType")
public class ServiceTypeController {
@Autowired
private CacheManager cacheManager;
@ApiOperation("根据条件查询所有服务类型列表")
@GetMapping(value = "/list")
@PreAuthorize("hasAuthority('massage:applyOrders:read')")
@Cacheable(value = "admin-ServiceTypeController-getServiceTypeByPage", key = "#pageNum + '_' + #pageSize")
public Object getServiceTypeByPage(ServiceType entity,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
) {
try {
return new CommonResult().success(iServiceTypeService.page(new Page<ServiceType>(pageNum, pageSize), new QueryWrapper<>(entity)));
} catch (Exception e) {
log.error("根据条件查询所有服务类型列表:%s", e.getMessage(), e);
}
return new CommonResult().failed();
}
}