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

}

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值