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

Springboot整合GuavaCache緩存過程解析

 更新時間:2020年02月19日 08:58:21   作者:泡椒炒甜瓜  
這篇文章主要介紹了springboot整合GuavaCache緩存過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了springboot整合GuavaCache緩存過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

Guava Cache是一種本地緩存機制,之所以叫本地緩存,是因為它不會把緩存數(shù)據(jù)放到外部文件或者其他服務(wù)器上,而是存放到了應(yīng)用內(nèi)存中。

Guava Cache的優(yōu)點是:簡單、強大、輕量級。

GuavaCache適用場景:

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

2.愿意使用或犧牲一些內(nèi)存空間來提升訪問或者計算速度;

3.緩存內(nèi)容或者結(jié)果值較小,不會超過內(nèi)存總?cè)萘浚?/p>

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

@Cacheable 觸發(fā)緩存邏輯

Spring 在執(zhí)行 @Cacheable 標(biāo)注的方法前先查看緩存中是否有數(shù)據(jù),如果有數(shù)據(jù),則直接返回緩存數(shù)據(jù);若沒有數(shù)據(jù),執(zhí)行該方法并將方法返回值放進(jìn)緩存。

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

@CacheEvict

觸發(fā)緩存逐出邏輯

方法執(zhí)行成功后會從緩存中移除相應(yīng)數(shù)據(jù)。

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

@CachePut

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

pom.xml配置文件:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-cache</artifactId>
  <version>1.5.9.RELEASE</version>
</dependency>

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>19.0</version>
</dependency>

GuavaCacheConfig:

/**
 * Copyright (c) 2020, All Rights Reserved.
 *
*/

package com.demo.server.config;

import java.util.concurrent.TimeUnit;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.cache.CacheBuilder;


@Configuration
@EnableCaching
public class GuavaCacheConfig {
  
  @Bean
  public CacheManager cacheManager() { 
    GuavaCacheManager cacheManager = new GuavaCacheManager();
    cacheManager.setCacheBuilder(
      CacheBuilder.newBuilder().
      expireAfterWrite(10, TimeUnit.SECONDS). //存活時間10秒
      maximumSize(1000));   //最大線程數(shù)
    return cacheManager;
  }
}

CacheController:

/**
 * Copyright (c) 2020, All Rights Reserved.
 *
*/

package com.demo.server.guava;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/guava")
public class CacheController {
  
  @Autowired
  private CacheService cacheService;

  /**
   * 查詢方法
   */
  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public String getByCache() {
    Long startTime = System.currentTimeMillis();
    long timestamp = this.cacheService.getByCache();
    Long endTime = System.currentTimeMillis();
    System.out.println("耗時: " + (endTime - startTime));
    return timestamp+"";
  }

  /**
   * 保存方法
   */
  @RequestMapping(value = "/save", method = RequestMethod.POST)
  public void save() {
    this.cacheService.save();
  }

  /**
   * 刪除方法
   */
  @RequestMapping(value = "delete", method = RequestMethod.DELETE)
  public void delete() {
    this.cacheService.delete();
  }
}

CacheService:

/**
 * Copyright (c) 2020, All Rights Reserved.
 *
*/

package com.demo.server.guava;

import java.sql.Timestamp;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


@Service
public class CacheService {

  @CachePut(value = "guavacache")
  public long save() {
    long timestamp = new Timestamp(System.currentTimeMillis()).getTime();
    System.out.println("進(jìn)行緩存:" + timestamp);
    return timestamp;
  }
 
  @CacheEvict(value = "guavacache")
  public void delete() {
    System.out.println("刪除緩存");
  }
 
  @Cacheable(value = "guavacache")
  public long getByCache() {
    try {
      Thread.sleep(3 * 1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return new Timestamp(System.currentTimeMillis()).getTime();
  }

}

Application:

package com.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
  private static final Logger LOG = LoggerFactory.getLogger(Application.class);

  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application.class);
    app.setBannerMode(Banner.Mode.OFF);
    app.setWebEnvironment(true);
    app.run(args);
    LOG.info("**************** Startup Success ****************");
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JavaFX如何獲取ListView(列表視圖)的選項

    JavaFX如何獲取ListView(列表視圖)的選項

    這篇文章主要介紹了JavaFX如何獲取ListView(列表視圖)的選項,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Spring Boot Web 靜態(tài)文件緩存處理的方法

    Spring Boot Web 靜態(tài)文件緩存處理的方法

    本篇文章主要介紹了Spring Boot Web 靜態(tài)文件緩存處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 簡單了解java中靜態(tài)初始化塊的執(zhí)行順序

    簡單了解java中靜態(tài)初始化塊的執(zhí)行順序

    這篇文章主要介紹了簡單了解java中靜態(tài)初始化塊的執(zhí)行順序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法

    Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法

    腳本文件,通常放入/bin目錄下,編寫啟動腳本需要保證能夠識別到對應(yīng)的jar文件,其次需要保證能夠識別到/config中的配置文件信息,這篇文章主要介紹了Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法,需要的朋友可以參考下
    2022-12-12
  • OPENCV+JAVA實現(xiàn)人臉識別

    OPENCV+JAVA實現(xiàn)人臉識別

    這篇文章主要為大家詳細(xì)介紹了OPENCV+JAVA實現(xiàn)人臉識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Java自定義Spring配置標(biāo)簽

    Java自定義Spring配置標(biāo)簽

    這篇文章主要介紹了Java自定義Spring配置標(biāo)簽,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-08-08
  • Spring中使用copyProperties方法進(jìn)行對象之間的屬性賦值詳解

    Spring中使用copyProperties方法進(jìn)行對象之間的屬性賦值詳解

    這篇文章主要介紹了Spring中使用copyProperties方法進(jìn)行對象之間的屬性賦值詳解,使用org.springframework.beans.BeanUtils.copyProperties方法進(jìn)行對象之間屬性的賦值,避免通過get、set方法一個一個屬性的賦值,需要的朋友可以參考下
    2023-12-12
  • 全面了解java基本類型和封裝類型的區(qū)別及應(yīng)用

    全面了解java基本類型和封裝類型的區(qū)別及應(yīng)用

    下面小編就為大家?guī)硪黄媪私鈐ava基本類型和封裝類型的區(qū)別及應(yīng)用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Springboot項目接口限流實現(xiàn)方案

    Springboot項目接口限流實現(xiàn)方案

    這篇文章主要介紹了Springboot項目接口限流實現(xiàn)方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • SpringBoot的屬性賦值@Value的用法說明

    SpringBoot的屬性賦值@Value的用法說明

    這篇文章主要介紹了SpringBoot的屬性賦值@Value的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論