午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring Cache相關(guān)知識(shí)總結(jié)

瀏覽:11日期:2023-07-13 13:52:10
簡(jiǎn)介

Spring 從 3.1 開(kāi)始定義了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口來(lái)統(tǒng)一不同的緩存技術(shù); 并支持使用 JCache ( JSR-107 )注解簡(jiǎn)化我們開(kāi)發(fā);

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

每次調(diào)用需要緩存功能的方法時(shí), Spring 會(huì)檢查檢查指定參數(shù)的指定的目標(biāo)方法是否已 經(jīng)被調(diào)用過(guò);如果有就直接從緩存中獲取方法調(diào)用后的結(jié)果,如果沒(méi)有就調(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=3600000spring.cache.redis.key-prefix=CACHE_spring.cache.redis.use-key-prefix=truespring.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@EnableCachingpublic class MyCacheConfig { // @Autowired// CacheProperties cacheProperties; /** * 配置文件中的東西沒(méi)有用上; * * 1、原來(lái)和配置文件綁定的配置類(lèi)是這樣子的 * @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; }}

第四步:

測(cè)試使用緩存 * @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.:在類(lèi)級(jí)別共享緩存的相同配置

失效模式:編輯的時(shí)候直接清空緩存。使其第一次查庫(kù)的時(shí)候存入緩存雙寫(xiě)模式:有一定的延遲,緩存期以后才可以讀到最新數(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; }

以下沒(méi)有整理。暫時(shí)記錄一下。

Spring Cache相關(guān)知識(shí)總結(jié)

Spring Cache相關(guān)知識(shí)總結(jié)

Spring Cache相關(guān)知識(shí)總結(jié)

Spring Cache相關(guān)知識(shí)總結(jié)

到此這篇關(guān)于Spring Cache相關(guān)知識(shí)總結(jié)的文章就介紹到這了,更多相關(guān)Spring Cache內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美色人阁| 久久婷婷国产麻豆91天堂 | 自拍视频一区 | 一区二区精彩视频 | 色av综合 | 中文字字幕在线中文乱码 | 欧美特大黄 | 日本一区二区三区四区五区六区 | 日韩综合一区二区三区 | 日本一区二区视频在线 | 91精品久久久久久久久久久久 | xxxxwwww国产 | 国产精品嫩草69影院 | 操欧美女人 | 日韩a在线 | 国产成人av一区二区三区在线观看 | 色综合天天综合网天天狠天天 | 91视频导航| 中国大陆高清aⅴ毛片 | 亚洲综合在| 亚洲视频免费播放 | 韩日免费av| 青青青手机视频在线观看 | 看免费黄色大片 | 国产成人一区 | 国产亚洲精品久久久久久 | 日本不卡一区二区 | 天天干夜夜 | 天天想夜夜操 | www.色亚洲| 欧美另类第一页 | 日韩网站在线观看 | 久久成人毛片 | 亚洲一区二区三区四区在线观看 | 色综合88| 亚洲欧美国产精品 | 最新中文字幕第一页 | 97精品| 撸av | 夜夜爽夜夜爽 | aaa精品 |