本文总结了Ehcache 2升级到Ehcache 3的改动点。

Ehcache 2升级到Ehcache 3的改动点

1. 包名更改

Ehcache 2包名如下:

import net.sf.ehcache.Cache;

Ehcache 3包名如下:

import org.ehcache.Cache;

2. 配置文件

Ehcache 2配置文件如下:

<ehcache><diskStore path="java.io.tmpdir"/><defaultCachemaxEntriesLocalHeap="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"maxEntriesLocalDisk="10000000"diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"><persistence strategy="localTempSwap"/></defaultCache></ehcache>

Ehcache 3配置文件如下:

<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd"><cache-template name="example"><key-type>java.lang.Long</key-type><value-type>java.lang.String</value-type><heap unit="entries">200</heap></cache-template>
</config>Ehcache 2 初始化配置方式如下:```java
try (InputStream fis = this.getClass().getResourceAsStream("/config/ehcache.xml")) {manager = new CacheManager(fis);
}

Ehcache 3初始化配置方式如下:

final URL myUrl = getClass().getResource("/configs/ ehcache.xml ");XmlConfiguration xmlConfig = new XmlConfiguration(myUrl);CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);

3. CacheManager类

Ehcache 2的CacheManager有addCacheIfAbsent方法。

public Cache getCacheAddIfAbsent(String cacheName) {Cache cache = manager.getCache(cacheName);if (cache == null) {synchronized (EhcacheManager.class) {manager.addCacheIfAbsent(cacheName);cache = manager.getCache(cacheName);}}return cache;}

Ehcache 3的CacheManager没有addCacheIfAbsent方法,需自己实现。

private CacheManager manager;public Cache getCacheAddIfAbsent(String cacheName) {Cache cache = manager.getCache(cacheName, String.class, Object.class);if (cache == null) {try {cache = manager.createCache(cacheName, configurationBuilder);} catch (IllegalArgumentException e) {//cache already existsLOGGER.info("cache {} already exists", cacheName);cache = manager.getCache(cacheName, String.class, Object.class);}}return cache;}

4. 没有了Element的概念

Ehcache 3已经没有了net.sf.ehcache.Element的概念。

Ehcache 2从缓存获取值的方式如下:

Cache cache = ……;Element element = cache.get("key1");Serializable value = element.getValue();

Ehcache 3从缓存获取值的方式如下:

Cache< String, String> myCache = ……;String value = myCache.get("key1");

Ehcache 3没有了Element这个中间对象,用法上更加简洁。

参考引用

  • 原本同步至:https://waylau.com/differences-between-ehcache-2-and-ehcache-3/
  • 该版本在高并发下bug:https://github.com/ehcache/ehcache3/issues/2787

Ehcache 2升级到Ehcache 3的改动点相关推荐

  1. ehcache 程序_将Ehcache添加到Openxava应用程序

    ehcache 程序 介绍 本文介绍如何在Openxava应用程序上快速启用Ehcache,从而提高性能. 查看实体及其图形时,将加载关系. 添加第二级缓存可加快关联元素的检索速度,因为已加载的元素是 ...

  2. ehcache 冲突_解决Ehcache缓存警告问题

    警告: Creating a new instance of CacheManager using the diskStorePath "D:\Apache Tomcat 6.0.18\te ...

  3. Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式 ...

  4. Ehcache学习总结(1)--Ehcache入门介绍

    Ehcache是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从hibernate的缓存开始的.网上中文的EhCache材料以简单介绍和配置方法居多,如果你有这方面的 ...

  5. Ehcache学习总结(2)--Ehcache整合spring配置

    首先需要的maven依赖为: [html] view plain copy   <!--ehcache--> <dependency> <groupId>com.g ...

  6. ehcache 异常net.sf.ehcache.CacheException: When configured copyOnRead or copyOnWrite, a Store will onl

    之前遇到"缓存的对象变更后,自动更新到缓存中"的问题,参照http://blog.csdn.net/haiyang4988/article/details/53201618 然后就 ...

  7. log4j升级为log4j2(不需要改动代码)

    公司的项目决定升级log4j,因为log4j2有一个自动删除日志的功能,这样可以减轻运维的一些工作,而且在多线程环境下,log4j2的异步日志系统比log4j和logback提高了十倍的性能(吞吐量和 ...

  8. OpenCV 笔记(04)— OpenCV2 升级到 OpenCV3/CV4 的改动(去掉 CV_前缀、使用新的前缀替换、使用新的命名空间宏)

    1. 由于宏名称的变更照成的"未声明的标识符"系列问题 有时候,遇到此类问题加入一句 #include <cv.h> 便可以让 OpenCV3 或者 OpenCV4 也 ...

  9. ASP.NET Boilerplate v5升级到Abp vNext的改动

    AppConfigurations GetAssembly() 改为 Assembly属性访问 MongoDB的配置更改 DBMigrate和 数据库连接. 注入方式的初始化 更改了审计日志的接口, ...

最新文章

  1. 【ArcGIS遇上Python】三种利用Python批量处理地理数据的方法——以栅格数据投影转换为例
  2. 2019 ICPC Asia Yinchuan Regional(9 / 13)
  3. 苹果封装的对称加密和非对称加密API
  4. 如何才能轻松地分析日志?
  5. 51CTO-redis-集群安装以及动态扩容
  6. 8月22日见!iQOO Pro跑分曝光:近50万得分 无对手
  7. Python不使用int()函数把字符串转换为数字
  8. 软件工程 第二章 可行性研究
  9. Qt编写安防视频监控系统23-图片地图
  10. 【智能无线小车系列九】在树莓派上使用USB摄像头
  11. 分析微商分销系统的缺陷
  12. 低信噪比环境下GPS信号识别捕获技术
  13. MailConnectException: Couldn‘t connect to host
  14. python 类和对象 atm_Python实现ATM提款机系统
  15. cps1 cps2 android,CPS1和CPS2模拟器详细图文教程
  16. EMC电磁兼容3:仪器受电磁辐射干扰怎么办?
  17. 云网融合学习之-VRRP协议实现网关保护探讨
  18. 解决java中浮点数相除向上取整出错的方法
  19. XML相关案例(无广告视频)
  20. Microsemi SmartFusion系列FPGA简介

热门文章

  1. 用html写出分子分母,数学中的分数分子分母用英文拼写方法
  2. Unity 脚本控制Spine播放动画
  3. 中医行业与软件行业的职业发展比较
  4. EDIUS中的视频怎么才能添加字幕?
  5. Web 性能测试工具
  6. java实现将文件或图片压缩成zip包
  7. 【洋桃电子】STM32入门100步-03
  8. Conway’s Game of Life中看C++SSE2并行化计算
  9. 工业人工智能与机器学习_机器学习与第四次工业革命
  10. 简记H2 Database内存数据踩过的坑