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

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

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

簡介 

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

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

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

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

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

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

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

相關文章

  • Netty如何設置為Https訪問

    Netty如何設置為Https訪問

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

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

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

    深入了解SparkSQL的運用及方法

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

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

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

    SpringBoot集成RocketMQ的使用示例

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

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

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

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

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

    java 中Spark中將對象序列化存儲到hdfs

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

    SpringCloud Alibaba項目實戰(zhàn)之nacos-server服務搭建過程

    Nacos 是阿里巴巴推出來的一個新開源項目,這是一個更易于構建云原生應用的動態(tài)服務發(fā)現(xiàn)、配置管理和服務管理平臺。本章節(jié)重點給大家介紹SpringCloud Alibaba項目實戰(zhàn)之nacos-server服務搭建過程,感興趣的朋友一起看看吧
    2021-06-06
  • 6種Java創(chuàng)建對象的方式總結(jié)

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

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

最新評論