本文,讲解 Spring Boot 如何集成 Guava Cache,实现缓存。

在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门」后,对 Spring Boot 集成缓存机制有一定了解后,对 Spring Boot 集成缓存机制有一定了解后,我们来了解下 Guava Cache 的使用。

Guava Cache 集成

Guava 包含了若干被 Google 的 Java 项目广泛依赖的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] 、字符串处理 [string processing] 、I/O 等等。

题外话,个人对 Guava 的 集合 [collections] 、缓存 [caching] 十分钟爱。

Spring Boot 对 Guava 的缓存机制也做了很好的支持。

在 Spring Boot 中集成 Guava Cache 非常容易,只需要在 pom.xml 中增加Guava 依赖即可。

  1. <dependency>
  2. <groupId>com.google.guava</groupId>
  3. <artifactId>guava</artifactId>
  4. <version>19.0</version>
  5. </dependency>

其实,底层根据自动配置机制实现的。具体实现原理,如果有兴趣的话,可以参考之前的文章「Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机」 和 「Spring Boot 揭秘与实战 源码分析 - 工作原理剖析」。

如果想对 Guava Cache 更深入的了解,可以参考 http://ifeve.com/google-guava-cachesexplained/。

运行起来,控制台打印的日志信息,说明已经是 GuavaCacheManager 实例,说明 GuavaCache 开启成功了。

  1. Bean 'cacheManager' of type [class org.springframework.cache.guava.GuavaCacheManager]

个性化配置

如果想自定义参数,例如存活时间,缓存最大数目等。我们可以通过 Java Config 的方式,进行配置。

下面案例,我配置存活时间为 10 秒,缓存最大数目为 1000 个。

  1. @Configuration
  2. @EnableCaching
  3. public class GuavaCacheConfig {
  4. @Bean
  5. public CacheManager cacheManager() {
  6. GuavaCacheManager cacheManager = new GuavaCacheManager();
  7. cacheManager.setCacheBuilder(
  8. CacheBuilder.newBuilder().
  9. expireAfterWrite(10, TimeUnit.SECONDS).
  10. maximumSize(1000));
  11. return cacheManager;
  12. }
  13. }

源代码

相关示例完整代码: springboot-action

(完)

如果觉得我的文章对你有帮助,请随意打赏。

Spring Boot 揭秘与实战(二) 数据缓存篇 - Guava Cache的更多相关文章

  1. Spring Boot 揭秘与实战(二) 数据缓存篇 - Redis Cache

    文章目录 1. Redis Cache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 Redis Cache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存 ...

  2. Spring Boot 揭秘与实战(二) 数据缓存篇 - EhCache

    文章目录 1. EhCache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 EhCache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门 ...

  3. Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门

    文章目录 1. 声明式缓存 2. Spring Boot默认集成CacheManager 3. 默认的 ConcurrenMapCacheManager 4. 实战演练5. 扩展阅读 4.1. Mav ...

  4. Spring Boot 揭秘与实战(二) 数据存储篇 - 声明式事务管理

    文章目录 1. 声明式事务 2. Spring Boot默认集成事务 3. 实战演练4. 源代码 3.1. 实体对象 3.2. DAO 相关 3.3. Service 相关 3.4. 测试,测试 本文 ...

  5. Spring Boot 揭秘与实战(二) 数据存储篇 - ElasticSearch

    文章目录 1. 版本须知 2. 环境依赖 3. 数据源 3.1. 方案一 使用 Spring Boot 默认配置 3.2. 方案二 手动创建 4. 业务操作5. 总结 4.1. 实体对象 4.2. D ...

  6. Spring Boot 揭秘与实战(二) 数据存储篇 - MongoDB

    文章目录 1. 环境依赖 2. 数据源 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 3. 使用mongoTemplate操作4. 总结 3.1. 实体对象 3 ...

  7. Spring Boot 揭秘与实战(二) 数据存储篇 - Redis

    文章目录 1. 环境依赖 2. 数据源 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 3. 使用 redisTemplate 操作4. 总结 3.1. 工具类 ...

  8. Spring Boot 揭秘与实战(二) 数据存储篇 - JPA整合

    文章目录 1. 环境依赖 2. 数据源 3. 脚本初始化 4. JPA 整合方案一 通过继承 JpaRepository 接口 4.1. 实体对象 4.2. DAO相关 4.3. Service相关 ...

  9. Spring Boot 揭秘与实战(二) 数据存储篇 - MyBatis整合

    文章目录 1. 环境依赖 2. 数据源3. 脚本初始化 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 4. MyBatis整合5. 总结 4.1. 方案一 通过 ...

随机推荐

  1. 牛客练习赛32 A/B/C

    https://ac.nowcoder.com/acm/contest/272/A v<=k时  答案就是k个1 否则贪心的从中间向两边添加1 #include<bits/stdc++.h ...

  2. python 绝版线程池

    2.绝版线程池设计思路:运用队列queue a.队列里面放任务 b.线程一次次去取任务,线程一空闲就去取任务 import queueimport threadingimport contextlib ...

  3. ActiveMQ 事务和XA

    1. 客户端怎样显式地使用事务? producer 开启事务(代码片段): ActiveMQSession session = (ActiveMQSession)connection.createSe ...

  4. npm 安装nodesass 或者包含nodesass的脚手架工具报错问题

    由于最近vue转angular 但是angular版本太多了,好多项目是angularv4 有的是v5 近日angular又发布了v6,依赖的东西好多不一样,结果npm install 时候,总是出现 ...

  5. DLL的Export和Import及extern "C"

    今天使用Unrar.dll,在调用RARProcessFileW时,VS总是提示“error LNK2001: 无法解析的外部符号”. Unrar.dll中是使用 extern "C&quo ...

  6. 关于Object.prototype.toString.call

    slice(8,-1)意思是从第8位开始(包含第8位)到最后一位之前(-1的意思就是最后一位,不包含最后一位): Object.prototype.toString.call(boj)这个是用来判断数 ...

  7. Standalone的更改方式

    1.更改Game视图中的Standalone分辨率 2.在unity中菜单栏中 File---->Build Settings(Ctrl+Shift+B)---->Player Setti ...

  8. WPS处理个人信息一种方法

    下面是WPS处理个人信息的方法,具体步骤如下: 第一步:任意打开一个文件: 第二步:点击左上角WPS的小三角,找到工具——选项,如图: 第三步:进入选项后,点击用户信息: 第四步:修改个人信息,用户名 ...

  9. tfs 2017 使用

    安装完成之后,创建一个项目管理. 初始化代码库 然后下载代理 (服务器)并设置.下载代理需要FQ才可以下载成功. 想要支持 netcore2.0  必须在代理服务器上安装 vs2017 跟netcor ...

  10. 2.1FTP的简单传输

    第一个简单的FTP传输实例 from ftplib import FTP nonpassive = False filename = 'new_1.py' dirname = '.' sitename ...