SpringBoot2.X整合Spring-Cache緩存開發(fā)的實(shí)現(xiàn)
引入依賴
<!-- 引入redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency><!-- 引入SpringCache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>配置
自動(dòng)配置
CacheAutoConfiguration會(huì)導(dǎo)入 RedisCacheConfiguration;自動(dòng)配置好了緩存管理器RedisCacheManager
配置使用redis作為緩存
spring.cache.type=redis
測試使用緩存
- @Cacheable: Triggers cache population. 觸發(fā)將數(shù)據(jù)保存到緩存的操作
- @CacheEvict: Triggers cache eviction. 觸發(fā)將數(shù)據(jù)從緩存刪除的操作
- @CachePut: Updates the cache without interfering with the method execution.不影響方法執(zhí)行更新緩存
- @Caching: Regroups multiple cache operations to be applied on a method.組合以上多個(gè)操作
- @CacheConfig: Shares some common cache-related settings at class-level.在類級別共享緩存的相同配置
@Cacheable注解的使用
- config中開啟緩存功能 @EnableCaching
- 只需要使用注解就能完成緩存操作
/**
?* 1、每一個(gè)需要緩存的數(shù)據(jù)我們都來指定要放到哪個(gè)名字的緩存?!揪彺娴姆謪^(qū)(按照業(yè)務(wù)類型分)】
?* 2、@Cacheable({"category"})
?* ? ? ?代表當(dāng)前方法的結(jié)果需要緩存,如果緩存中有,方法不再調(diào)用。
?* ? ? ?如果緩存中沒有,會(huì)調(diào)用方法,最后將方法的結(jié)果放入緩存。
?* 3、默認(rèn)行為
?* ? ? ?1)、如果緩存中有,方法不用調(diào)用。
?* ? ? ?2)、key默認(rèn)自動(dòng)生成:格式:緩存的名字::SimpleKey [](自主生成的key值) 例:category::SimpleKey []
?* ? ? ?3)、緩存的value值,默認(rèn)使用jdk序列化機(jī)制。將序列化后的數(shù)據(jù)存到redis
?* ? ? ?4)、默認(rèn)ttl時(shí)間:-1;
?*
?* ? 自定義:
?* ? ? ?1)、指定生成的緩存使用的key key屬性指定,接受一個(gè)SpEl @Cacheable(value = {"category"}, key = "#root.method.name")
??? ??? ??? ??? ?key的SpEl可以參考:https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/integration.html#cache-spel-context
?* ? ? ?2)、指定緩存的數(shù)據(jù)的存活時(shí)間 spring.cache.redis.time-to-live=3600000
?* ? ? ?3)、將數(shù)據(jù)保存為json格式
?*
?*
?* @return
?*/
@Cacheable(value = {"category"}, key = "#root.method.name")
@Override
public List<CategoryEntity> findCatelog1() {
? System.out.println("查詢數(shù)據(jù)庫---");
? return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
}指定緩存數(shù)據(jù)的存活時(shí)間
spring.cache.redis.time-to-live=3600000
將數(shù)據(jù)保存為json格式:配置
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching // 開啟緩存
public class MyCacheConfig {
? ? /**
? ? ?* 配置文件中的東西沒有用上
? ? ?*
? ? ?* 1、原來和配置文件綁定的配置類是這樣的
? ? ?* ? ? ?@ConfigurationProperties(prefix = "spring.cache")
? ? ?* ? ? ?public class CacheProperties
? ? ?*
? ? ?* 2、要讓他生效
? ? ?* ? ? ?@EnableConfigurationProperties(CacheProperties.class)
? ? ?* @return
? ? ?*/
? ? @Bean
? ? public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
? ? ? ? RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
? ? ? ? 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;
? ? }
}緩存的其他自定義配置
# 如果指定了前綴就用我們指定的前綴,如果沒有就默認(rèn)使用緩存的名字作為前綴 # spring.cache.redis.key-prefix=CACHE_ # 默認(rèn)就使用分區(qū)名 spring.cache.redis.use-key-prefix=true # 是否緩存空值。防止緩存穿透 spring.cache.redis.cache-null-values=true
@CacheEvict注解的使用
數(shù)據(jù)一致性中的失效模式
/**
* 使用失效模式:先刪除緩存,在訪問系統(tǒng)獲得緩存
* findCatelog1:緩存時(shí)的key名
* value = "category" 需要與緩存時(shí)的名稱相同
* 存儲(chǔ)同一個(gè)類型的數(shù)據(jù),都可以指定成同一個(gè)分區(qū)。分區(qū)名默認(rèn)就是緩存的前綴
*/
// @CacheEvict(value = "category", key = "'findCatelog1'")// 刪除具體key的緩存
@CacheEvict(value = "category", allEntries = true)// 指定刪除某個(gè)分區(qū)下的所有數(shù)據(jù)
@Transactional
@Override
public void updateCascade(CategoryEntity category) {
this.updateById(category);
categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
}數(shù)據(jù)一致性中的雙寫模式,使用@CachePut注解
@Caching注解的使用
/**
* @CacheEvict: 失效模式:先刪除緩存,在訪問系統(tǒng)獲得緩存
* 1、同時(shí)進(jìn)行多種緩存操作 @Caching
* 2、指定刪除某個(gè)分區(qū)下的所有數(shù)據(jù)
* @param category
*/
@Caching(evict = {
@CacheEvict(value = "category", key = "'findCatelog1'"),// 刪除緩存
@CacheEvict(value = "category", key = "'getCatalogJson'"),// 刪除緩存
})
@Transactional
@Override
public void updateCascade(CategoryEntity category) {
this.updateById(category);
categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
}@CachePut注解的使用
數(shù)據(jù)一致性中的雙寫模式
@CachePut // 雙寫模式時(shí)使用
Spring-Cache的不足
讀模式
- 緩存穿透:查詢一個(gè)null數(shù)據(jù)。解決:緩存空數(shù)據(jù):spring.cache.redis.cache-null-values=true
- 緩存雪崩:大量的key同時(shí)過期。解決:加隨機(jī)時(shí)間,加上過期時(shí)間。spring.cache.redis.time-to-live=3600000
- 緩存擊穿:大量并發(fā)同時(shí)查詢一個(gè)正好過期的數(shù)據(jù)。解決:加鎖。SpringCache默認(rèn)是沒有加鎖的。
@Cacheable(value = {"category"}, key = "#root.method.name", sync = true)
sync = true 相當(dāng)于是加本地鎖,可以用來解決擊穿問題
寫模式
- 讀寫加鎖
- 引入Canal,感知到MySQL的更新去更新數(shù)據(jù)庫
- 讀多寫多,直接去數(shù)據(jù)庫查詢就行
總結(jié)
常規(guī)數(shù)據(jù)(讀多寫少,即時(shí)性,一致性要求不高的數(shù)據(jù)),完全可以使用Spring-Cache。寫模式:只要緩存的數(shù)據(jù)有過期時(shí)間就足夠了
到此這篇關(guān)于SpringBoot2.X整合Spring-Cache緩存開發(fā)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Spring-Cache緩存 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用this調(diào)用構(gòu)造函數(shù)的實(shí)現(xiàn)方法示例
這篇文章主要介紹了java使用this調(diào)用構(gòu)造函數(shù)的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了java面向?qū)ο蟪绦蛟O(shè)計(jì)中函數(shù)調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
關(guān)于SpringCloud?Ribbon替換輪詢算法問題
Spring?Cloud?Ribbon是基于Netlix?Ribbon實(shí)現(xiàn)的一套客戶端負(fù)載均衡的工具。接下來通過本文給大家介紹SpringCloud?Ribbon替換輪詢算法問題,需要的朋友可以參考下2022-01-01
springboot?vue項(xiàng)目管理后端實(shí)現(xiàn)接口新增
這篇文章主要為大家介紹了springboot?vue項(xiàng)目管理后端實(shí)現(xiàn)接口新增,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Java實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解
大家都用過鬧鐘,鬧鐘可以說是一種定時(shí)任務(wù)。那么,在?Java?中,如何實(shí)現(xiàn)這樣的功能呢?即如何實(shí)現(xiàn)定時(shí)任務(wù)呢?本文就來詳細(xì)和大家聊聊2022-10-10
使用SpringBoot代碼詳細(xì)解釋<List>的用法
List是Java集合框架中的一種數(shù)據(jù)結(jié)構(gòu),用于存儲(chǔ)一組有序的元素,使用List可以方便地向其中添加、刪除或者修改元素,也可以通過下標(biāo)或者迭代器遍歷其中的元素,這篇文章主要介紹了用SpringBoot代碼詳細(xì)解釋<List>的用法,需要的朋友可以參考下2023-09-09
java 方法泛型入?yún)和String的重載關(guān)系詳解
這篇文章主要介紹了java 方法泛型入?yún)和String的重載關(guān)系詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

