首页 > 其他分享 >Ehcache 2升级到Ehcache 3的改动点

Ehcache 2升级到Ehcache 3的改动点

时间:2023-02-06 13:02:29浏览次数:57  
标签:Ehcache 改动 String cache manager 升级 cacheName ehcache


本文总结了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"/>

<defaultCache
maxEntriesLocalHeap="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 exists
LOGGER.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这个中间对象,用法上更加简洁。

参考引用


标签:Ehcache,改动,String,cache,manager,升级,cacheName,ehcache
From: https://blog.51cto.com/u_9427273/6038913

相关文章

  • mysql从5.7升级到8.0的注意事项
    ####################################(1)sql_mode全局变量:在升级MySQL版本到8.0的过程中,需要关注sql_mode参数默认值的变化,8.0版本sql_mode不支持NO_AUTO_CREATE_USER,要避......
  • [RISC-V] StarFive VisionFive 2 升级固件,安装 Debian Image 69 工程版
    硬件昉·星光2开发板 准备步骤1.要安装 DebianImage69工程版,需要确认 VisionFive2板载QSPIflash更新至最新的uboot和spl。https://forum.rvspace.org......
  • Java基础班升级考试文档1
    一、笔试题部分一、Mysql笔试题列名数据类型长度是否为空备注idint4否ID,主键namevarchar12是姓名ageint3是年龄birdatetime是生日......
  • Java基础班升级考试文档2
    一、笔试题部分一、Mysql笔试题列名数据类型长度是否为空备注emp_noint11否ID,主键,自增birth_datedatetime是生日first_namevarchar14是姓......
  • Java基础阶段升级考试3
    一、笔试题部分一、mysql部分1.创建学生表studentcreatetablestudent(studentNovarchar(10)notnullprimarykeycomment'学生学号',studentNamevarchar(20)no......
  • vue版本2.9.6升级到高版本
    由于npminstall-gvue-cli安装的是老版本,如果想要安装新版本,可以先看一下自己是否已经安装了新版本vuecreateproj:可以看到我这边有两个版本了,如果想要安装新版本需要......
  • spring boot + spring cache 实现两级缓存(redis + ehcache)
    前言本文参考了​​springboot+springcache实现两级缓存(redis+caffeine)​​。处理流程与​​springboot+springcache实现两级缓存(redis+caffeine)​​一致:事......
  • 如何注册 ChatGPT Plus:稳定不掉线升级版 ChatGPT
    ChatGPT自从2022年11月30号发布以来已经形成了AI热浪席卷全球。许多人已经离不开ChatGPT。然而免费版本的ChatGPT却常常因为过高的负载导致无法正常访问。好消息是,......
  • 芯和半导体在DesignCon2023大会上发布新品Notus,并升级高速数字解决方案
    芯和半导体在近日举行的DesignCon2023大会上正式发布了针对封装及板级的信号完整性、电源完整分析和热分析的全新EDA平台Notus。本届DesignCon大会在美国加州的圣克拉拉会......
  • Linux下Python2.x升级Python3.7
    一、查看当前Python版本[root@localhost~]#python-VPython2.7.5二、下载新的python包并安装进入Python官网(https://www.python.org),选择需要的版本。此处我选择Py......