SpringBoot集成GuavaCache實現本地緩存「區別於redis緩存實現」


 

前言

好久沒有寫文章了,前段時間由於公司項目比較忙,因此耽擱了一些時間。本篇文章也是本頭條號轉正后發的第一篇文章,在此跟各位看官道歉,同時也感謝各位看官的不離不棄。希望各位看官可以關注本頭條號以便持續獲取最新資訊。

SpringBoot和GuavaCache

當各位看官打開這篇文章時,相信對SpringBoot的使用已經足夠了解了,我就不做過多的贅述了,這篇文章主要是講解如何在SpringBoot框架中集成GuavaCache從而實現本地緩存。相信大家都明白,在多線程高並發的情況下緩存(cache)的存在是必須的,但是需要根據不同的應用場景來使用不同的緩存策略。現階段經常使用的緩存策略有很多,也很成熟。比如分布式緩存:redis、memcached,本地緩存:ehcache、CaffeineCache以及本篇文章將要講到的GuavaCache。

GuavaCache

Guava Cache是一種本地緩存機制,之所以叫本地緩存,是因為它不會把緩存數據放到外部文件或者其他服務器上,而是存放到了應用內存中。

Guava Cache的優點是:簡單、強大、輕量級。

本篇示例還有一個優點是:可以根據不同的業務場景設置不同的緩存過期時間,詳細代碼配置可在【guava緩存配置】項中找到。

GuavaCache適用場景

1.某些接口或者鍵值會被查詢多次以上;

2.願意使用或犧牲一些內存空間來提升訪問或者計算速度;

3.緩存內容或者結果值較小,不會超過內存總容量;

GuavaCache中基於注解的聲明式緩存操作

  • @Cacheable 觸發緩存邏輯

    Spring 在執行 @Cacheable 標注的方法前先查看緩存中是否有數據,如果有數據,則直接返回緩存數據;若沒有數據,執行該方法並將方法返回值放進緩存。

    參數: value緩存名、 key緩存鍵值、 condition滿足緩存條件、unless否決緩存條件

SpringBoot集成GuavaCache實現本地緩存「區別於redis緩存實現」

@Cacheable使用示例

  • @CacheEvict

    觸發緩存逐出邏輯

方法執行成功后會從緩存中移除相應數據。

參數: value緩存名、 key緩存鍵值、 condition滿足緩存條件、 unless否決緩存條件、 allEntries是否移除所有數據 (設置為true時會移除所有緩存)

SpringBoot集成GuavaCache實現本地緩存「區別於redis緩存實現」

@CacheEvict使用示例

SpringBoot集成GuavaCache實現本地緩存「區別於redis緩存實現」

@CacheEvict使用示例

  • @CachePut

  • 不干涉方法執行地更新緩存

    和 @Cacheable 類似,但會把方法的返回值放入緩存中, 主要用於數據新增和修改方法。

SpringBoot集成GuavaCache實現本地緩存「區別於redis緩存實現」

@CachePut使用示例

  • @Caching

  • 重組一個方法上的多重緩存操作

代碼實現

一、maven-pom.xml配置文件新增

SpringBoot集成GuavaCache實現本地緩存「區別於redis緩存實現」

pom.xml新增

二、guava緩存配置

  • import java.util.ArrayList;

  • import java.util.concurrent.TimeUnit;

  • import org.springframework.cache.CacheManager;

  • import org.springframework.cache.annotation.EnableCaching;

  • import org.springframework.cache.guava.GuavaCache;

  • import org.springframework.cache.support.SimpleCacheManager;

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

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

  • import com.google.common.cache.CacheBuilder;

  • /**

  • * <p>guava緩存配置</p>

  • * @author Bruce

  • *

  • */

  • @Configuration

  • @EnableCaching

  • public class GuavaConfig {

  • private static final int DEFAULT_MAXSIZE = 1000;

  • private static final int DEFAULT_TTL = 3600;

  • /**

  • * 個性化配置緩存

  • */

  • @Bean

  • public CacheManager cacheManager() {

  • SimpleCacheManager manager = new SimpleCacheManager();

  • //把各個cache注冊到cacheManager中,GuavaCache實現了org.springframework.cache.Cache接口

  • ArrayList<GuavaCache> caches = new ArrayList<>();

  • for (Caches c : Caches.values()) {

  • caches.add(new GuavaCache(c.name(), CacheBuilder.newBuilder().recordStats().expireAfterWrite(c.getTtl(), TimeUnit.SECONDS).maximumSize(c.getMaxSize()).build()));

  • }

  • manager.setCaches(caches);

  • return manager;

  • }

  • /**

  • * 定義cache名稱、超時時長秒、最大個數

  • * 每個cache缺省3600秒過期,最大個數1000

  • */

  • public enum Caches {

  • API_PAGESUB(7200),

  • API_MATERIAL(30);

  • private int maxSize = DEFAULT_MAXSIZE; //最大數量

  • private int ttl = DEFAULT_TTL; //過期時間(秒)

  • Caches() {

  • }

  • Caches(int ttl) {

  • this.ttl = ttl;

  • }

  • Caches(int ttl, int maxSize) {

  • this.ttl = ttl;

  • this.maxSize = maxSize;

  • }

  • public int getMaxSize() {

  • return maxSize;

  • }

  • public void setMaxSize(int maxSize) {

  • this.maxSize = maxSize;

  • }

  • public int getTtl() {

  • return ttl;

  • }

  • public void setTtl(int ttl) {

  • this.ttl = ttl;

  • }

  • }

  • }

  • 三、springboot啟動器配置

  • package com.zhibo.xmt;

  • import org.springframework.boot.SpringApplication;

  • import org.springframework.boot.autoconfigure.SpringBootApplication;

  • import org.springframework.cache.annotation.EnableCaching;

  • import com.unionpay.acp.sdk.SDKConfig;

  • /**

  • * springboot啟動器

  • * @author Bruce

  • *

  • */

  • @SpringBootApplication

  • @EnableCaching

  • public class App {

  • public static void main(String[] args) {

  • System.out.println("xmt api start........");

  • SDKConfig.getConfig().loadPropertiesFromSrc();

  • SpringApplication.run(App.class, args);

  • }

  • }

四、servie層緩存添加

SpringBoot集成GuavaCache實現本地緩存「區別於redis緩存實現」

service層緩存添加-自動過期時間30秒

SpringBoot集成GuavaCache實現本地緩存「區別於redis緩存實現」

service層緩存添加-自動過期時間7200秒

 

 
 轉自:https://blog.csdn.net/yueaini10000/article/details/78918871?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM