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

Springboot整合GuavaCache緩存過程解析

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

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

Guava Cache是一種本地緩存機(jī)制,之所以叫本地緩存,是因?yàn)樗粫丫彺鏀?shù)據(jù)放到外部文件或者其他服務(wù)器上,而是存放到了應(yīng)用內(nèi)存中。

Guava Cache的優(yōu)點(diǎn)是:簡單、強(qiáng)大、輕量級。

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)文章

  • java循環(huán)結(jié)構(gòu)、數(shù)組的使用小結(jié)

    java循環(huán)結(jié)構(gòu)、數(shù)組的使用小結(jié)

    這篇文章主要介紹了java循環(huán)結(jié)構(gòu)、數(shù)組的使用小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • 基于Springboot的高校社團(tuán)管理系統(tǒng)的設(shè)計與實(shí)現(xiàn)

    基于Springboot的高校社團(tuán)管理系統(tǒng)的設(shè)計與實(shí)現(xiàn)

    本文將基于Springboot+Mybatis開發(fā)實(shí)現(xiàn)一個高校社團(tuán)管理系統(tǒng),系統(tǒng)包含三個角色:管理員、團(tuán)長、會員。文中采用的技術(shù)有Springboot、Mybatis、Jquery、AjAX、JSP等,感興趣的可以了解一下
    2022-07-07
  • IDEA 單元測試創(chuàng)建方法詳解(2020.03版本親測)

    IDEA 單元測試創(chuàng)建方法詳解(2020.03版本親測)

    這篇文章主要介紹了IDEA 單元測試創(chuàng)建方法詳解(2020.03版本親測),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Spring Boot 2.4新特性減少95%內(nèi)存占用問題

    Spring Boot 2.4新特性減少95%內(nèi)存占用問題

    這篇文章主要介紹了Spring Boot 2.4新特性減少95%內(nèi)存占用問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • 詳解Spring的autowire-candidate設(shè)計

    詳解Spring的autowire-candidate設(shè)計

    現(xiàn)在的Spring應(yīng)用通常都是基于注解開發(fā),但是對Spring感興趣的同學(xué)可以借助Spring早期基于Xml配置的各種運(yùn)用來加深對Spring框架內(nèi)部的理解和體會Spring框架的設(shè)計之妙。這篇文章我們就來談?wù)刋ml配置之default-autowire-candidates
    2021-06-06
  • java使用jdbc連接數(shù)據(jù)庫簡單實(shí)例

    java使用jdbc連接數(shù)據(jù)庫簡單實(shí)例

    這篇文章主要為大家詳細(xì)介紹了java使用jdbc連接數(shù)據(jù)庫的簡單實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • IDEA中實(shí)體類(POJO)與JSON快速互轉(zhuǎn)問題

    IDEA中實(shí)體類(POJO)與JSON快速互轉(zhuǎn)問題

    這篇文章主要介紹了IDEA中實(shí)體類(POJO)與JSON快速互轉(zhuǎn),本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Java發(fā)展史之Java由來

    Java發(fā)展史之Java由來

    本文主要給大家簡單講解了一下java的發(fā)展史,詳細(xì)說明了java的由來以及如何一步步發(fā)展起來的,想了解的小伙伴可以來參考下
    2016-10-10
  • Java獲取當(dāng)前時間年月日的方法

    Java獲取當(dāng)前時間年月日的方法

    這篇文章主要為大家詳細(xì)介紹了Java獲取當(dāng)前時間年月日的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • springboot 在idea中實(shí)現(xiàn)熱部署的方法

    springboot 在idea中實(shí)現(xiàn)熱部署的方法

    這篇文章主要介紹了springboot 在idea中實(shí)現(xiàn)熱部署的方法,實(shí)現(xiàn)了熱部署,在每一次作了修改之后,都會自動的重啟,非常節(jié)約時間,感興趣的小伙伴們可以參考一下
    2018-10-10

最新評論

东安县| 普兰县| 綦江县| 沁阳市| 四平市| 日照市| 浪卡子县| 吕梁市| 台前县| 巴南区| 丰顺县| 茂名市| 株洲县| 莱芜市| 鹿邑县| 明溪县| 南平市| 铜山县| 调兵山市| 沈阳市| 张家口市| 称多县| 资中县| 淳化县| 红河县| 海盐县| 和林格尔县| 乐至县| 沁水县| 周口市| 榆社县| 固阳县| 潢川县| 阜康市| 西丰县| 赞皇县| 张掖市| 湘阴县| 甘洛县| 拜城县| 寿光市|