本文,讲解 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. AWR报告学习示例

    1.  参数1  AAS AAS讲解 elapsed 为 该AWR性能报告的时间跨度 DB_TIME =所有前台session花费在database调用上的总和时间. 注意:前台进程 foregrou ...

  2. logstash快速入门

    转自 http://blog.csdn.net/wp500/article/details/41040213 原文地址:http://logstash.net/docs/1.4.2/tutorials ...

  3. 类似“未能加载文件或程序集“tesseractengine3”或它的某一个依赖项”等一些问题的解决方案

    有些时候我们引用了一些32位的dll,结果就会出现类似“未能加载文件或程序集“tesseractengine3”或它的某一个依赖项”这样的问题,原因是IIS的应用程序池的设置中默认是不启用32位的应用 ...

  4. Notes for 'Making elephants fly'

    1. 技术陷阱:应是需求导向, 而不是技术导向. 2. 时机最重要:而不是创造力,团队,客户,产品,或技术. 3. 模仿:能模仿就模仿,不能模仿就创新.巧匠摹形,大师窃意. good artists ...

  5. Notes for GGplot2: Getting started with ggplot2

    Alpha-ma 2016/10/7 1 Introduction of GGplot2 ggplot2 is an R package for producing statistical, or d ...

  6. Mysql for Linux安装配置之—— rpm(bundle)安装

    1.准备及安装1)下载rpm安装包(或rpm bundle)  rpm安装包包括两个(bundle会更多),一个是client,另一个是server,例如:MySQL-client-5.5.44-1. ...

  7. vue-navigation 实现前进刷新,后退不刷新

    vue-navigation GitHub地址 导航默认行为类似手机APP的页面导航(A.B.C为页面): A前进到B,再前进到C: C返回到B时,B会从缓存中恢复: B再次前进到C,C会重新生成,不 ...

  8. git上传新建项目

    新建立本地项目,现在需要上传到git.对上传过程归纳如下: 一 在gitlab中新建项目:如下图所示: 二,新建后获取url地址,在本地打开gitbash,根据url把git上的项目clone到本地: ...

  9. etymon word forget acid acrid acri shap acu=sour act out 1

    1● acid   2● sharp 3● acri 4● acrid acu=sour 酸的,尖酸的     1● act = to do drive   行动    

  10. 字符串和数组----string

    一.初始化string对象的方式 #include <iostream> #include <string> using std::cout; using std::endl; ...