# 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 將當前緩存過時(清空) _________ 還有不少緩存註解有待讀者去探索...