目录
1、简介
2、构建项目
3、注解、表达式语法
4、自定义缓存、缓存穿透解决
简介
1、概述
Spring 从 3.1开始定义了org.springframework.cache.Cache
和org.springframework.cache.CacheManager,接口来统一不同的缓存技术;并支持使用JCache (JSR107)注解简化我们开发;
Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
Cache接口下 Spring 提供了各种xxxCache…的实现﹔如RedisCache.,hCacheCache, ,ConcurrentMapCache等;
每次调用需要缓存功能的方法时,Spring 会检查检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。
使用Spring缓存抽象时我们需要关注以下两点;
(1)确定方法需要被缓存以及他们的缓存策略.
(2)从缓存中读取之前缓存存储的数据
2、关系图
二、构建项目
1、引入坐标
<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>
2、写配置
spring.cache.type=redis
spring.redis.host=101.34.181.131
spring.re