springboot使用GuavaCache做简单缓存处理的方法
问题背景
实际项目碰到一个上游服务商接口有10秒的查询限制(同个账号)。
项目中有一个需求是要实时统计一些数据,一个应用下可能有多个相同的账号。由于服务商接口的限制,当批量查询时,可能出现同一个账号第一次查询有数据,但第二次查询无数据的情况。
解决方案
基于以上问题,提出用缓存的过期时间来解决。
这时,可用Redis和Guava Cache来解决:
当批量查询时,同一个账号第一次查询有数据则缓存并设置过期时间10s, 后续查询时直接从缓存中取,没有再从服务商查询。
最终采用Guava Cache来解决,原因是:
- 应用是部署单台的,不会有分布式的问题
- Redis虽然可以实现,但会有通讯时间消耗
- Guava Cache使用本地缓存,支持并发
使用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取得参数,根据参数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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
JSON复杂数据处理之Json树形结构数据转Java对象并存储到数据库的实现
这篇文章主要介绍了JSON复杂数据处理之Json树形结构数据转Java对象并存储到数据库的实现的相关资料,需要的朋友可以参考下2016-03-03
Mybatis Plus插入数据后获取新数据id值的踩坑记录
在某些情况下,需要在执行新增后,需要获取到新增行的id,这篇文章主要给大家介绍了关于Mybatis Plus插入数据后获取新数据id值的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2022-08-08
Spring Boot集成Spring Cloud Security进行安全增强的方法
Spring Cloud Security是Spring Security的扩展,它提供了对Spring Cloud体系中的服务认证和授权的支持,包括OAuth2、JWT等,这篇文章主要介绍了Spring Boot集成Spring Cloud Security进行安全增强,需要的朋友可以参考下2024-11-11


最新评论