-
Spring緩存抽象
-
重要概念以及緩存註解
-
Cache:緩存接口,定義緩存操作,實現又:RedisCache,EhCacheCache,ConcurrentMapCache等
-
CacheManager:緩存管理器,管理各種緩存組件
-
@Cacheable:修飾方法,能夠根據方法的請求參數和返回結果進行緩存
-
@CacheEvict:修飾方法,清空緩存,例如從數據庫刪除某個用戶,那就需要把相應的緩存也刪除掉
-
@CachePut:修飾方法,調用函數,更新緩存
-
@Cacheable一旦緩存命中,就不再調用修飾的函數了
-
@CachePut一定會調用函數,並把值放到緩存,常用於更新操作,先調用函數,在更新緩存
-
-
@EnableCaching:開啓基於註解的緩存模式
-
keyGenerator:緩存數據時key的生成策略
-
serialize:緩存數據時value序列化策略
-
-
搭建環境
-
引入包
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-cache</artifactId>
-
</dependency>
-
-
使用緩存@Cacheablepublic String getUser(int id){System.out.println("緩存未命中");return "zhangsan";}
-
開啓基於註解的緩存
-
-
-
使用緩存
-
-
-
@Cacheable@Caching(cacheable={@Cacheable(),@Cacheable()},put={},evict = {})
-
屬性
-
cacheNames/value:數組,緩存名字,因爲緩存管理器下有許多緩存,所以這個指定從哪個緩存中查找數據,或者將數據存入哪個緩存中,可以將一份數據存到多個緩存
-
@Cacheable(cacheNames="emp")
-
-
key:存緩存或者查找緩存value時用的鍵,支持spel表達式。默認key爲參數的值
-
@Cacheable的key不能用#result
-
keyGenerator:key的生成器,可以指定key生成器的組件id,key和keyGenerator兩個屬性二選一@Configurationpublic class CacheAutoConfiguration{@Bean("myKeyGenerator")public KeyGenerator keyGenerator(){return new KeyGenerator() {@Overridepublic Object generate(Object target, Method method, Object... params) {return "hahaha";}}}}
-
自定義keyGenerator
-
使用自定義的keyGenerator
-
@Cacheable(cacheNames = "emp", keyGenerator = "myKeyGenerator")
-
-
-
cacheManager:指定從哪個緩存管理器中拿到緩存,
-
cacheResolver:指定尋找 cacheManager的策略,cacheResolver與cacheManager二選一
-
condition:可以使用spel表達式,滿足條件才緩存
-
unless:滿足unless條件,則不緩存,可以獲取到結果進行判斷
-
@Cacheable(unless="#result == null")
-
-
sync:是否使用異步模式,默認false.意思是。在put數據時,是異步還是同步
-
-
原理
-
自動配置類:CacheAutoConfiguration
-
Import選擇器:CacheConfigurationImportSelector
-
org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration
-
org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration
-
org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration
-
org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration
-
org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration
-
org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration
-
org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
-
org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration
-
org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration
-
org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration
-
-
哪個配置生效呢?
-
每個CacheConfiguration上面都用註解標識了生效條件
-
SimpleCacheConfiguration這個有可能生效
-
-
SimpleCacheConfiguration給容器中注入了一個CacheManager(ConcurrentMapCacheManager)的bean
-
ConcurrentMapCacheManager維護了一個Map<String,ConcurrentMapCache>,String爲緩存的名字
-
-
ConcurrentMapCacheManager可以獲取和創建ConcurrentMapCache組件,並將數據放到ConcurrentMapCache中
-
ConcurrentMapCache就是一個類,屬性有name和ConcurrentMap
-
-
整個流程
-
-
@CachePut
-
先調用函數在更新緩存
-
注意點
-
@Cacheable添加緩存
-
@Cacheable(cacheNames = "emp", keyGenerator = "myKeyGenerator")
-
public String getUser(int id){
-
-
@CachePut添加緩存
-
@CachePut(cacheNames = "emp")
-
public Employee update(Employee emp){
-
-
這兩個緩存的key的生成可能不一楊,可能會導致他們更新的緩存原本應該是同一個,但是卻由於生成key不一樣,導致緩存更新錯誤
-
-
-
@CacheEvict
-
key:指定要清除的key
-
allEntries=true:清除緩存中的所有數據,默認false
-
beforeInvocation=true:實在方法執行之前清除緩存,還是在方法執行之後清除緩存,默認爲false,
-
如果在方法執行之後清除緩存,一旦方法出現異常,那麼緩存不會被清除
-
-
-
@Caching可以使用組合註解
-
@CacheConfig修飾類,可以將Cacheable,CachePut,CacheEvict的公共屬性提取出來
-
-
-
整合redis使用緩存
-
引入依賴
-
<!-- springboot整合redis的starter-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-data-redis</artifactId>
-
</dependency>
-
<!-- redis如果要使用連接池技術,需要依賴這個包-->
-
<dependency>
-
<artifactId>commons-pool2</artifactId>
-
</dependency>
-
<!-- 由於我們需要更改對象序列化爲json存到redis中,需要這個包-->
-
<dependency>
-
<groupId>com.fasterxml.jackson.core</groupId>
-
<artifactId>jackson-core</artifactId>
-
<version>2.9.8</version>
-
</dependency>
-
<!-- 從redis讀取數據,反序列化爲對象時,綁定數據需要這個包 -->
-
<dependency>
-
<groupId>com.fasterxml.jackson.core</groupId>
-
<artifactId>jackson-databind</artifactId>
-
<version>2.9.8</version>
-
</dependency>
-
-
配置redis連接池
-
spring:
-
redis:
-
host: 127.0.0.1
-
port: 6379
-
database: 0
-
lettuce:
-
pool:
-
# 連接池最大連接數 默認8 ,負數表示沒有限制
-
max-active: 20
-
# 連接池中的最大空閒連接 默認8
-
max-idle: 10
-
# 連接池最大阻塞等待時間(使用負值表示沒有限制) 默認-1
-
max-wait: 1000
-
-
-
timeout: 10000
-
-
-
-
修改cacheManager的默認配置
-
public CacheManager cacheManager(LettuceConnectionFactory redisConnectionFactory) {//初始化一個RedisCacheWriterRedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);//設置CacheManager的值序列化方式爲json序列化Jackson2JsonRedisSerializer<Person> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Person.class);RedisSerializationContext.SerializationPair<Person> pair = RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer);RedisCacheConfiguration defaultCacheConfig= RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair).entryTtl(Duration.ofSeconds(30))//設置默認超過期時間是30秒.disableCachingNullValues();//初始化RedisCacheManagerreturn new RedisCacheManager(redisCacheWriter, defaultCacheConfig);}
-
使用註解即可
-
開啓緩存@EnableCaching
-
在相應方法上添加註解
-
@Cacheable(cacheNames = "statuschecker")
-
public Person request(int id){
-
System.out.println("Cache未命中");
-
return new Person("www",24);
-
-
}
-
-
-
Springboot緩存與redis整合
Springboot緩存
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.