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

SpringBoot+SpringCache實現(xiàn)兩級緩存(Redis+Caffeine)

 更新時間:2021年04月29日 16:53:48   作者:xfgg  
這篇文章主要介紹了SpringBoot+SpringCache實現(xiàn)兩級緩存(Redis+Caffeine),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1. 緩存、兩級緩存

1.1 內容說明

Spring cache:主要包含spring cache定義的接口方法說明和注解中的屬性說明
springboot+spring cache:rediscache實現(xiàn)中的缺陷
caffeine簡介
spring boot+spring cache實現(xiàn)兩級緩存

使用緩存時的流程圖

在這里插入圖片描述

1.2 Sping Cache

spring cache是spring-context包中提供的基于注解方式使用的緩存組件,定義了一些標準接口,通過實現(xiàn)這些接口,就可以通過在方法上增加注解來實現(xiàn)緩存。這樣就能夠避免緩存代碼與業(yè)務處理耦合在一起的問題。spring cache的實現(xiàn)是使用spring aop中對方法切面(MethodInterceptor)封裝的擴展,當然spring aop也是基于Aspect來實現(xiàn)的。
spring cache核心的接口就兩個:Cache和CacheManager

1.2.1 Cache接口

提供緩存的具體操作,比如緩存的放入,讀取,清理,spring框架中默認提供的實現(xiàn)有

在這里插入圖片描述

1.2.2 CacheManager接口

主要提供Cache實現(xiàn)bean的創(chuàng)建,每個應用里可以通過cacheName來對Cache進行隔離,每個CaheName對應一個Cache實現(xiàn),spring框架中默認提供的實現(xiàn)與Cache的實現(xiàn)都是成對出現(xiàn)的

1.2.3 常用的注解說明

  • @Cacheable:主要應用到查詢數據的方法上
  • @CacheEvict:清除緩存,主要應用到刪除數據的方法上
  • @CachePut:放入緩存,主要用到對數據有更新的方法上
  • @Caching:用于在一個方法上配置多種注解
  • @EnableCaching:啟用spring cache緩存,作為總的開關,在spring boot的啟動類或配置類上需要加入次注解才會生效

2.實戰(zhàn)多級緩存的用法

package com.xfgg.demo.config;

import lombok.AllArgsConstructor;
import com.github.benmanes.caffeine.cache.Caffeine;

import org.springframework.cache.CacheManager;

import org.springframework.cache.caffeine.CaffeineCacheManager;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@AllArgsConstructor
//把定義的緩存加入到Caffeine中
public class CacheConfig {
    @Bean
    public CacheManager cacheManager(){
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                //使用refreshAfterWrite必須要設置cacheLoader
                //在5分鐘內沒有創(chuàng)建/覆蓋時,會移除該key,下次取的時候從loading中取【重點:失效、移除Key、失效后需要獲取新值】
                .expireAfterWrite(5, TimeUnit.MINUTES)
                //初始容量
                .initialCapacity(10)
                //用來控制cache的最大緩存數量
                .maximumSize(150)
        );
        return cacheManager;
    }
}

package com.xfgg.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

//生成的redis連接
public class RedisConfig<GenericObjectPoolConfig> {
    @Value("${spring.redis1.host}")
    private String host;
    @Value("${spring.redis1.port}")
    private Integer port;
    @Value("${spring.redis1.password}")
    private String password;
    @Value("${spring.redis1.database}")
    private Integer database;

    @Value("${spring.redis1.lettuce.pool.max-active}")
    private Integer maxActive;
    @Value("${spring.redis1.lettuce.pool.max-idle}")
    private Integer maxIdle;
    @Value("${spring.redis1.lettuce.pool.max-wait}")
    private Long maxWait;
    @Value("${spring.redis1.lettuce.pool.min-idle}")
    private Integer minIdle;


    @Bean
    public RedisStandaloneConfiguration redis1RedisConfig() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPassword(RedisPassword.of(password));
        config.setPort(port);
        config.setDatabase(database);
        return config;
    }
    //配置序列化器
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object>template=new RedisTemplate<>();
        //關聯(lián)
        template.setConnectionFactory(factory);
        //設置key的序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //設置value的序列化器
        template.setValueSerializer(new StringRedisSerializer());
        return template;
    }
}

一個使用cacheable注解,一個使用redistemplate進行緩存
因為公司項目中用到的是jedis和jediscluster所以這里只是做個了解,沒有寫的很細

到此這篇關于SpringBoot+SpringCache實現(xiàn)兩級緩存(Redis+Caffeine)的文章就介紹到這了,更多相關SpringBoot SpringCache兩級緩存內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java中成員變量與局部變量區(qū)別分析

    java中成員變量與局部變量區(qū)別分析

    這篇文章主要介紹了java中成員變量與局部變量區(qū)別,較為詳細的分析了java中成員變量與局部變量的功能、用法與區(qū)別,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • springBoot?啟動指定配置文件環(huán)境多種方案(最新推薦)

    springBoot?啟動指定配置文件環(huán)境多種方案(最新推薦)

    springBoot?啟動指定配置文件環(huán)境理論上是有多種方案的,一般都是結合我們的實際業(yè)務選擇不同的方案,比如,有pom.xml文件指定、maven命令行指定、配置文件指定、啟動jar包時指定等方案,今天我們一一分享一下,需要的朋友可以參考下
    2023-09-09
  • Spring Cloud Config實現(xiàn)分布式配置中心

    Spring Cloud Config實現(xiàn)分布式配置中心

    這篇文章主要介紹了Spring Cloud Config實現(xiàn)分布式配置中心,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • JAVA WEB中Servlet和Servlet容器的區(qū)別

    JAVA WEB中Servlet和Servlet容器的區(qū)別

    這篇文章主要介紹了JAVA WEB中Servlet和Servlet容器的區(qū)別,文中示例代碼非常詳細,供大家參考和學習,感興趣的朋友可以了解下
    2020-06-06
  • JavaWeb三大組件之監(jiān)聽器Listener詳解

    JavaWeb三大組件之監(jiān)聽器Listener詳解

    這篇文章主要介紹了JavaWeb三大組件之監(jiān)聽器Listener詳解,在JavaWeb應用程序中,Listener監(jiān)聽器是一種機制,用于監(jiān)聽和響應特定的事件,它可以感知并響應與應用程序相關的事件,從而執(zhí)行相應的邏輯處理,需要的朋友可以參考下
    2023-10-10
  • 詳解spring security filter的工作原理

    詳解spring security filter的工作原理

    這篇文章主要介紹了詳解spring security filter的工作原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • Java基礎強化訓練輸入錯誤即結束進程

    Java基礎強化訓練輸入錯誤即結束進程

    本文主要介紹了Java編程的基礎知識強化應用,文中實例涉及到了許多基礎知識,new對象,控制臺輸入,if語句等。很實用,需要的朋友可以參考下
    2017-09-09
  • 基于java HashMap插入重復Key值問題

    基于java HashMap插入重復Key值問題

    這篇文章主要介紹了基于java HashMap插入重復Key值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java的super關鍵字與instanceof運算符使用方法

    Java的super關鍵字與instanceof運算符使用方法

    這篇文章主要介紹了Java的super關鍵字與instanceof運算符使用方法,是Java入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • Java運算符的知識點與代碼匯總

    Java運算符的知識點與代碼匯總

    這篇文章主要給大家總結介紹了關于Java運算符知識點與代碼的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04

最新評論