SpringBoot集成GuavaCache实现本地缓存「区别于redis缓存实现」

前言

好久没有写文章了,前段时间由于公司项目比较忙,因此耽搁了一些时间。本篇文章也是本头条号转正后发的第一篇文章,在此跟各位看官道歉,同时也感谢各位看官的不离不弃。希望各位看官可以关注本头条号以便持续获取最新资讯。

SpringBoot和GuavaCache

当各位看官打开这篇文章时,相信对SpringBoot的使用已经足够了解了,我就不做过多的赘述了,这篇文章主要是讲解如何在SpringBoot框架中集成GuavaCache从而实现本地缓存。相信大家都明白,在多线程高并发的情况下缓存(cache)的存在是必须的,但是需要根据不同的应用场景来使用不同的缓存策略。现阶段经常使用的缓存策略有很多,也很成熟。比如分布式缓存:redis、memcached,本地缓存:ehcache、CaffeineCache以及本篇文章将要讲到的GuavaCache。

GuavaCache

Guava Cache是一种本地缓存机制,之所以叫本地缓存,是因为它不会把缓存数据放到外部文件或者其他服务器上,而是存放到了应用内存中。

Guava Cache的优点是:简单、强大、轻量级。

本篇示例还有一个优点是:可以根据不同的业务场景设置不同的缓存过期时间,详细代码配置可在【guava缓存配置】项中找到。

GuavaCache适用场景

1.某些接口或者键值会被查询多次以上;

2.愿意使用或牺牲一些内存空间来提升访问或者计算速度;

3.缓存内容或者结果值较小,不会超过内存总容量;

GuavaCache中基于注解的声明式缓存操作

  • @Cacheable 触发缓存逻辑

    Spring 在执行 @Cacheable 标注的方法前先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,执行该方法并将方法返回值放进缓存。

    参数: value缓存名、 key缓存键值、 condition满足缓存条件、unless否决缓存条件

SpringBoot集成GuavaCache实现本地缓存「区别于redis缓存实现」

@Cacheable使用示例

  • @CacheEvict

    触发缓存逐出逻辑

方法执行成功后会从缓存中移除相应数据。

参数: value缓存名、 key缓存键值、 condition满足缓存条件、 unless否决缓存条件、 allEntries是否移除所有数据 (设置为true时会移除所有缓存)

SpringBoot集成GuavaCache实现本地缓存「区别于redis缓存实现」

@CacheEvict使用示例

SpringBoot集成GuavaCache实现本地缓存「区别于redis缓存实现」

@CacheEvict使用示例

  • @CachePut

  • 不干涉方法执行地更新缓存

    和 @Cacheable 类似,但会把方法的返回值放入缓存中, 主要用于数据新增和修改方法。

SpringBoot集成GuavaCache实现本地缓存「区别于redis缓存实现」

@CachePut使用示例

  • @Caching

  • 重组一个方法上的多重缓存操作

代码实现

一、maven-pom.xml配置文件新增

SpringBoot集成GuavaCache实现本地缓存「区别于redis缓存实现」

pom.xml新增

二、guava缓存配置

  • import java.util.ArrayList;

  • import java.util.concurrent.TimeUnit;

  • import org.springframework.cache.CacheManager;

  • import org.springframework.cache.annotation.EnableCaching;

  • import org.springframework.cache.guava.GuavaCache;

  • import org.springframework.cache.support.SimpleCacheManager;

  • import org.springframework.context.annotation.Bean;

  • import org.springframework.context.annotation.Configuration;

  • import com.google.common.cache.CacheBuilder;

  • /**

  • * <p>guava缓存配置</p>

  • * @author Bruce

  • *

  • */

  • @Configuration

  • @EnableCaching

  • public class GuavaConfig {

  • private static final int DEFAULT_MAXSIZE = 1000;

  • private static final int DEFAULT_TTL = 3600;

  • /**

  • * 个性化配置缓存

  • */

  • @Bean

  • public CacheManager cacheManager() {

  • SimpleCacheManager manager = new SimpleCacheManager();

  • //把各个cache注册到cacheManager中,GuavaCache实现了org.springframework.cache.Cache接口

  • ArrayList<GuavaCache> caches = new ArrayList<>();

  • for (Caches c : Caches.values()) {

  • caches.add(new GuavaCache(c.name(), CacheBuilder.newBuilder().recordStats().expireAfterWrite(c.getTtl(), TimeUnit.SECONDS).maximumSize(c.getMaxSize()).build()));

  • }

  • manager.setCaches(caches);

  • return manager;

  • }

  • /**

  • * 定义cache名称、超时时长秒、最大个数

  • * 每个cache缺省3600秒过期,最大个数1000

  • */

  • public enum Caches {

  • API_PAGESUB(7200),

  • API_MATERIAL(30);

  • private int maxSize = DEFAULT_MAXSIZE; //最大數量

  • private int ttl = DEFAULT_TTL; //过期时间(秒)

  • Caches() {

  • }

  • Caches(int ttl) {

  • this.ttl = ttl;

  • }

  • Caches(int ttl, int maxSize) {

  • this.ttl = ttl;

  • this.maxSize = maxSize;

  • }

  • public int getMaxSize() {

  • return maxSize;

  • }

  • public void setMaxSize(int maxSize) {

  • this.maxSize = maxSize;

  • }

  • public int getTtl() {

  • return ttl;

  • }

  • public void setTtl(int ttl) {

  • this.ttl = ttl;

  • }

  • }

  • }

  • 三、springboot启动器配置

  • package com.zhibo.xmt;

  • import org.springframework.boot.SpringApplication;

  • import org.springframework.boot.autoconfigure.SpringBootApplication;

  • import org.springframework.cache.annotation.EnableCaching;

  • import com.unionpay.acp.sdk.SDKConfig;

  • /**

  • * springboot启动器

  • * @author Bruce

  • *

  • */

  • @SpringBootApplication

  • @EnableCaching

  • public class App {

  • public static void main(String[] args) {

  • System.out.println("xmt api start........");

  • SDKConfig.getConfig().loadPropertiesFromSrc();

  • SpringApplication.run(App.class, args);

  • }

  • }

四、servie层缓存添加

SpringBoot集成GuavaCache实现本地缓存「区别于redis缓存实现」

service层缓存添加-自动过期时间30秒

SpringBoot集成GuavaCache实现本地缓存「区别于redis缓存实现」

service层缓存添加-自动过期时间7200秒

  • 10
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值