간단 한 캐 시 관리

4079 단어 자바
package cn.cche.cache;

import java.io.Serializable;

import cn.cche.util.Utils;
import cn.cche.util.Const;

/**
 * @author chexingyou
 * @date   2013-5-23
 */
public class CacheValue implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Object obj;
	private long time;
	private long priod;
	
	public CacheValue(){
		
	}
	
	public CacheValue(Object obj, long time) {

		this.obj = obj;
		this.time = time;
		this.priod = Const.CachePriod.normal;
	}
	
	public CacheValue(Object obj, long time,long priod) {

		this.obj = obj;
		this.time = time;
		this.priod = priod;
	}
	
	public Object getObj() {
	
		return obj;
	}
	public void setObj(Object obj) {
	
		this.obj = obj;
	}
	public long getTime() {
	
		return time;
	}
	public void setTime(long time) {
	
		this.time = time;
	}
	
	public long getPriod() {
	
		return priod;
	}

	public void setPriod(long priod) {
	
		this.priod = priod;
	}

	public String toString(){
		return Utils.toString(this);
	}
	
}
package cn.cche.cache;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import net.sourceforge.sizeof.SizeOf;

/**
 * @author chexingyou
 * @date 2013-5-23
 */
public class CacheMgr {

	private static Map> cache = new HashMap>();

	private CacheMgr() {

	}

	static {

		Timer timer = new Timer();
		TimerTask task = new TimerTask() {
			public void run() {

				System.out.println("      ...");
				
				Iterator it = cache.keySet().iterator();
				while (it.hasNext()) {
					Serializable regionkey = it.next();
					Map regionCache = cache.get(regionkey);
					Iterator regionit = regionCache.keySet().iterator();
					while (regionit.hasNext()) {
						Serializable key = regionit.next();
						CacheValue value = regionCache.get(key);
						if ((System.currentTimeMillis() - value.getTime()) / 1000 > value
								.getPriod()) {
							// evict(regionkey,key);
							System.out.println("    : " + regionkey + "/" + key);
							regionit.remove();

						}
					}
				}

			}
		};
		timer.schedule(task, 0, 5000);

	}

	public static Object get(Serializable region, Serializable key) {

		// sizeOf();
		Map regionCache = cache.get(region);
		CacheValue value = null;
		if (region != null && key != null) {
			regionCache = cache.get(region);
			if (regionCache == null)
				return null;
			value = regionCache.get(key);
			if (value != null) {
				if ((System.currentTimeMillis() - value.getTime()) / 1000 > value.getPriod()) {
					evict(region, key);
					value = null;
				} else {
					System.out.println("    : " + region + "/" + key);
				}
			}
		}

		return value == null ? null : value.getObj();
	}

	public static void put(Serializable region, Serializable key, CacheValue value) {

		if (key != null && value != null) {

			Map map = new HashMap();
			map.put(key, value);

			Map regionCache = cache.get(region);
			if (regionCache == null) {
				cache.put(region, map);
			} else {
				regionCache.putAll(map);
			}

			System.out.println("    : " + region + "/" + key);
		}
	}

	public static void evict(Object region, Object key) {

		if (region != null && key != null) {
			Map regionCache = cache.get(region);
			if (regionCache != null)
				cache.get(region).remove(key);
			System.out.println("    : " + region + "/" + key);
		}
	}

	public static void sizeOf() {

		String size = SizeOf.humanReadable(SizeOf.deepSizeOf(cache));
		System.out.println("    :" + size);
	}

}

좋은 웹페이지 즐겨찾기