Spring Cache相關(guān)知識(shí)總結(jié)
簡介
Spring 從 3.1 開始定義了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口來統(tǒng)一不同的緩存技術(shù); 并支持使用 JCache ( JSR-107 )注解簡化我們開發(fā);
Cache 接口為緩存的組件規(guī)范定義,包含緩存的各種操作集合; Cache 接 口 下 Spring 提 供 了 各 種 xxxCache 的 實(shí) 現(xiàn) ; 如 RedisCache , EhCacheCache , ConcurrentMapCache 等;
每次調(diào)用需要緩存功能的方法時(shí), Spring 會(huì)檢查檢查指定參數(shù)的指定的目標(biāo)方法是否已 經(jīng)被調(diào)用過;如果有就直接從緩存中獲取方法調(diào)用后的結(jié)果,如果沒有就調(diào)用方法并緩 存結(jié)果后返回給用戶。下次調(diào)用直接從緩存中獲取。
使用 Spring 緩存抽象時(shí)我們需要關(guān)注以下兩點(diǎn);
1 、確定方法需要被緩存以及他們的緩存策略
2 、從緩存中讀取之前緩存存儲(chǔ)的數(shù)據(jù)
第一步
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache </artifactId> </dependency>
第二步
application.properties配置:
spring.cache.type=redis spring.cache.redis.time-to-live=3600000 spring.cache.redis.key-prefix=CACHE_ spring.cache.redis.use-key-prefix=true spring.cache.redis.cache-null-values=true
第三步:
config創(chuàng)建MyCacheConfig
package com.atguigu.gulimall.product.config; 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; @EnableConfigurationProperties(CacheProperties.class) @Configuration @EnableCaching public class MyCacheConfig { // @Autowired // CacheProperties cacheProperties; /** * 配置文件中的東西沒有用上; * * 1、原來和配置文件綁定的配置類是這樣子的 * @ConfigurationProperties(prefix = "spring.cache") * public class CacheProperties * * 2、要讓他生效 * @EnableConfigurationProperties(CacheProperties.class) * * @return */ @Bean RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); // config = config.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; } }
第四步:
測試使用緩存
* @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.:在類級(jí)別共享緩存的相同配置
失效模式:編輯的時(shí)候直接清空緩存。使其第一次查庫的時(shí)候存入緩存
雙寫模式:有一定的延遲,緩存期以后才可以讀到最新數(shù)據(jù)
具體案例:
@Cacheable(value = {"category"},key = "#root.method.name",sync = true) @Override public List<CategoryEntity> getLevel1Categorys() { System.out.println("getLevel1Categorys....."); long l = System.currentTimeMillis(); List<CategoryEntity> categoryEntities = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0)); return categoryEntities; }
以下沒有整理。暫時(shí)記錄一下。
到此這篇關(guān)于Spring Cache相關(guān)知識(shí)總結(jié)的文章就介紹到這了,更多相關(guān)Spring Cache內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于獲取JAVA路徑,包括CLASSPATH外的路徑的方法詳解
本篇文章是對(duì)獲取JAVA路徑,包括CLASSPATH外的路徑的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05idea創(chuàng)建包含多個(gè)springboot module的maven project的方法
這篇文章主要介紹了idea創(chuàng)建包含多個(gè)springboot module的maven project的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs
這篇文章主要介紹了java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs的相關(guān)資料,需要的朋友可以參考下2017-06-06SpringCloud Alibaba項(xiàng)目實(shí)戰(zhàn)之nacos-server服務(wù)搭建過程
Nacos 是阿里巴巴推出來的一個(gè)新開源項(xiàng)目,這是一個(gè)更易于構(gòu)建云原生應(yīng)用的動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺(tái)。本章節(jié)重點(diǎn)給大家介紹SpringCloud Alibaba項(xiàng)目實(shí)戰(zhàn)之nacos-server服務(wù)搭建過程,感興趣的朋友一起看看吧2021-06-066種Java創(chuàng)建對(duì)象的方式總結(jié)
在Java中,創(chuàng)建對(duì)象可以使用多種方式,本文將詳細(xì)介紹以下六種創(chuàng)建對(duì)象的方式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04