# 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 将当前缓存过时(清空) _________ 还有不少缓存注解有待读者去探索...