Ehcache 的使用

先来说说ehcache,目前的版本为1.2,已经支持集群了。对于ehcache的使用,感觉很容易上手,基本上都是配置。以前在hibernate的时候配置过,所以也不是很陌生。API也挺简单,如下的api:
CacheManager主要的缓存管理类,一般一个应用为一个实例,如下
CacheManager.create();也可以使用new CacheManager的方式创建
默认的配置文件为ehcache.xml文件,也可以使用不同的配置

 

CacheManager manager = new CacheManager('src/config/other.xml');   

// 缓存的创建,采用自动的方式

CacheManager singletonManager = CacheManager.create();
singletonManager.addCache('testCache');
Cache test = singletonManager.getCache('testCache');   

// 或者直接创建Cache

CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache('testCache', 5000, false, false, 5, 2);
manager.addCache(memoryOnlyCache);
Cache test = singletonManager.getCache('testCache');   

// 删除cache

CacheManager singletonManager = CacheManager.create();
singletonManager.removeCache('sampleCache1');   

// 在使用ehcache后,需要关闭

CacheManager.getInstance().shutdown()   

// caches 的使用

Cache cache = manager.getCache('sampleCache1');   

// 执行crud操作

Cache cache = manager.getCache('sampleCache1');
Element element = new Element('key1', 'value1');
cache.put(element);   

// update

Cache cache = manager.getCache('sampleCache1');
cache.put(new Element('key1', 'value1');
//This updates the entry for 'key1'
cache.put(new Element('key1', 'value2');   

// get Serializable

Cache cache = manager.getCache('sampleCache1');
Element element = cache.get('key1');
Serializable value = element.getValue();   

// get non serializable

Cache cache = manager.getCache('sampleCache1');
Element element = cache.get('key1');
Object value = element.getObjectValue();   

// remove

Cache cache = manager.getCache('sampleCache1');
Element element = new Element('key1', 'value1'
cache.remove('key1');  

 

一个典型的Ecache的配置文件应该如下:

#ehcache.xml

<ehcache>

        <diskStore path="java.io.tmpdir"/>
                <defaultCache
                maxElementsInMemory="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                overflowToDisk="true"
                diskPersistent="false"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU"
         />
     
        <cache name="Test"
                maxElementsInMemory="10000"
                eternal="false"
                overflowToDisk="true"
                timeToIdleSeconds="2"
                timeToLiveSeconds="600"
                memoryStoreEvictionPolicy="LRU"
        />
                       
</ehcache>

 

 

参数说明:
name - 元素名字。
maxElementsInMemory - 设定内存中创建对象的最大值。
overflowToDisk - 设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘上。
eternal - 设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超时限制且元素永不消亡。
timeToIdleSeconds - 设置某个元素消亡前的停顿时间。也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则设置该属性也无用)。如果该值是 0 就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds - 为元素设置消亡前的生存时间。也就是一个元素从构建到消亡的最大时间间隔值。这只能在元素不是永久驻留时有效。N秒后消亡。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值