使用Ehche 首先应导入相应jar包,然后配置ehche.xml 文件 并声明一个案例
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
<!-- dict cache-->
<cache name="example"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"/>
<cache name="SimplePageFragmentCachingFilter"
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="10000"
timeToLiveSeconds="10000"
overflowToDisk="true">
</cache>
</ehcache>
package com.sun.util;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import flex.messaging.io.ArrayList;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class SunTest {
private CacheManager manager;
private Cache cache;
public SunTest(){
this.manager = CacheManager.create("src/ehcache.xml");
this.cache = manager.getCache("example");
}
@SuppressWarnings("rawtypes")
public List getData(){
List listData = new ArrayList();
Date dt = new Date();
long long1 = dt.getTime();
if( null == cache.get("list")){
DataConntionUtil datautil = new DataConntionUtil();
System.out.println("\n =======> 从JDBC取数据ing.... ");
String sql = "select system_name from wbssystem_time where rid <50000";
listData = datautil.queryListBySql(sql);
Date _dt = new Date();
long long2 = _dt.getTime();
long2 = (long2 - long1);
System.out.println("======> 运行时间:"+long2);
Element element = new Element("list", listData);
cache.put(element);
return listData;
}else{
System.out.println(" =======> 从缓存取数据ing.... ");
Element element = this.cache.get("list");
listData = (List)element.getValue();
}
Date _dt = new Date();
long long2 = _dt.getTime();
long2 = (long2 - long1);
System.out.println("======> 运行时间:"+long2);
return listData;
}
public static void main(String[] args) {
SunTest sun = new SunTest();
System.out.println("第一次查询数据:"+ sun.getData().size());
Scanner in = new Scanner(System.in);
if(in.next().equals("ok")){
System.out.println("第二次:"+ sun.getData().size());
}
Scanner _in = new Scanner(System.in);
System.out.println(_in.next());
//sun.manager.removalAll();
}
}
314

被折叠的 条评论
为什么被折叠?



