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

springboot使用GuavaCache做簡單緩存處理的方法

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

問題背景

實際項目碰到一個上游服務(wù)商接口有10秒的查詢限制(同個賬號)。

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

解決方案

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

這時,可用Redis和Guava Cache來解決:

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

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

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

使用GuavaCache可以快速建立緩存

1.需要在啟動類上注解@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 控制器測試類

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中的那個,具體看配置類
  @Cacheable(value = "GuavaCacheAll",key = "#name")
  public String tt(String name){
    System.out.println("in tt");
    return "name:"+name;
  }
}

Application.java springboot啟動類

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);
  }
}

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

相關(guān)文章

最新評論

皮山县| 色达县| 沙洋县| 周口市| 乐陵市| 邛崃市| 辰溪县| 平邑县| 邵阳县| 武义县| 深州市| 宣汉县| 宣威市| 崇明县| 富川| 思茅市| 宁都县| 阿拉善左旗| 兴山县| 慈利县| 喀喇| 黄龙县| 乐清市| 清徐县| 高唐县| 九龙坡区| 保定市| 康定县| 东城区| 富民县| 屏东县| 巴东县| 阿鲁科尔沁旗| 南阳市| 伊宁市| 古交市| 曲阳县| 焉耆| 沁阳市| 宝鸡市| 新野县|