最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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添加依賴(lài)

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ě)入或訪(fǎng)問(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ě)入或訪(fǎng)問(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添加依賴(lài)

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)添加同方式二
  • 配置類(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)文章

  • Servlet連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶(hù)登錄的實(shí)現(xiàn)示例

    Servlet連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶(hù)登錄的實(shí)現(xiàn)示例

    本文主要介紹了Servlet連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶(hù)登錄的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • SpringBoot中的Logging詳解

    SpringBoot中的Logging詳解

    這篇文章主要介紹了SpringBoot中的Logging詳解,log配置可能是被忽視的一個(gè)環(huán)節(jié),一般的項(xiàng)目中日志配置好了基本上很少去改動(dòng),我們常規(guī)操作是log.info來(lái)記錄日志內(nèi)容,很少會(huì)有人注意到springBoot中日志的配置,需要的朋友可以參考下
    2023-09-09
  • spring boot國(guó)際化之MessageSource的使用方法

    spring boot國(guó)際化之MessageSource的使用方法

    這篇文章主要給大家介紹了spring boot國(guó)際化之MessageSource使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • java 線(xiàn)程池的實(shí)現(xiàn)方法

    java 線(xiàn)程池的實(shí)現(xiàn)方法

    在本篇文章里小編給大家整理了關(guān)于java 線(xiàn)程池的實(shí)現(xiàn)方法,有興趣的朋友們可以學(xué)習(xí)參考下。
    2020-02-02
  • 淺談Java中方法參數(shù)傳遞的問(wèn)題

    淺談Java中方法參數(shù)傳遞的問(wèn)題

    下面小編就為大家?guī)?lái)一篇淺談Java中方法參數(shù)傳遞的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Java中圖像銳化操作的方法詳解

    Java中圖像銳化操作的方法詳解

    這篇文章主要給大家介紹了關(guān)于Java中圖像銳化操作的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java和C#下的參數(shù)驗(yàn)證方法

    Java和C#下的參數(shù)驗(yàn)證方法

    下面小編就為大家?guī)?lái)一篇Java和C#下的參數(shù)驗(yàn)證實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • Java實(shí)現(xiàn)登錄與注冊(cè)頁(yè)面

    Java實(shí)現(xiàn)登錄與注冊(cè)頁(yè)面

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)登錄與注冊(cè)頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • SpringMVC4.3?HandlerExceptionResolver異常處理源碼解析

    SpringMVC4.3?HandlerExceptionResolver異常處理源碼解析

    這篇文章主要為大家介紹了SpringMVC4.3?HandlerExceptionResolver異常處理源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>
    2023-09-09
  • JAVA面試題 從源碼角度分析StringBuffer和StringBuilder的區(qū)別

    JAVA面試題 從源碼角度分析StringBuffer和StringBuilder的區(qū)別

    這篇文章主要介紹了JAVA面試題 從源碼角度分析StringBuffer和StringBuilder的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面我們來(lái)一起學(xué)習(xí)下吧
    2019-07-07

最新評(píng)論

五家渠市| 旌德县| 吉隆县| 图木舒克市| 南丰县| 梨树县| 阳山县| 南华县| 凤山县| 宜城市| 宁陵县| 景德镇市| 十堰市| 仪陇县| 灌阳县| 云浮市| 西贡区| 大石桥市| 沁源县| 喜德县| 磴口县| 咸宁市| 娄底市| 伊吾县| 鹰潭市| 嘉义县| 晋中市| 榆社县| 临漳县| 胶州市| 原阳县| 汕头市| 铁岭市| 沂南县| 邵阳市| 凌源市| 红河县| 丰顺县| 岳西县| 海安县| 红原县|