最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年05月27日 17:16:57   作者:柴米油鹽那點事兒  
今天帶大家學(xué)習(xí)Spring的相關(guān)知識,文中對Spring Cache作了非常詳細(xì)的介紹,對正在學(xué)習(xí)Java Spring的小伙伴們很有幫助,需要的朋友可以參考下

簡介 

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

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

每次調(diào)用需要緩存功能的方法時, Spring 會檢查檢查指定參數(shù)的指定的目標(biāo)方法是否已 經(jīng)被調(diào)用過;如果有就直接從緩存中獲取方法調(diào)用后的結(jié)果,如果沒有就調(diào)用方法并緩 存結(jié)果后返回給用戶。下次調(diào)用直接從緩存中獲取。 

使用 Spring 緩存抽象時我們需要關(guān)注以下兩點;  

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

2 、從緩存中讀取之前緩存存儲的數(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.:組合以上多個操作
 *          @CacheConfig: Shares some common cache-related settings at class-level.:在類級別共享緩存的相同配置


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

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

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

相關(guān)文章

  • JAVA生成pdf文件的實操教程

    JAVA生成pdf文件的實操教程

    PDF是可移植文檔格式,是一種電子文件格式,具有許多其他電子文檔格式無法相比的優(yōu)點,下面這篇文章主要給大家介紹了關(guān)于JAVA生成pdf文件的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • javax.management.InvalidApplicationException的問題解決

    javax.management.InvalidApplicationException的問題解決

    javax.management.InvalidApplicationException是與Java Management Extensions (JMX) API相關(guān)的一個常見異常,本文主要介紹了javax.management.InvalidApplicationException的問題解決,感興趣的可以了解一下
    2024-08-08
  • springboot使用@Validated或@Valid注解校驗參數(shù)方式

    springboot使用@Validated或@Valid注解校驗參數(shù)方式

    這篇文章主要介紹了springboot使用@Validated或@Valid注解校驗參數(shù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Spring?Boot?集成Elasticsearch模塊實現(xiàn)簡單查詢功能

    Spring?Boot?集成Elasticsearch模塊實現(xiàn)簡單查詢功能

    本文講解了Spring?Boot集成Elasticsearch采用的是ES模板的方式實現(xiàn)基礎(chǔ)查詢,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2022-06-06
  • IDEA中Spring Initializr沒有Java8選項的解決辦法

    IDEA中Spring Initializr沒有Java8選項的解決辦法

    在使用IDEA中的Spring Initializr創(chuàng)建新項目時,Java 版本近可選擇Java17,21 ,不能選擇Java8;SpringBoot 版本也只有 3.x,所以本文給大家介紹了IDEA中Spring Initializr沒有Java8選項的解決辦法,需要的朋友可以參考下
    2024-06-06
  • SpringRunner和SpringJUnit4ClassRunner的區(qū)別及說明

    SpringRunner和SpringJUnit4ClassRunner的區(qū)別及說明

    這篇文章主要介紹了SpringRunner和SpringJUnit4ClassRunner的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • SpringCloud?Gateway實現(xiàn)API接口加解密

    SpringCloud?Gateway實現(xiàn)API接口加解密

    這篇文章主要為大家介紹了SpringCloud?Gateway如何實現(xiàn)API接口加解密的,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)有一定的幫助,需要的可以參考一下
    2022-06-06
  • Java利用docx4j+Freemarker生成word文檔

    Java利用docx4j+Freemarker生成word文檔

    這篇文章主要為大家詳細(xì)介紹了Java如何利用docx4j+Freemarker生成word文檔,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-04-04
  • SpringBoot調(diào)整ApplicationContextAware如何實現(xiàn)類加載順序

    SpringBoot調(diào)整ApplicationContextAware如何實現(xiàn)類加載順序

    SpringBoot調(diào)整ApplicationContextAware實現(xiàn)類加載順序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • spring boot 全局異常處理方法匯總

    spring boot 全局異常處理方法匯總

    這篇文章主要介紹了spring boot 全局異常處理方法匯總,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10

最新評論

SHOW| 依兰县| 洪洞县| 宁晋县| 西乌珠穆沁旗| 阳原县| 红原县| 香河县| 通州市| 仁化县| 德钦县| 松桃| 会宁县| 宁城县| 彰武县| 新干县| 临湘市| 长宁县| 蓬安县| 神木县| 普定县| 富平县| 乡城县| 剑河县| 荔浦县| 南澳县| 满洲里市| 黎川县| 朝阳市| 澄城县| 南昌市| 乡宁县| 龙门县| 东平县| 五家渠市| 安顺市| 黄骅市| 咸宁市| 德惠市| 德阳市| 四平市|