Spring Cache

14 篇文章 0 订阅
4 篇文章 0 订阅

1.简介

Spring 从 3.1 开始定义了 org.springframework.cache.Cache,和 org.springframework.cache.CacheManager 接口来统一不同的缓存技术;并支持使用 JCache(JSR-107)注解简化我们开发;详情请参考官网添加链接描述
在这里插入图片描述
在这里插入图片描述

  • Cache 接口为缓存的组件规范定义,包含缓存的各种操作集合;Cache 接 口 下 Spring 提 供 了 各 种 xxxCache 的 实 现 ; 如 RedisCache , EhCacheCache , ConcurrentMapCache 等;
  • 每次调用需要缓存功能的方法时,Spring 会检查检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。
  • 使用 Spring 缓存抽象时我们需要关注以下两点;
  • 1、确定方法需要被缓存以及他们的缓存策略
  • 2、从缓存中读取之前缓存存储的数据

2.基础概念

在这里插入图片描述

3.整合Spring Cache

3.1.添加依赖

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

3.2.配置信息

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
创建application.properties

spring.cache.type=redis

#spring.cache.cache-names=qq,毫秒为单位
spring.cache.redis.time-to-live=3600000

#如果指定了前缀就用我们指定的前缀,如果没有就默认使用缓存的名字作为前缀
#spring.cache.redis.key-prefix=CACHE_
#spring.cache.redis.use-key-prefix=true

#是否缓存空值,防止缓存穿透
#spring.cache.redis.cache-null-values=true

#配置线程池
#gulimall.thread.coreSize=20
#gulimall.thread.maxSize=200
#gulimall.thread.keepAliveTime=10

#开启debug日志
#logging.level.org.springframework.cloud.openfeign=debug
#logging.level.org.springframework.cloud.sleuth=debug

3.3.启动类上加@EnableCaching

package com.atguigu.gulimall.product;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableCaching
@EnableFeignClients(basePackages = "com.atguigu.gulimall.product.feign")
@EnableDiscoveryClient
@SpringBootApplication
@MapperScan("com.atguigu.gulimall.product.dao")
public class GulimallProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallProductApplication.class, args);
    }

}

3.4.使用缓存注解

CategoryServiceImpl的getTopCategorys方法上加@Cacheable

 /**
  * 查询所有1级分类
  * @Cacheable代表当前方法的结果需要缓存,若缓存中有则方法不会调用,若缓存中没有会调用方法并将结果放入缓存
  * @return
  */
 @Cacheable({"category"})
 @Override
 public List<CategoryEntity> getTopCategorys() {
     long startTime = System.currentTimeMillis();
     List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(
             new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
     System.out.println("消耗时间:" + (System.currentTimeMillis() - startTime));
     return categoryEntityList;
 }

在这里插入图片描述
再次刷新首页方法不会被调用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值