Spring Cache

  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

    Spring Cache介绍

    Spring cache是一个框架实现了基于注解的缓存功能只需要简单地加一个注解就能实现缓存功能。

    Spring Cache提供了一层抽象底层可以切换不同的cache实现。具体就是通过CacheManager接口来统一不同的缓存技术。

    CacheManager是Spring提供的各种缓存技术抽象接口。

    针对不同的缓存技术需要实现不同的CacheManager:

    Spring Cache常用注解

    在spring boot项目中使用缓存技术只需在项目中导入相关缓存技术的依赖包并在启动类上使用@EnableCaching开启缓存支持即可。

    例如使用Redis作为缓存技术只需要导入Spring data Redis的maven坐标即可。

    Spring Cache使用方式

    在Spring Boot项目中使用Spring Cache的操作步骤(使用redis缓存技术);

    1、导入maven坐标

    • spring-boot-starter-data-redis、spring-boot-starter-cache

    2、配置application.yml

    spring:
        cache:
            redis:
                time-to-live: 1800000#设置缓存有效期
    

    3、在启动类上加入@EnableCaching注解开启缓存注解功能

    4、在Controller的方法上加入@Cacheable、@CacheEvict等注解进行缓存操作

    5.案例

     @Cacheable(value = "demo", key = "#id")
        @GetMapping("/{id}")
        public R<Employee> getById(@PathVariable Long id) {
            log.info("根据id查询员工信息");
            Employee byId = employeeService.getById(id);
            return R.success(byId);
        }
  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
    标签: Spring