欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Springboot整合GuavaCache緩存過程解析

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

創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的鄂溫克網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

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

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

GuavaCache適用場景:

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

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

3.緩存內(nèi)容或者結(jié)果值較小,不會(huì)超過內(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í)行成功后會(huì)從緩存中移除相應(yīng)數(shù)據(jù)。

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

@CachePut

和 @Cacheable 類似,但會(huì)把方法的返回值放入緩存中, 主要用于數(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). //存活時(shí)間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("耗時(shí): " + (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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站標(biāo)題:Springboot整合GuavaCache緩存過程解析
轉(zhuǎn)載注明:http://aaarwkj.com/article44/ipocee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、服務(wù)器托管、云服務(wù)器動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)
国产亚洲成人精品久久| 老熟女露脸吞精一二三四区| 一本久久精品午夜福利| 久久亚洲中文字幕乱码| 国产日韩欧美老年人激情| 日韩人妻熟妇中文字幕| 欧美午夜一级特黄大片| 九九九热免费在线观看| 亚洲日本韩国福利久久| 午夜视频在线观看免费版| 日本高清久久一区二区三区| 日韩三级黄片在线观看| 美女视频黄的日本的日进去了| 亚洲精品成人综合色在线| 日本人妻中文字幕在线一区 | 国产区一区二区三在线播放| 日韩永久免费av网站| 天天操夜夜操狠狠操91| 亚洲欧美日韩在线观看a三区| 亚洲av天堂免费在线观看| 久久色综合色悠悠色综合色| 色哟哟免费在线观看视频| 国产精品福利手机在线观看| 青青草免费视频观看在线| 日本人妻风俗店中文字幕| 我的极品小姨在线观看| 国产综合一区在线观看97| 亚洲精品影视一区二区| 久久精品一区二区婷婷| 亚洲欧美综合精品久久成人| 亚洲一二三区精品与老人| 97公开视频在线观看| 国产精品一区二区夜夜夜| 欧美亚洲清纯唯美另类| 中文字幕亚洲天堂久久| 国产免费高清av在线| 亚洲精品有码中文字幕| 超碰97国产资源在线| 日韩欧美一区二区麻豆| 日韩女优在线播放一区二区| 97视频在线中文字幕|