Ehche 的使用小例子

使用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();
		
		
	}
	
	
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值