spring boot中使用spring cache

原因

项目原来越慢,为了提升效率加入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();
    }
 }



相关推荐

  1. SpringCache使用配置

    2024-03-26 07:14:04       75 阅读
  2. 使用SpringCache缓存数据

    2024-03-26 07:14:04       66 阅读
  3. SpringCache

    2024-03-26 07:14:04       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-26 07:14:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 07:14:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 07:14:04       87 阅读
  4. Python语言-面向对象

    2024-03-26 07:14:04       96 阅读

热门阅读

  1. arm iic通信

    2024-03-26 07:14:04       45 阅读
  2. GPT-4:下一代人工智能的突破与挑战

    2024-03-26 07:14:04       40 阅读
  3. 使用GPT将文档生成问答对

    2024-03-26 07:14:04       35 阅读
  4. Spring和spring Boot的区别

    2024-03-26 07:14:04       31 阅读
  5. 考研复习时间表(3-4月)(待完善)

    2024-03-26 07:14:04       38 阅读
  6. 最长公共子序列力扣题

    2024-03-26 07:14:04       34 阅读