您的位置:首页 > 编程语言 > Java开发

springboot之本地缓存(guava与caffeine)

2019-10-11 09:04 2176 查看

1. 场景描述

因项目要使用本地缓存,具体为啥不用redis等,就不讨论,记录下过程,希望能帮到需要的朋友。

2.解决方案

2.1 使用google的guava作为本地缓存

初步的想法是使用google的guava,因为本身项目中就有guava的denpency。

2.1.1 pom文件

需要3个dependency,如下:

<!--软件老王 1-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--软件老王 2-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!--软件老王 3-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.0-jre</version>
</dependency>

2.1.2 java类

(1)GuavaCacheManager

新建bean,返回及配置guava的GuavaCacheManager。

import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;
/**
*
* @auther: 软件老王
*/
@Configuration
@EnableCaching
public class GuavaCacheConfig {

@Bean
public CacheManager cacheManager() {
GuavaCacheManager cacheManager = new GuavaCacheManager();
cacheManager.setCacheBuilder(
CacheBuilder.newBuilder().
expireAfterWrite(6, TimeUnit.HOURS).
maximumSize(1000)); //软件老王,6小时过期,最大1000条
return cacheManager;
}
}

(2)spring注解标签使用缓存

/**
*
* @auther: 软件老王
*/
@Cacheable(value = "ruanjianlaowang")
public VO<UserParm> getUserInfo(String token) {
VO<UserParm> vo = userauthClient.verifyToken(token);
return vo;
}

guava本地缓存完成。

2.2 spring框架提供的本地缓存方案

spring中已经提供了抽象类,默认只需要配置加载jar包,配置type就可以了,但是从spring5开始,将guava的剔除掉了,替换成了caffeine,spring做了测试,作为本地缓存caffeine比guava要高出好多,我们也改成了caffeine作为本地缓存。

springboot与guava、caffeine等本地缓存,springboot1的时候还有guava,springboot2里面就把guava的缓存接口去掉了,所以前面guava本地缓存才需要定义Configuration,在这里面的话,就只需要在配置文件里面定义类型就会自动注入了。

2.3 caffeine本地缓存

2.3.1 pom文件
<!--软件老王-->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.8.0</version>
</dependency>
2.3.2 配置文件
spring:
cache:
type: caffeine
cache-names: bc.gateway.ut
caffeine:
#        spec: maximumSize=5000,expireAfterWrite=20s
spec: maximumSize=5000,expireAfterAccess=3600s

具体参数含义

/**
* @auther: 软件老王
*/
• initialCapacity=[integer]: 初始的缓存空间大小
• maximumSize=[long]: 缓存的最大条数
• maximumWeight=[long]: 缓存的最大权重
• expireAfterAccess=[duration]: 最后一次写入或访问后经过固定时间过期
• expireAfterWrite=[duration]: 最后一次写入后经过固定时间过期
• refreshAfterWrite=[duration]: 创建缓存或者最近一次更新缓存后经过固定的时间间隔,刷新缓存
2.3.3 使用方式还是spring的标签不用动:
/**
*
* @auther: 软件老王
*/
@Cacheable(value = "ruanjianlaowang")
public VO<UserParm> getUserInfo(String token) {
VO<UserParm> vo = userauthClient.verifyToken(token);
return vo;
}

2.4 关于spring缓存标签的说明

四个标签:

(1)@Cacheable,首先在缓存中查询,没有的话,进方法执行,执行完成后返回值放缓存;

(2)@CachePut,不进行查询判断,直接新增或者更新缓存内容,类似add或update;

(3)@CacheEvict,直接清除,类似delete。

(4)@Caching,1,2,3个组合

value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如: @Cacheable(value=”laowang_cache1”) @Cacheable(value={””laowang_cache1,”laowang_cache2”}
key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 @Cacheable(value=”laowang_cache1”,key=”#userName”)
condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 @Cacheable(value=”laowang_cache1”, condition=”#userName.length()>11”)

I’m 「软件老王」,如果觉得还可以的话,关注下呗,后续更新秒知!欢迎讨论区、同名公众号留言交流!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: