欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自己编写泛型通用 Ehcache入门

程序员文章站 2022-05-21 14:56:31
...
今天周末在家,无聊中看了下Ehcache,把我的Hello world 发表出了,呵呵:
1.下载Ehcache包
   [url]http://nchc.dl.sourceforge.net/project/ehcache/ehcache/ehcache-2.4.2/ehcache-2.4.2-distribution.tar.gz
[/url]

2.ECLIPSE 创建java工程
(见附件)

GeneralCacheLoader.java
3.主要类

package com.sm.test;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * 通用CacheLoader
 * @author Share Mu
 * @param <T>
 */
public class GeneralCacheLoader<T> {
	private static final CacheManager  cacheManager  = new CacheManager(GeneralCacheLoader.class.getResource("ehcache.xml"));
	private String cacheName=null;
	public void setCacheName(String cacheName) {
		this.cacheName = cacheName;
	}
	public void setIdp(ICacheDataProvider idp) {
		this.idp = idp;
	}

	public GeneralCacheLoader(){
	}
	private ICacheDataProvider idp;
	public GeneralCacheLoader(String cacheName ,  ICacheDataProvider idp){
		this.cacheName = cacheName;
		this.idp = idp;
	}
	public T load(String id){
		if((T)getCache().get(id)==null)
		{
			
			T t =(T)idp.get(id);
			if(t!=null)
			{
				getCache().put(new Element(id,t));
			}
			return t;
		}
		T p  = (T)getCache().get(id).getValue();
		return p;
	}
	
	public Cache getCache(){
		return cacheManager.getCache(cacheName);
	}
	
	public void clear(){
		cacheManager.getCache(cacheName).removeAll();
	}
	
	public void remove(String id){
		cacheManager.getCache(cacheName).remove(id);
	}
	
	
}

ICacheDataProvider.java
public interface ICacheDataProvider {
	public Object get(String id);
}


PersonDataProviderImpl.java
package com.sm.test;

import java.util.HashMap;
import java.util.Map;

/**
 * 为Person cache提供数据
 * @author Share Mu
 *
 */
public class PersonDataProviderImpl implements  ICacheDataProvider{
	private Map<Long,Person> source = new HashMap<Long,Person>();
	public PersonDataProviderImpl(){
		for(Long i = 0L ; i<100;i++)
		{
			Person ps = new Person();
			ps.setId(i);
			ps.setCode("code_"+i);
			ps.setName("name_"+i);
			source.put(i,ps);
		}
	}
	public Object get(String id) {
		System.out.println("aaaaaaaaaaaaaa");
		return source.get(new Long(id));
	}

}


PersonEhcacheTest.java
package com.sm.test;

/**
 * 测试类
 * @author Share Mu
 *
 */
public class PersonEhcacheTest {
	public static void main(String[] args)
	{
		PersonDataProviderImpl pdp = new PersonDataProviderImpl();
		GeneralCacheLoader<Person> pelps = new GeneralCacheLoader<Person>("mypersoncache",pdp);
		long c1 = System.currentTimeMillis();
		Person p = pelps.load("88");
		long c2 = System.currentTimeMillis();
		System.out.print("耗时"+(c2-c1));
		if(p==null)
		{
			System.out.println("没发现....");
		}else{
			System.out.println("发现了...."+p.getName()+"--"+p.getCode());
		}
		
		long c3 = System.currentTimeMillis();
		p = pelps.load("88");
		long c4 = System.currentTimeMillis();
		System.out.print("耗时"+(c4-c3));
		if(p==null)
			{
				System.out.println("没发现....");
			}else{
				System.out.println("发现了...."+p.getName()+"--"+p.getCode());
			}
	}
}