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. 方案一 通过 ...
随机推荐
- fastjson SerializerFeature详解(转)
原文地址:fastjson SerializerFeature详解
- #pragma 处理警告 clang diagnostic 的使用
首先#pragma在本质上是声明,常用的功能就是注释,尤其是给Code分段注释:而且它还有另一个强大的功能是处理编译器警告,但却没有上一个功能用的那么多. clang diagnostic 是#pra ...
- 详谈Oracle12c新特点容器数据库&可插拔数据库(CDB&PDB)
一般信息 数据字典 CDB_FILE$ DBA_PDBS PDB$SEED CDB_LOCAL_ADMINAUTH$ DBA_PDB_HISTORY PDB_HISTORY$ CDB_PDB_SAVE ...
- Linux 硬件信息命令
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat /proc/cpuinfo| g ...
- php ajax bootstrap多文件上传图片预览,ajax上传文件
<form enctype="multipart/form-data" id="upForm"> <label class="btn ...
- Ubuntu Windows双系统重装windows后看不到ubuntu启动引导
1.下载并安装Easy BCD 2.点击编辑引导菜单,看到只有windows一项 3.点击“添加新条目”,添加引导菜单,选择linux/bsd ,类型选择GRUB 2,然后输入名称,选择Ubuntu所 ...
- laravel 查询指定字段的值
$this->model->where('id',$id)->value('user');
- learning ddr mode reigsters
For application flexibility, various functions, features, and modes are programmable in four Mode Re ...
- 熔断监控集群(Turbine)
Spring Cloud Turbine 上一章我们集成了Hystrix Dashboard,使用Hystrix Dashboard可以看到单个应用内的服务信息,显然这是不够的,我们还需要一个工具能让 ...
- nexus下载远程maven中央仓库的解决方案
参考:http://www.linuxidc.com/Linux/2014-03/98708.htm https://repo.maven.apache.org/maven2/.index/ 下载这两 ...