亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot集成本地緩存Caffeine的三種使用方式(小結(jié))

 更新時(shí)間:2022年06月02日 16:04:00   作者:冬風(fēng)孤立  
本文主要介紹了springboot集成本地緩存Caffeine的三種使用方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

第一種方式(只使用Caffeine)

gradle添加依賴

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
    runtimeOnly 'mysql:mysql-connector-java'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    compile group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '2.8.4'
//    compile('org.springframework.boot:spring-boot-starter-cache')
}


編寫(xiě)配置類(lèi)

package org.example.base.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author l
 * @date Created in 2020/10/27 11:05
 */
@Configuration
//@EnableCaching
public class CacheConfig {


    @Bean(value = "caffeineCache")
    public Cache<String, Object> caffeineCache() {
        return Caffeine.newBuilder()
                // 設(shè)置最后一次寫(xiě)入或訪問(wèn)后經(jīng)過(guò)固定時(shí)間過(guò)期
                .expireAfterWrite(60, TimeUnit.SECONDS)
                // 初始的緩存空間大小
                .initialCapacity(1000)
                // 緩存的最大條數(shù)
                .maximumSize(10000)
                .build();

    }

    @Bean(value = "caffeineCache2")
    public Cache<String, Object> caffeineCache2() {
        return Caffeine.newBuilder()
                // 設(shè)置最后一次寫(xiě)入或訪問(wèn)后經(jīng)過(guò)固定時(shí)間過(guò)期
                .expireAfterWrite(120, TimeUnit.SECONDS)
                // 初始的緩存空間大小
                .initialCapacity(1000)
                // 緩存的最大條數(shù)
                .maximumSize(10000)
                .build();

    }


}

測(cè)試

package org.example.base;

import com.github.benmanes.caffeine.cache.Cache;
import org.example.base.bean.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BaseApplicationTests {

	@Qualifier("caffeineCache")
	@Autowired
    Cache<String, Object> cache;


	@Qualifier("caffeineCache2")
	@Autowired
	Cache<String, Object> cache2;

    @Test
    public void test() {
        User user = new User(1, "張三", 18);
        cache.put("123", user);
        User user1 = (User) cache.getIfPresent("123");
        assert user1 != null;
        System.out.println(user1.toString());
        User user2 = (User) cache2.getIfPresent("1234");
        System.out.println(user2 == null);
    }

}

輸出

第二種方式(使用Caffeine和spring cache)

gradle添加依賴

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
    runtimeOnly 'mysql:mysql-connector-java'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    compile group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '2.8.4'
    compile('org.springframework.boot:spring-boot-starter-cache')
}

編寫(xiě)配置類(lèi)

package org.example.base.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.ArrayList;


/**
 * @author l
 * @date Created in 2020/10/27 11:05
 */
@Configuration
@EnableCaching
public class CacheConfig {


    public enum CacheEnum {
        /**
         * @date 16:34 2020/10/27
         * 第一個(gè)cache
         **/
        FIRST_CACHE(300, 20000, 300),
        /**
         * @date 16:35 2020/10/27
         * 第二個(gè)cache
         **/
        SECOND_CACHE(60, 10000, 200);

        private int second;
        private long maxSize;
        private int initSize;

        CacheEnum(int second, long maxSize, int initSize) {
            this.second = second;
            this.maxSize = maxSize;
            this.initSize = initSize;
        }

    }

    @Bean("caffeineCacheManager")
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        ArrayList<CaffeineCache> caffeineCaches = new ArrayList<>();
        for (CacheEnum cacheEnum : CacheEnum.values()) {
            caffeineCaches.add(new CaffeineCache(cacheEnum.name(),
                    Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(cacheEnum.second))
                            .initialCapacity(cacheEnum.initSize)
                            .maximumSize(cacheEnum.maxSize).build()));
        }
        cacheManager.setCaches(caffeineCaches);
        return cacheManager;
    }

//    @Bean("FIRST_CACHE")
//    public Cache firstCache(CacheManager cacheManager) {
//        return cacheManager.getCache("FIRST_CACHE");
//    }
//
//    @Bean("SECOND_CACHE")
//    public Cache secondCache(CacheManager cacheManager) {
//        return cacheManager.getCache("SECOND_CACHE");
//    }

}

編寫(xiě)service層

package org.example.base;

import org.example.base.bean.User;
import org.example.base.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BaseApplicationTests {

    @Autowired
    private UserService userService;

    @Test
    public void test() {
        User user = new User(123,"jack l",18);
        userService.setUser(user);
        System.out.println(userService.getUser("123"));
    }


}

測(cè)試

package org.example.base;

import org.example.base.bean.User;
import org.example.base.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BaseApplicationTests {

    @Autowired
    private UserService userService;

    @Test
    public void test() {
        User user = new User(123,"jack l",18);
        userService.setUser(user);
        System.out.println(userService.getUser("123"));
    }


}

輸出結(jié)果

第三種方式(使用Caffeine和spring cache)

  • gradle依賴添加同方式二
  • 配置類(lèi)添加方式同方式二
  • 編寫(xiě)service層
package org.example.base.service.impl;

import org.example.base.bean.User;
import org.example.base.service.UserService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


/**
 * @author l
 * @date Created in 2020/10/23 14:47
 */
@Service
//@CacheConfig(cacheNames = "SECOND_CACHE",cacheManager = "caffeineCacheManager")
public class UserServiceImpl implements UserService {

    /**
     * 使用@CachePut注解的方法,一定要有返回值,該注解聲明的方法緩存的是方法的返回結(jié)果。
     * it always causes the
     * method to be invoked and its result to be stored in the associated cache
     **/
    @Override
    @CachePut(key = "#user.getId()", value = "SECOND_CACHE", cacheManager = "caffeineCacheManager")
    public User setUser(User user) {
        System.out.println("已經(jīng)存儲(chǔ)進(jìn)緩存了");
        return user;
    }

