# 1. 引入缓存依赖
``` xml

org.springframework.boot
spring-boot-starter-data-redis
2.1.5.RELEASE

```

# 2. 增加缓存配置
在application.properties文件中增加以下配置

``` text
## Redis部分
# Redis服务器地址
spring.redis.host=${redis.host}
# Redis服务器连接端口
spring.redis.port=${redis.port}
# Redis服务器连接密码(默认为空)
spring.redis.password=${redis.password}
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=${redis.maxTotal}
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1ms
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=${redis.maxIdle}
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=4
# 连接超时时间(毫秒)
spring.redis.timeout=5000

## Cache部分
#缓存的名称集合,多个采用逗号分割
spring.cache.cache-names=
#缓存的类型,官方提供了很多,这里我们填写redis
spring.cache.type=redis
#是否缓存null数据,默认是false
spring.cache.redis.cache-null-values=false
#redis中缓存超时的时间,默认60000ms
spring.cache.redis.time-to-live=60000
#缓存数据key是否使用前缀,默认是true
spring.cache.redis.use-key-prefix=true
#缓存数据key的前缀,在上面的配置为true时有效,
spring.cache.redis.key-prefix=
```

# 3. 增加开启缓存注解EnableCaching

``` java
@EnableCaching
public class WebApplication {

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

# 4. 增加缓存注解

## @Cacheable
该注解作用是标识这个方法返回值将会被缓存;
需要注意 `condition` 和 ` unless` ,它们都是条件判断参数:
- `condition`:在调用方法之前进行判断,所以不能将方法的结果值作为判断条件;
- `unless`:在调用方法之后进行判断,此时可以拿到方法放回值作为判断条件。
所以依赖方法返回值作为是否进行缓存的操作必须使用 ` unless` 参数,而不是 `condition`

## @CachePut
将方法返回值更新当前缓存

## @CacheEvict
将当前缓存过期(清空)

_________

还有很多缓存注解有待读者去探索...