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

springboot使用GuavaCache做簡(jiǎn)單緩存處理的方法

 更新時(shí)間:2019年01月08日 10:17:23   作者:qianggetaba  
這篇文章主要介紹了springboot使用GuavaCache做簡(jiǎn)單緩存處理的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

問題背景

實(shí)際項(xiàng)目碰到一個(gè)上游服務(wù)商接口有10秒的查詢限制(同個(gè)賬號(hào))。

項(xiàng)目中有一個(gè)需求是要實(shí)時(shí)統(tǒng)計(jì)一些數(shù)據(jù),一個(gè)應(yīng)用下可能有多個(gè)相同的賬號(hào)。由于服務(wù)商接口的限制,當(dāng)批量查詢時(shí),可能出現(xiàn)同一個(gè)賬號(hào)第一次查詢有數(shù)據(jù),但第二次查詢無數(shù)據(jù)的情況。

解決方案

基于以上問題,提出用緩存的過期時(shí)間來解決。

這時(shí),可用Redis和Guava Cache來解決:

當(dāng)批量查詢時(shí),同一個(gè)賬號(hào)第一次查詢有數(shù)據(jù)則緩存并設(shè)置過期時(shí)間10s, 后續(xù)查詢時(shí)直接從緩存中取,沒有再?gòu)姆?wù)商查詢。

最終采用Guava Cache來解決,原因是:

  • 應(yīng)用是部署單臺(tái)的,不會(huì)有分布式的問題
  • Redis雖然可以實(shí)現(xiàn),但會(huì)有通訊時(shí)間消耗
  • Guava Cache使用本地緩存,支持并發(fā)

使用GuavaCache可以快速建立緩存

1.需要在啟動(dòng)類上注解@EnableCaching
2.配置CacheManager
3.控制器上注解使用@Cacheable

pom.xml

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>18.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>

CacheConfig.java 配置類

package application.config;

import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
public class CacheConfig {

  public CacheManager cacheManager(){
    GuavaCache guavaCache = new GuavaCache("GuavaCacheAll", CacheBuilder.newBuilder()
    .recordStats()
    .expireAfterWrite(10000, TimeUnit.SECONDS)
    .build());

    List list = new ArrayList();
    list.add(guavaCache);
    SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
    simpleCacheManager.setCaches(list);
    return simpleCacheManager;
  }
}

TestController.java 控制器測(cè)試類

package application.controller;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

  @RequestMapping("/test")
  //key是使用spEl取得參數(shù),根據(jù)參數(shù)name作為緩存的key,value是使用的緩存list中的那個(gè),具體看配置類
  @Cacheable(value = "GuavaCacheAll",key = "#name")
  public String tt(String name){
    System.out.println("in tt");
    return "name:"+name;
  }
}

Application.java springboot啟動(dòng)類

package application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class,args);
  }
}

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

相關(guān)文章

最新評(píng)論

揭东县| 龙南县| 阜新| 永靖县| 蒙阴县| 罗定市| 秭归县| 乡宁县| 仪征市| 华安县| 衡南县| 涪陵区| 库伦旗| 博罗县| 荥阳市| 乐清市| 理塘县| 莒南县| 宣城市| 威远县| 东台市| 土默特右旗| 洮南市| 大连市| 黄大仙区| 中宁县| 澄江县| 伊吾县| 孝感市| 珲春市| 罗源县| 海口市| 榆树市| 奉新县| 晋城| 西昌市| 东光县| 萨迦县| 吕梁市| 冕宁县| 壤塘县|