Ehcache 3.x 快速使用

本文介绍了Ehcache 3.x的快速使用,包括简介和Hello World示例。讲解了直接编码配置和通过XML配置两种方式,并概述了不同的缓存策略如TTI、TTL和none。此外,还提到了heap、off-heap和disk三种存储类型的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ehcache 3.X快速使用

简介

Ehcache 是一个开源的高性能缓存,拥有很高的拓展性和伸缩性,广泛使用各种 Java 项目中(如 Hibernate 默认使用 Ehcache作为二级缓存),在目前基于 Java 的缓存方案里,几乎是性能最高的实现,目前新版本的 Ehcache 3.X 通过支持 Terracotta 改善了2.X 版本体验不佳的分布式缓存支持;

Ehcahe 3.X 和 Ehache 2.X 的 API 差异比较大,以下示例以 Ehcache 3.x 为主;

Ehcache 官网: http://www.ehcache.org
Ehcache 3.X 技术文档: http://www.ehcache.org/documentation/

使用 Ehcache 需要导入依赖:org.ehcache:ehcache
如在 Gradle 中:
 
dependencies {
    compile 'org.ehcache:ehcache:3.4.0'
}

Hello world

以下通过一个简单的示例,来演示 Ehcache 3.X 的基本使用,Ehcache支持2种配置方式:直接通过编码配置,通过XML配置;

直接编码配置

 
//构建一个缓存管理器,创建一个默认的缓存 "preConfigured"
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .withCache("preConfigured",         //缓存别名
                CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,   
                        ResourcePoolsBuilder.heap(100))         //设置缓存堆容纳元素个数
                        .build())
        .build(true);               //创建之后立即初始化
//从缓存管理器中获取预定的缓存
Cache<Long, String> preConfigured
        = cacheManager.getCache("preConfigured", Long.class, String.class);
//直接从缓存管理器创建一个新的缓存
Cache<Long, String> myCache = cacheManager.createCache("myCache",
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                ResourcePoolsBuilder.heap(100)).build());
//向缓存里添加缓存键值
myCache.put(1L, "Hello World!");
//从指定缓存里获取键值
String value = myCache.get(1L);
cacheManager.close();

通过XML配置

在项目根目录创建XML配置文件: ehcache.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!--缓存配置方式1:直接配置一个cache节点-->
    <!--完整配置一个缓存-->
    <cache alias="myCache1">
        <!--储存条目索引和储存值类型配置-->
        <key-type>java.lang.String</key-type>      <!--缓存条目索引-->
        <value-type>java.lang.String</value-type>  <!--缓存条目类型-->
         <!--储存层配置-->
         <resources>
            <heap unit="entries">2000</heap>       <!--配置堆储存-->
            <offheap unit="MB">20</offheap>        <!--配置堆外储存-->
         </resources>
    </cache>
    
    <!--缓存配置方式2:通过缓存模板配置cache节点-->
    <!--配置一个缓存模板-->
    <cache-template name="myDefault">
        <key-type>java.lang.Long</key-type>
        <value-type>java.lang.String</value-type>
        <heap unit="entries">200</heap>
    </cache-template>
    <!--使用缓存模板配置缓存-->
    <cache alias="myCache2" uses-template="myDefault" />
    <cache alias="myCache3" uses-template="myDefault">
        <value-type>java.lang.Number</value-type>
    </cache>
</config>
使用缓存代码:
 
//从配置文件创建配置对象
Configuration xmlConf = new XmlConfiguration(getClass().getResource("/ehcache.xml"));
//创建缓存管理器
CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConf);
//从缓存管理器中获取缓存
Cache<Long,String> mycache1 = cacheManager.getCache("myCache1",Long.class,String.class);
//使用缓存
mycache1.put(1L,"Hello world!");
mycache1.get(1L);
//清空缓存,关闭缓存管理器
mycache1.clear();
cacheManager.close();

详细配置

以下是一份比较详细的 ehache 配置文件:
 
<?xml version="1.0" encoding="UTF-8"?>
<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">
    
    <!--配置硬盘持久化目录-->
    <persistence directory="usr/tmp/ehcache" />
    <!--完整配置一个缓存-->
    <cache alias="myCache1">
        <!--储存条目索引和储存值类型配置-->
        <key-type>java.lang.String</key-type>      <!--缓存条目索引-->
        <value-type>java.lang.String</value-type>  <!--缓存条目类型-->
        <!--缓存到期配置-->
        <expiry>
            <tti unit="minutes">2</tti>   <!--使用 TTI(time to idle) 策略-->
            <!--<ttl unit="minutes">30</ttl>  使用 TTL(time to leave)策略 -->
        </expiry>
        <!--储存层配置-->
        <resources>
            <heap unit="entries">2000</heap>       <!--配置堆储存-->
            <offheap unit="MB">20</offheap>        <!--配置堆外储存-->
            <disk unit="MB" persistent="true">500</disk>   <!--配置磁盘持久化储存-->
        </resources>
    </cache>
</config>

<cache>节点
在配置文件中,一个cache节代表一个缓存对象,需要设置 alias 别名属性,在代码中就是通过 CacheManager 对象获取相应的别名来获取相应的缓存对象的;
缓存条目是以 key-value 键值对的形式存在,需要通过<key-type ><key-value>分别指定缓存键值对的索引和值,Ehcache 支持泛型类型的键值对类型;

缓存到期配置
echache 的缓存到期策略通过 cache 的 <expiry > 子节点配置,默认支持以下3种缓存到期策略
  • tti:TTI(time to idle)策略,即固定空闲期策略,通过该节点的 unit 指定时间单位,节点值指定时间数值;
  • ttl:TTL(time to live)策略,即固定存活期策略;
  • none:不采用任何过期策略

储存层配置
echache 的缓存到期策略通过 cache 的 <resources > 子节点配置,ehcache 的缓存储存主要分为以下3层:
  • heap:JVM byted-size 堆缓存,速度最快;
  • off-heap:JVM 对外内存,速度低于 heap,但是高于 disk;
  • disk:磁盘储存,速度最低,相对于 heap ,off-heap可以分配大量资源空间;
这3层储存都可以相应的子节点进行配置,unit 指定数值单位,默认情况下为 entries ,即缓存数量;
如果需要配置磁盘持久化缓存,disk 节点需要设置 persistent 属性为 true,如果需要自定义磁盘持久化位置,可以在 config 节点下配置一个 persistence 节点;

线程池配置
在使用 ehcache 集群,常常会涉及到大量异步服务,需要配置缓存的线程池行为,详见  http://www.ehcache.org/documentation/3.4/thread-pools.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值