日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java+cache使用方法_java相关:springboot使用GuavaCache做简单缓存处理的方法

發布時間:2025/3/12 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java+cache使用方法_java相关:springboot使用GuavaCache做简单缓存处理的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java相關:springboot使用GuavaCache做簡單緩存處理的方法

發布于 2020-3-29|

復制鏈接

摘記: 問題背景

實際項目碰到一個上游服務商接口有10秒的查詢限制(同個賬號)。項目中有一個需求是要實時統計一些數據,一個應用下可能有多個相同的賬號。由于服務商接口的限制,當批量查詢時,可能出現同一個賬號第一次查詢有數據 ..

問題背景實際項目碰到一個上游服務商接口有10秒的查詢限制(同個賬號)。項目中有一個需求是要實時統計一些數據,一個應用下可能有多個相同的賬號。由于服務商接口的限制,當批量查詢時,可能出現同一個賬號第一次查詢有數據,但第二次查詢無數據的情況。解決方案

基于以上問題,提出用緩存的過期時間來解決。這時,可用Redis和Guava Cache來解決:當批量查詢時,同一個賬號第一次查詢有數據則緩存并設置過期時間10s, 后續查詢時直接從緩存中取,沒有再從服務商查詢。最終采用Guava Cache來解決,原因是:

應用是部署單臺的,不會有分布式的問題

Redis雖然可以實現,但會有通訊時間消耗

Guava Cache使用本地緩存,支持并發

使用GuavaCache可以快速建立緩存 1.需要在啟動類上注解@EnableCaching

2.配置CacheManager

3.控制器上注解使用@Cacheablepom.xml

```xml

org.springframework.boot

spring-boot-starter-parent

1.5.9.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework

spring-context-support

4.3.9.RELEASE

com.google.guava

guava

18.0

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

UTF-8

```

CacheConfig.java 配置類

```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 控制器測試類

```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取得參數,根據參數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啟動類

```java

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

}

}

```

總結

以上是生活随笔為你收集整理的java+cache使用方法_java相关:springboot使用GuavaCache做简单缓存处理的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。