Spring Boot 揭秘与实战(二) 数据缓存篇 - Guava Cache
本文,讲解 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 依赖即可。
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- <version>19.0</version>
- </dependency>
其实,底层根据自动配置机制实现的。具体实现原理,如果有兴趣的话,可以参考之前的文章「Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机」 和 「Spring Boot 揭秘与实战 源码分析 - 工作原理剖析」。
如果想对 Guava Cache 更深入的了解,可以参考 http://ifeve.com/google-guava-cachesexplained/。
运行起来,控制台打印的日志信息,说明已经是 GuavaCacheManager 实例,说明 GuavaCache 开启成功了。
- Bean 'cacheManager' of type [class org.springframework.cache.guava.GuavaCacheManager]
个性化配置
如果想自定义参数,例如存活时间,缓存最大数目等。我们可以通过 Java Config 的方式,进行配置。
下面案例,我配置存活时间为 10 秒,缓存最大数目为 1000 个。
- @Configuration
- @EnableCaching
- public class GuavaCacheConfig {
- @Bean
- public CacheManager cacheManager() {
- GuavaCacheManager cacheManager = new GuavaCacheManager();
- cacheManager.setCacheBuilder(
- CacheBuilder.newBuilder().
- expireAfterWrite(10, TimeUnit.SECONDS).
- maximumSize(1000));
- return cacheManager;
- }
- }
源代码
相关示例完整代码: springboot-action
(完)
如果觉得我的文章对你有帮助,请随意打赏。

- 版权声明:本文由 梁桂钊 发表于 梁桂钊的博客
- 转载声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证),非商业转载请注明作者及出处,商业转载请联系作者本人。
- 文章标题:Spring Boot 揭秘与实战(二) 数据缓存篇 - Guava Cache
- 文章链接:http://blog.720ui.com/2017/springboot_02_data_cache_guavacache/
Spring Boot 揭秘与实战(二) 数据缓存篇 - Guava Cache的更多相关文章
- Spring Boot 揭秘与实战(二) 数据缓存篇 - Redis Cache
文章目录 1. Redis Cache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 Redis Cache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存 ...
- Spring Boot 揭秘与实战(二) 数据缓存篇 - EhCache
文章目录 1. EhCache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 EhCache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门 ...
- Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门
文章目录 1. 声明式缓存 2. Spring Boot默认集成CacheManager 3. 默认的 ConcurrenMapCacheManager 4. 实战演练5. 扩展阅读 4.1. Mav ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - 声明式事务管理
文章目录 1. 声明式事务 2. Spring Boot默认集成事务 3. 实战演练4. 源代码 3.1. 实体对象 3.2. DAO 相关 3.3. Service 相关 3.4. 测试,测试 本文 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - ElasticSearch
文章目录 1. 版本须知 2. 环境依赖 3. 数据源 3.1. 方案一 使用 Spring Boot 默认配置 3.2. 方案二 手动创建 4. 业务操作5. 总结 4.1. 实体对象 4.2. D ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - MongoDB
文章目录 1. 环境依赖 2. 数据源 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 3. 使用mongoTemplate操作4. 总结 3.1. 实体对象 3 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - Redis
文章目录 1. 环境依赖 2. 数据源 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 3. 使用 redisTemplate 操作4. 总结 3.1. 工具类 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - JPA整合
文章目录 1. 环境依赖 2. 数据源 3. 脚本初始化 4. JPA 整合方案一 通过继承 JpaRepository 接口 4.1. 实体对象 4.2. DAO相关 4.3. Service相关 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - MyBatis整合
文章目录 1. 环境依赖 2. 数据源3. 脚本初始化 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 4. MyBatis整合5. 总结 4.1. 方案一 通过 ...
随机推荐
- AWR报告学习示例
1. 参数1 AAS AAS讲解 elapsed 为 该AWR性能报告的时间跨度 DB_TIME =所有前台session花费在database调用上的总和时间. 注意:前台进程 foregrou ...
- logstash快速入门
转自 http://blog.csdn.net/wp500/article/details/41040213 原文地址:http://logstash.net/docs/1.4.2/tutorials ...
- 类似“未能加载文件或程序集“tesseractengine3”或它的某一个依赖项”等一些问题的解决方案
有些时候我们引用了一些32位的dll,结果就会出现类似“未能加载文件或程序集“tesseractengine3”或它的某一个依赖项”这样的问题,原因是IIS的应用程序池的设置中默认是不启用32位的应用 ...
- Notes for 'Making elephants fly'
1. 技术陷阱:应是需求导向, 而不是技术导向. 2. 时机最重要:而不是创造力,团队,客户,产品,或技术. 3. 模仿:能模仿就模仿,不能模仿就创新.巧匠摹形,大师窃意. good artists ...
- 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 ...
- Mysql for Linux安装配置之—— rpm(bundle)安装
1.准备及安装1)下载rpm安装包(或rpm bundle) rpm安装包包括两个(bundle会更多),一个是client,另一个是server,例如:MySQL-client-5.5.44-1. ...
- vue-navigation 实现前进刷新,后退不刷新
vue-navigation GitHub地址 导航默认行为类似手机APP的页面导航(A.B.C为页面): A前进到B,再前进到C: C返回到B时,B会从缓存中恢复: B再次前进到C,C会重新生成,不 ...
- git上传新建项目
新建立本地项目,现在需要上传到git.对上传过程归纳如下: 一 在gitlab中新建项目:如下图所示: 二,新建后获取url地址,在本地打开gitbash,根据url把git上的项目clone到本地: ...
- 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 行动
- 字符串和数组----string
一.初始化string对象的方式 #include <iostream> #include <string> using std::cout; using std::endl; ...