Ehcache的使用

Ehcache是一套比较成熟的缓存解决方案,很多主流框架像Spring,Hibernate都对其有很好的支持。且 Ehcache是支持集群环境的,API也比较简单,上手比较容易。下面就介绍一下Ehcache主要功能的使用。

Ehcache默认的配置文件是ehcache.xml,内容如下:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<!-- <diskStore path="c:\\mdcp_temp" />-->
 <cacheManagerEventListenerFactory class="" properties="" />
 
 <defaultCache>
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="600"
        overflowToDisk="false"
 </defaultCache>  
 
<cache>
        name="configCache"
        maxElementsInMemory="1000"
        maxElementsOnDisk="1000" 
        eternal="true"
        timeToIdleSeconds="300" 
        timeToLiveSeconds="1000"
        overflowToDisk="false"
< /cache>

</ehcache>
这里配置了一个名为configCache的缓存实例。参数说明如下:
   name : 元素名称即缓存实例的名称。
   maxElementsInMemory : 设定内存中保存对象的最大值。
   overflowToDisk : 设置当内存中缓存到达maxElementsInMemory指定值时是否可以写到硬盘上。
   eternal : 设置内存中的对象是否为永久驻留对象。如果是则无视timeToIdleSeconds和timeToLiveSeconds两个属性。
   timeToIdleSeconds : 设置某个元素消亡前的停顿时间。指元素消亡之前,两次访问时间的最大时间间隔值。
   timeToLiveSeconds : 设置某个元素消亡前的生存时间。指元素从构建到消亡的最大时间间隔。
  注意:defaultCache不管用不用都是必须要配置的。
//初始化
    CacheManager manager = new CacheManager(“src/config/ehcache.xml”);
    //获取指定Cache对象
    Cache configCache = manager.getCache(“configCache”);
    //创建节点对象
    Element element = new Element(“key1”,”value1”);
    //保存节点到configCache
    configCache.put(element);
    //从configCache获取节点
    Element element2 = configCache.getCache(“key1”);
    Object  value = element2.getValue();
    //更新节点
    configCache.put(new Element(“key1”,”value2”));
    //删除节点
    configCache.remove(“key1”);
以上是Ehcache的基本使用,是不是很简单?

转载于:https://my.oschina.net/angel243/blog/125225

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值