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

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

EhCache 集成

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。

在 Spring Boot 中集成 EhCache 非常容易,只需要两个步骤。

首先,在 pom.xml 中增加 EhCache 依赖。

  1. <dependency>
  2. <groupId>net.sf.ehcache</groupId>
  3. <artifactId>ehcache</artifactId>
  4. </dependency>

第二步,在 src/main/resources 目录下创建 ehcache.xml。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="ehcache.xsd">
  4. <cache name="ehcache"
  5. maxElementsInMemory="1000"
  6. timeToLiveSeconds="300">
  7. </cache>
  8. </ehcache>

其中,maxElementsInMemory,表示缓存最大数目。 timeToLiveSeconds: ,表示设置对象在失效前允许存活时间(单位:秒)。

如果想对 ehcache.xml 更深入的了解,可以参考 http://www.ehcache.org/ehcache.xml。

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

  1. Bean 'cacheManager' of type [class org.springframework.cache.ehcache.EhCacheCacheManager]

源代码

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

(完)

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

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

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

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

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

    文章目录 1. Guava Cache 集成 2. 个性化配置 3. 源代码 本文,讲解 Spring Boot 如何集成 Guava Cache,实现缓存. 在阅读「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. Leetcode 1029. 可被 5 整除的二进制前缀

    1029. 可被 5 整除的二进制前缀  显示英文描述 我的提交返回竞赛   用户通过次数467 用户尝试次数662 通过次数477 提交次数1964 题目难度Easy 给定由若干 0 和 1 组成的 ...

  2. Windows下Python3.6安装第三方模块

    一 安装pip 一般需要用pip进行安装,不过我安装p3.6的时候pip已经有了. 如果没有的话,可以用在以下http://www.lfd.uci.edu/~gohlke/pythonlibs/#pi ...

  3. IDEA 自动生成 serialVersionUID 的设置

    打开File菜单,选择Settings选项,打开Settings设置对话框:左边树形目录,打开Editor>Inspections

  4. JavaScript中 null 的 typeof是object

    JavaScript中  null 的 typeof是object

  5. OpenSSH隐藏版本号教程

    1.查看当前OpenSSH版本号 sshd -v #sshd并没有-v选项,这并不是重点重点是这样能曝出版本号 2.查看sshd位置 which sshd 3.备份sshd cp /usr/sbin/ ...

  6. auxre7使用安装

    auxre7安装     1● auxre7下载 2● 安装 D:\soft   axureuser 8wFfIX7a8hHq6yAy6T8zCz5R0NBKeVxo9IKu+kgKh79FL6IyP ...

  7. Win10系列:VC++绘制几何图形3

    在绘制三角形之前,首先需要创建一个三角形,打开D2DBasicAnimation.h头文件,在D2DBasicAnimation类中添加如下的代码: private:     //声明成员变量obje ...

  8. Android 音视频深入 三 MP4解码播放视频 (附源码下载)

    本篇项目地址,名字是媒体解码MediaCodec,MediaExtractor,求starhttps://github.com/979451341/Audio-and-video-learning-m ...

  9. 【原创】<Debug> QString

    [问题1] 'class QString' has no member named 'toAscii' [解答] 把toAscii().data()改成toLatin1().data() 如果QStr ...

  10. 关于iOS设备的那些事

    首先推荐一个在用的库XYQuick 地址:https://github.com/uxyheaven/XYQuick idfa: 获取方式 [ASIdentifierManager sharedMana ...