Ehcache 3.x 快速使用

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
 
 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">
    
    
    <cache alias="myCache1">
        
        <key-type>java.lang.Stringkey-type>      
        <value-type>java.lang.Stringvalue-type>  
         
         <resources>
            <heap unit="entries">2000heap>       
            <offheap unit="MB">20offheap>        
         resources>
    cache>
    
    
    
    <cache-template name="myDefault">
        <key-type>java.lang.Longkey-type>
        <value-type>java.lang.Stringvalue-type>
        <heap unit="entries">200heap>
    cache-template>
    
    <cache alias="myCache2" uses-template="myDefault" />
    <cache alias="myCache3" uses-template="myDefault">
        <value-type>java.lang.Numbervalue-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 配置文件:
 
 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.Stringkey-type>      
        <value-type>java.lang.Stringvalue-type>  
        
        <expiry>
            <tti unit="minutes">2tti>   
            
        expiry>
        
        <resources>
            <heap unit="entries">2000heap>       
            <offheap unit="MB">20offheap>        
            <disk unit="MB" persistent="true">500disk>   
        resources>
    cache>
config>

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

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

储存层配置
echache 的缓存到期策略通过 cache 的 > 子节点配置,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;



你可能感兴趣的:(Java)