一个简单的缓存管理

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<Serializable, Map<Serializable, CacheValue>> cache = new HashMap<Serializable, Map<Serializable, CacheValue>>();

	private CacheMgr() {

	}

	static {

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

				System.out.println("定时器工作中...");
				
				Iterator<Serializable> it = cache.keySet().iterator();
				while (it.hasNext()) {
					Serializable regionkey = it.next();
					Map<Serializable, CacheValue> regionCache = cache.get(regionkey);
					Iterator<Serializable> 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<Serializable, CacheValue> 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<Serializable, CacheValue> map = new HashMap<Serializable, CacheValue>();
			map.put(key, value);

			Map<Serializable, CacheValue> 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<Serializable, CacheValue> 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);
	}

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值