亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring Cache相關知識總結

瀏覽:3日期:2023-07-13 13:52:10
簡介

Spring 從 3.1 開始定義了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口來統一不同的緩存技術; 并支持使用 JCache ( JSR-107 )注解簡化我們開發;

Cache 接口為緩存的組件規范定義,包含緩存的各種操作集合; Cache 接 口 下 Spring 提 供 了 各 種 xxxCache 的 實 現 ; 如 RedisCache , EhCacheCache , ConcurrentMapCache 等;

每次調用需要緩存功能的方法時, Spring 會檢查檢查指定參數的指定的目標方法是否已 經被調用過;如果有就直接從緩存中獲取方法調用后的結果,如果沒有就調用方法并緩 存結果后返回給用戶。下次調用直接從緩存中獲取。

使用 Spring 緩存抽象時我們需要關注以下兩點;

1 、確定方法需要被緩存以及他們的緩存策略

2 、從緩存中讀取之前緩存存儲的數據

第一步

<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=3600000spring.cache.redis.key-prefix=CACHE_spring.cache.redis.use-key-prefix=truespring.cache.redis.cache-null-values=true

第三步:

config創建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@EnableCachingpublic 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.:觸發將數據保存到緩存的操作 * @CacheEvict: Triggers cache eviction.:觸發將數據從緩存刪除的操作 * @CachePut: Updates the cache without interfering with the method execution.:不影響方法執行更新緩存 * @Caching: Regroups multiple cache operations to be applied on a method.:組合以上多個操作 * @CacheConfig: Shares some common cache-related settings at class-level.:在類級別共享緩存的相同配置

失效模式:編輯的時候直接清空緩存。使其第一次查庫的時候存入緩存雙寫模式:有一定的延遲,緩存期以后才可以讀到最新數據

具體案例:

@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; }

以下沒有整理。暫時記錄一下。

Spring Cache相關知識總結

Spring Cache相關知識總結

Spring Cache相關知識總結

Spring Cache相關知識總結

到此這篇關于Spring Cache相關知識總結的文章就介紹到這了,更多相關Spring Cache內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 97精品国产福利一区二区三区 | 中国一级黄色毛片 | 婷婷99视频精品全部在线观看 | 久久国产主播 | 天天做夜夜操 | jizjizjiz亚洲人| 777免费视频 | 国产夜色视频 | 久久澳门 | 婷婷综合丁香 | 在线观看免费黄色片 | 欧美片免费观看网址 | 亚洲1卡2卡三卡3卡4卡网站 | 免费黄色网络 | 真实偷清晰对白在线视频 | 亚洲综合无码一区二区 | 中文字幕 视频一区 | 黄色网址大全免费 | 日本人强jizzjapanesehd | 国产精品午夜国产小视频 | 国产成人高清精品免费观看 | 一级黄毛片 | 亚洲国产日韩a在线播放 | 麻豆网站视频国产在线观看 | aaa黄色片| 欧美a级成人淫片免费看 | 国产自自拍| 麻豆精品密在线观看 | 国产精品日本不卡一区二区 | 黄色的视频免费看 | 日韩精品一区二区三区免费视频 | 可以在线看黄的网站 | 亚洲一区二区视频在线观看 | 色涩视频| 蜜桃视频一区二区在线看 | 日韩亚洲欧美综合一区二区三区 | 国产三级三级三级三级 | 老司机成人福利视频在线观看免费 | 久久99亚洲综合精品首页 | 久热精品视频在线观看99小说 | 久久国产自偷自偷免 |