首页 > 其他分享 >77、缓存---SpringCache---整合&体验@Cacheable

77、缓存---SpringCache---整合&体验@Cacheable

时间:2023-02-02 00:13:21浏览次数:51  
标签:77 缓存 SpringCache boot 调用 redis --- Cacheable

1、导入依赖

        <!--缓存-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

由于我们使用redis作为缓存,所以导入redis的依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2、写配置
(1)自动配置了哪些
CacheAutoConfiguration会导入RedisCacheConfiguration
自动配置好了缓存管理器RedisCacheManager
(2)配置使用redis作为缓存

(3)测试使用缓存

@Caching:组合以上多个操作
@CacheConfig:在类级别共享缓存的相同配置
开启缓存功能:在Springboot的Application类上加上注解@EnableCaching
只需要注解就能完成缓存操作

     * 1、每一个需要缓存的数据都需要我们指定放到那个名字的缓存【缓存的分区(按照业务类型分)】
     * 2、@Cacheable({"catagory"})
     *          代表当前方法的结果需要缓存,如果缓存中有,方法不调用
     *          如果缓存中没有,会调用方法,最后将方法的结果放入缓存


我们第一次调用会打印,但是以后调用就不会打印,并且redis中存储了我们的catalog数据

标签:77,缓存,SpringCache,boot,调用,redis,---,Cacheable
From: https://www.cnblogs.com/morehair/p/17084547.html

相关文章