    @Override
    @CacheEvict(value = "SECOND_CACHE",cacheManager = "caffeineCacheManager")
    public void deleteUser(Integer id) {
        System.out.println("緩存刪除了");
    }

    @Override
    @Cacheable(key = "#id", value = "SECOND_CACHE", cacheManager = "caffeineCacheManager")
    public User getUser(Integer id) {
        System.out.println("從數(shù)據(jù)庫(kù)取值");
        //模擬數(shù)據(jù)庫(kù)中的數(shù)據(jù)
       return null;
    }


}

測(cè)試

package org.example.base;

import org.example.base.bean.User;
import org.example.base.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BaseApplicationTests {

    @Autowired
    private UserService userService;


    @Test
    public void test4(){
        User user1 = new User(123, "jack l", 18);
        userService.setUser(user1);
        System.out.println("從緩存中獲取 "+userService.getUser(123));
        System.out.println(userService.getUser(123322222));
        userService.deleteUser(123);
        System.out.println(userService.getUser(123));

    }


}


輸出結(jié)果

到此這篇關(guān)于springboot集成本地緩存Caffeine的三種使用方式(小結(jié))的文章就介紹到這了,更多相關(guān)springboot集成本地緩存Caffeine內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot整合Lucene實(shí)現(xiàn)全文檢索的詳細(xì)步驟

    SpringBoot整合Lucene實(shí)現(xiàn)全文檢索的詳細(xì)步驟

    全文搜索(Full-Text?Search)是指對(duì)大規(guī)模存儲(chǔ)在計(jì)算機(jī)系統(tǒng)中的文本數(shù)據(jù)進(jìn)行檢索和匹配的技術(shù),它允許用戶輸入關(guān)鍵字,然后從海量的文本數(shù)據(jù)中快速找到相關(guān)的信息,本文介紹了SpringBoot整合Lucene實(shí)現(xiàn)全文檢索的詳細(xì)步驟,需要的朋友可以參考下
    2024-03-03
  • 基于@RequestBody注解只能注入對(duì)象和map的解決

    基于@RequestBody注解只能注入對(duì)象和map的解決

    這篇文章主要介紹了@RequestBody注解只能注入對(duì)象和map的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java簡(jiǎn)單實(shí)現(xiàn)數(shù)組的增刪改查方法

    java簡(jiǎn)單實(shí)現(xiàn)數(shù)組的增刪改查方法

    這篇文章主要介紹了Java數(shù)組的增刪改查的示例,幫助大家更好的利用Java處理數(shù)據(jù),感興趣的朋友可以了解下,希望能給你帶來(lái)幫助
    2021-07-07
  • 整理java讀書(shū)筆記十五之java中的內(nèi)部類(lèi)

    整理java讀書(shū)筆記十五之java中的內(nèi)部類(lèi)

    內(nèi)部類(lèi)是指在一個(gè)外部類(lèi)的內(nèi)部再定義一個(gè)類(lèi)。類(lèi)名不需要和文件夾相同。本文給大家分享java讀書(shū)筆記十五之java中的內(nèi)部類(lèi),對(duì)java讀書(shū)筆記相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • PowerJob的DatabaseMonitorAspect源碼流程

    PowerJob的DatabaseMonitorAspect源碼流程

    這篇文章主要為大家介紹了PowerJob的DatabaseMonitorAspect源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Java基礎(chǔ)字符編碼與內(nèi)存流詳細(xì)解讀

    Java基礎(chǔ)字符編碼與內(nèi)存流詳細(xì)解讀

    這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Java中String的JdbcTemplate連接SQLServer數(shù)據(jù)庫(kù)的方法

    Java中String的JdbcTemplate連接SQLServer數(shù)據(jù)庫(kù)的方法

    這篇文章主要介紹了Java中String的JdbcTemplate連接SQLServer數(shù)據(jù)庫(kù)的方法,在研發(fā)過(guò)程中我們需要與其他系統(tǒng)對(duì)接的場(chǎng)景,連接SQLServer拉取數(shù)據(jù),所以就用jdbc連接數(shù)據(jù)庫(kù)的方式連接外部數(shù)據(jù)源,需要的朋友可以參考下
    2021-10-10
  • Java中的字符流FileReader與FileWriter詳解

    Java中的字符流FileReader與FileWriter詳解

    這篇文章主要介紹了Java中的字符流FileReader與FileWriter詳解,在Java中,使用Unicode約定存儲(chǔ)字符,字符流自動(dòng)允許我們逐字符讀/寫(xiě)數(shù)據(jù),有助于執(zhí)行16位Unicode的輸入和輸出,它是以reader和writer結(jié)尾的,需要的朋友可以參考下
    2023-10-10
  • SpringBoot中的攔截器細(xì)節(jié)解析

    SpringBoot中的攔截器細(xì)節(jié)解析

    這篇文章主要介紹了SpringBoot中的攔截器細(xì)節(jié)解析,攔截器的概念、作用、實(shí)現(xiàn)方式、執(zhí)行順序、生命周期以及高級(jí)應(yīng)用,最后,我們還將探討攔截器的性能優(yōu)化策略和常見(jiàn)問(wèn)題,需要的朋友可以參考下
    2023-09-09
  • SpringBoot2學(xué)習(xí)之springboot與spring區(qū)別分析

    SpringBoot2學(xué)習(xí)之springboot與spring區(qū)別分析

    這篇文章主要為大家介紹了SpringBoot2學(xué)習(xí)之springboot與spring區(qū)別分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評(píng)論