整合springcache簡化快取開發

2021-10-24 08:20:21 字數 3873 閱讀 8431

由上篇的redis之redission的分布式鎖機制可知

可以用springcache的@cacheput註解和@cacheevict註解來實現快取資料一致性之雙寫模式、失效模式,就更加簡便

需要快取的業務

考慮快取的兩種用法模式

1.讀模式,如何讀取乙個資料,應該遵循先從快取中讀取,

如果快取中沒有,再在資料庫讀取,如果在資料庫查到資料則再放到快取中,並返回

2.寫模式,如何保證快取中的資料和資料庫中的資料是一致的

可以使用雙寫模式或失效模式

雙寫模式:如果修改資料。如果快取中有,則可以改完資料庫中的資料後,再改快取中的資料,把快取中以前的資料覆蓋掉

失效模式:改完資料庫資料以後,可以把快取中的資料直接清除掉,可以保證下一次從快取中拿到的資料是最新的

如果出現資料不一致,可以通過加鎖來保證順序一致性問題,來達到快取與資料庫的最終一致

整合springcach簡化快取開發

1)引入依賴

spring-boot-starter-cache、spring-boot-starter-data-redis

2)配置

(1) cacheautoconfiguration會匯入 rediscacheconfiguration,自動配置好了快取管理器rediscachemanager

(2)配置使用redis使用快取

spring.cache.type=redis

3)測試使用快取

@cacheable: triggers cache population.觸發將資料儲存到快取的操作

@cacheevict: triggers cache eviction.觸發將資料從快取中刪除的操作

@cacheput: updates the cache without interfering with the method execution.不影響方法執行更新快取

@cacheconfig: shares some common cache-related settings at class-level.在類級別共享快取的相同配置

1)開啟快取功能 @enablecaching

2) 只需要使用註解就能完成快取操作

1.每乙個需要快取的資料我們都來指定要放到哪個名字的快取中。【相當於快取的分割槽(按照業務型別分)】

2.@cacheable()代表當前方法的結果需要快取,如果快取中有,方法不呼叫。如果快取中沒有會呼叫方法,最後將方法的結果放入快取

3.預設行為

1)如果快取中有,則方法不呼叫

2)key預設自動生成:快取的名字::******key(自主生成的key值) category::getlevelcategorys

3)快取的value值,預設使用jdk序列化機制,將序列化後的資料存到redis

4)預設ttl時間 -1:永不過期

自定義:

1)指定生成的快取使用的key 有key屬性指定,接受乙個spel

#root.methodname、#root.method.name、#root.target、#root.targetclass

#root.args[0]、#root.caches[0].name、#iban or #a0 (you can also use #p0 or #p<#arg> 、#result

2)指定快取資料的存活時間 配置檔案中修改ttl

3)將資料存為json格式 自定義rediscacheconfiguration即可

4.spring-cache的不足:

1)讀模式:

快取穿透:查詢出null資料。解決:快取空資料:ache-null-values=true

快取擊穿:大併發進來同時查詢乙個正好要過期的資料。解決辦法:加鎖 ?預設是無加鎖的,可在@cacheable中配置sync=true(加鎖,解決擊穿問題,加的是本地鎖)

快取雪崩:大量的key同時過期。解決:加隨機時間,加上過期時間。spring.cache.redis.time-to-live=360000

2)寫模式:(快取與資料庫的一致性)

1)讀寫鎖

2)引入canal,當感知到mysql中的資料被更新就會去更新快取

3)讀多寫多的場景,直接去資料庫查詢就行

原理:cachemanager(rediscachemanager)——>cache(rediscache)——>cache負責快取的讀寫

@cacheable(value = ,key = "#root.method.name")

@override

public listgetlevelcategorys()

用springcache來實現的快取操作,可以很簡便使用以下註解來實現雙寫模式、 失效模式,而不用再手動用各種分布式鎖去防止併發操作

可以看關於redis之redission的分布式鎖機制  

*@cacheable: triggers cache population.觸發將資料儲存到快取的操作

失效模式:*@cacheevict: triggers cache eviction.觸發將資料從快取中刪除的操作

雙寫模式:*@cacheput: updates the cache without interfering with the method execution.不影響方法執行更新快取,

@cacheconfig: shares some common cache-related settings at class-level.在類級別共享快取的相同配置

import org.springframework.boot.autoconfigure.cache.cacheproperties;

import org.springframework.boot.context.properties.enableconfigurationproperties;

import org.springframework.cache.annotation.enablecaching;

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.configuration;

import org.springframework.data.redis.cache.rediscacheconfiguration;

import org.springframework.data.redis.serializer.genericjackson2jsonredisserializer;

import org.springframework.data.redis.serializer.redisserializationcontext;

import org.springframework.data.redis.serializer.stringredisserializer;

//開啟屬性配置的繫結功能:將配置檔案中的屬性繫結到cacheproperties類中,並放入容器中

@enableconfigurationproperties(cacheproperties.class)

@enablecaching

@configuration

public class mycacheconfig

if (redisproperties.getkeyprefix() != null)

if (!redisproperties.iscachenullvalues())

if (!redisproperties.isusekeyprefix())

return config;

}}

spring cache簡單使用

spring從3.1起自帶了cache功能。可以快取乙個方法的返回值,也就是說如果有快取,spring就會直接使用快取值,而不會再去執行這個方法 cashe相關的功能是在spring context.4.2.5.release.jar這個jar包中的。然後,開啟cache註解,配置cachemana...

SpringCache自我學習

cacheable 把查詢出來的資料放到快取 cacheevict 更新 刪除模式 cacheput 更新 雙寫模式 cacheable value是分割槽名 key是快取名 configuration enablecaching 開啟快取 enableconfigurationproperties...

springCache註解詳解

1 首先springcache需要匯入一下依賴 org.springframework.boot spring boot starter cache 2.1 cacheable 2.1.1 cacheable 屬性 cacheable 將方法的執行結果進行快取 以後再要相同資料,直接找快取中獲取,不...