国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

SpringBoot+SpringCache實(shí)現兩級緩存(Redis+Caffeine)

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:xfgg 欄目: 編程語(yǔ)言 歡迎投稿:712375056

1. 緩存、兩級緩存

1.1 內容說(shuō)明

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

使用緩存時(shí)的流程圖

1.2 Sping Cache

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

1.2.1 Cache接口

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

1.2.2 CacheManager接口

主要提供Cache實(shí)現bean的創(chuàng )建,每個(gè)應用里可以通過(guò)cacheName來(lái)對Cache進(jìn)行隔離,每個(gè)CaheName對應一個(gè)Cache實(shí)現,spring框架中默認提供的實(shí)現與Cache的實(shí)現都是成對出現的

1.2.3 常用的注解說(shuō)明

  • @Cacheable:主要應用到查詢(xún)數據的方法上
  • @CacheEvict:清除緩存,主要應用到刪除數據的方法上
  • @CachePut:放入緩存,主要用到對數據有更新的方法上
  • @Caching:用于在一個(gè)方法上配置多種注解
  • @EnableCaching:?jiǎn)⒂胹pring cache緩存,作為總的開(kāi)關(guān),在spring boot的啟動(dòng)類(lèi)或配置類(lèi)上需要加入次注解才會(huì )生效

2.實(shí)戰多級緩存的用法

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分鐘內沒(méi)有創(chuàng  )建/覆蓋時(shí),會(huì )移除該key,下次取的時(shí)候從loading中取【重點(diǎn):失效、移除Key、失效后需要獲取新值】
                .expireAfterWrite(5, TimeUnit.MINUTES)
                //初始容量
                .initialCapacity(10)
                //用來(lái)控制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<>();
        //關(guān)聯(lián)
        template.setConnectionFactory(factory);
        //設置key的序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //設置value的序列化器
        template.setValueSerializer(new StringRedisSerializer());
        return template;
    }
}

一個(gè)使用cacheable注解,一個(gè)使用redistemplate進(jìn)行緩存
因為公司項目中用到的是jedis和jediscluster所以這里只是做個(gè)了解,沒(méi)有寫(xiě)的很細

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

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

少妇裸体性生交| 中国丰满人妻VIDEOSHD| 公交车纯肉超H赵雪晴| 亚洲AV无码成H人动漫网站| 亚洲国产精品日韩专区AV| 调教済み変态JK扩张调教し|