Spring Cache
簡介
- Cache介面為快取的組件規范定義,包含快取的各種操作集合
- Cache介面下Spring提供了各種xxcache的實作;如RedisCache,EhCacheCache,ConcurrentMapCache等;
- 每次呼叫需要快取功能的方法時,Spring會檢查檢查指定引數的指定的目標方法是否已經被呼叫過;如果有就直接從快取中獲取方法呼叫后的結果,如果沒有就呼叫方法并快取結果后回傳給用戶,下次呼叫直接從快取中獲取,

引入依賴
<!--引入spring cache-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
配置
spring.cache.type=redis
開啟注解
@EnableCaching
常用注解
- @Cacheable:觸發操作將資料保存到快取中,引數value指cacheName
- @CacheEvict:觸發操作將資料從快取中洗掉(運用于失效模式)
- @CachePut:不影響方法執行更新(運用于雙寫模式)
- @Caching:組合以上多個操作
- @CacheConfig:再類級別上共享有關快取的配置
默認配置
- key自動生成:快取名字::SimpleKey[]
- value默認是jdk序列化
- 默認ttl時間:-1
- 默認查詢資料如果快取中存在,則不再呼叫方法,直接將快取中命中的資料回傳
自定義配置

-
指定key:
-
key默認為spel運算式:#root.method.name指用方法名作為key保存在快取中
@Cacheable(value="https://www.cnblogs.com/jklixin/p/category",key="#root.methodName")

-
key是字串的話額外要加單引號(默認會做運算式決議)
@Cacheable(value=https://www.cnblogs.com/jklixin/p/“category”,key=“‘catalogJson-cache’”)
-
快取key表達為:
-
指定前綴(前提:有指定前綴并且開啟了使用前綴)+指定key名

-
推薦: 默認前綴(快取名字,前提:允許使用前綴,沒有指定前綴):: key名字 (并且保存在以指定的快取名字namespace下,樹形結構顯示)

-
key名字(前提:無前綴)

-
-
-
指定其他資訊
spring.cache.type=redis #指定ttl,單位必需(m,s,h,d) spring.cache.redis.time-to-live=3600000m spring.cache.redis.key-prefix=CACHE_ spring.cache.redis.use-key-prefix=true #默認允許快取空值->解決快取穿透問題,暫時性保存null給其他并發執行緒回傳,以保護資料庫 spring.cache.redis.cache-null-values=true -
將value改為json格式(默認Jdk序列化器)
Redis序列化器:

@EnableCaching
@Configuration
public class MyCacheConfig {
@Bean
RedisCacheConfiguration cacheConfiguration(CacheProperties cacheProperties){
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//entryTtl方法回傳一個新的RedisCacheConfiguration物件覆寫原有物件,其他屬性方法也是如此
//cacheConfig=cacheConfig.entryTtl()
//config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
//整合組態檔(不整合組態檔所配置的內容不生效)
//取出配置資訊
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}
}
更新快取資料
1、@CacheEvict
用于快取失效模式:修改后洗掉該快取資訊
@CacheEvict 是用來標注在需要清除快取元素的方法或類上的
-
單個洗掉:
在方法上標注(一般為保存,更新方法)
@CacheEvict(value="https://www.cnblogs.com/jklixin/p/category",key="'getLevel1Categories'")表示一旦執行該方法則會從快取中洗掉對應的key
-
批量洗掉:
-
@Caching批量操作
@Caching(evict = { @CacheEvict(value="https://www.cnblogs.com/jklixin/p/category",key="'getLevel1Categories'"), @CacheEvict(value="https://www.cnblogs.com/jklixin/p/category",key="'getCatalogJson'") }) -
洗掉整個namespace(一般統一型別的資料存放在同一個磁區中)
@CacheEvict(value="https://www.cnblogs.com/jklixin/p/category",allEntries = true)- allEntries:默認為false,當其為true時表示清除快取中的所有元素
- beforeInvocation:默認是false,表示執行方法后洗掉(當方法如果因為拋出例外而未能成功回傳時也不會觸發清除操作),當指定該屬性值為true時,Spring會在呼叫該方法之前清除快取中的指定元素,
-
2、@CachePut
用于快取雙寫模式:修改后立即寫入快取
@CachePut標注的方法在執行前不會去檢查快取中是否存在之前執行過的結果,而是每次都會執行方法,并將結果(方法回傳值)存入指定的快取中
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/153787.html
標籤:Java
