欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年05月27日 17:16:57   作者:柴米油鹽那點(diǎn)事兒  
今天帶大家學(xué)習(xí)Spring的相關(guān)知識(shí),文中對(duì)Spring Cache作了非常詳細(xì)的介紹,對(duì)正在學(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 的 實(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)文章

  • Netty如何設(shè)置為Https訪問

    Netty如何設(shè)置為Https訪問

    這篇文章主要介紹了Netty如何設(shè)置為Https訪問,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 基于獲取JAVA路徑,包括CLASSPATH外的路徑的方法詳解

    基于獲取JAVA路徑,包括CLASSPATH外的路徑的方法詳解

    本篇文章是對(duì)獲取JAVA路徑,包括CLASSPATH外的路徑的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 深入了解SparkSQL的運(yùn)用及方法

    深入了解SparkSQL的運(yùn)用及方法

    SparkSQL就是將SQL轉(zhuǎn)換成一個(gè)任務(wù),提交到集群上運(yùn)行,類似于Hive的執(zhí)行方式。本文給大家分享了SparkSQl的運(yùn)用及方法,感興趣的朋友跟隨小編一起看看吧
    2022-03-03
  • 如何解決@Data和@Builder的沖突問題

    如何解決@Data和@Builder的沖突問題

    在使用@Data和@Builder注解時(shí),可能會(huì)導(dǎo)致無法使用無參構(gòu)造方法創(chuàng)建實(shí)體類實(shí)例的問題,本文提出了兩種解決方法:一是手動(dòng)添加無參構(gòu)造并使用@Tolerate注解兼容;二是同時(shí)添加@AllArgsConstructor和@NoArgsConstructor注解,既添加無參構(gòu)造也添加全參構(gòu)造
    2024-10-10
  • SpringBoot集成RocketMQ的使用示例

    SpringBoot集成RocketMQ的使用示例

    RocketMQ是阿里巴巴開源的一款消息中間件,性能優(yōu)秀,功能齊全,被廣泛應(yīng)用在各種業(yè)務(wù)場景,本文就來介紹一下SpringBoot集成RocketMQ的使用示例,感興趣的可以了解一下
    2023-11-11
  • java實(shí)現(xiàn)文件上傳到服務(wù)器

    java實(shí)現(xiàn)文件上傳到服務(wù)器

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件上傳到服務(wù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • idea創(chuàng)建包含多個(gè)springboot module的maven project的方法

    idea創(chuàng)建包含多個(gè)springboot module的maven project的方法

    這篇文章主要介紹了idea創(chuàng)建包含多個(gè)springboot module的maven project的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs

    java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs

    這篇文章主要介紹了java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • SpringCloud Alibaba項(xiàng)目實(shí)戰(zhàn)之nacos-server服務(wù)搭建過程

    SpringCloud 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-06
  • 6種Java創(chuàng)建對(duì)象的方式總結(jié)

    6種Java創(chuàng)建對(duì)象的方式總結(jié)

    在Java中,創(chuàng)建對(duì)象可以使用多種方式,本文將詳細(xì)介紹以下六種創(chuàng)建對(duì)象的方式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-04-04

最新評(píng)論