ehcache

ehcache就像是一个可以管理的map,可以单用。
更多的用于和hibernate集成,也就是成为hibernate的二级缓存。 需要设置和DB同步的机制,多少时间同步等。 在银行内部系统中,由于没有很多的访问量,对数据的可靠性要求高,所以使用很少。
hibernate二级缓存就是将查询过的数据放在ehcache中,下次查询时不用再访问数据库了。



<?xml version="1.0" encoding="UTF-8"?>
<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="cache1" maxElementsInMemory="10000"
eternal="false" overflowToDisk="false"
timeToIdleSeconds="60"
timeToLiveSeconds="120" memoryStoreEvictionPolicy="LFU" />
</ehcache>


package com.hp.it.innovation.collaboration.test;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class TestEhcache {
public static void main(String[] args) {
CacheManager manager = new CacheManager(System.getProperty("user.dir") + "/ehcache.xml");
Cache configCache = manager.getCache("cache1");

//String
Element e1 = new Element("key1", "value1");
configCache.put(e1);
Element r1 = configCache.get("key1");
Object value = r1.getValue();
System.out.println(value.toString());

//List
List<String> list = new ArrayList<String>();
list.add("a");list.add("b");list.add("c");list.add("d");
Element e2 = new Element("key2", list);
configCache.put(e2);
Element r2 = configCache.get("key2");
List<String> list2 = (List<String>) r2.getValue();
for(String v : list2){
System.out.println(v);
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值