寫Cache快取物件測試例項

瓜瓜東西發表於2014-08-06
package com.wanju.project001.zonghe.test;

import java.util.HashMap;

public class CacheObject {

	OCache oCache = new OCache();
	public static void main(String[] args) {
		CacheObject test = new CacheObject();
		test.init();
	}

	public void init()
	{
		oCache.setTimeYouxiao(3);//3s
		getStudentById(1);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		getStudentById(1);
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		getStudentById(1);
	}
	public Object getStudentById(int id) {
		Object result = oCache.getCacheObjectByKey(id+"");
		if(null ==result){
			StudentPojo student= new StudentPojo();
			student.setId(1);
			student.setName("zhangsan");
			oCache.addObject(1+"", student);
			return student;
		}
		return result;
	}
}

class OCache extends ICache {

	public synchronized HashMap getMap() {
		return map;
	}

	public synchronized void addObject(String key, Object object) {
		System.out.println("新增了快取,因為快取失效或者第一次新增物件");
		put(key, object);
	}

	public synchronized Object getCacheObjectByKey(String key) {
		return get(key);
	}
}

abstract class ICache {

	protected long timeYouxiao;
	protected boolean isUseing;

	protected HashMap<String, CachePack> map = new HashMap<String, CachePack>();

	public void put(String key, Object object) {
		map.put(key,
				new CachePack(System.currentTimeMillis(), System
						.currentTimeMillis() + timeYouxiao * 1000, key, object));
	}

	public Object get(String key) {
		if(map.get(key)!=null && isShiXiao(key))
		{
			System.out.println("使用了快取");
		}
		return map.get(key)==null || !isShiXiao(key)?null:(map.get(key).getObject());
	}

	public boolean isShiXiao(String key)
	{
		if(map.get(key).getCurrentTime()-System.currentTimeMillis()>0){
			return true;
		}
		return false;
	}
	public long getTimeYouxiao() {
		return timeYouxiao;
	}

	public void setTimeYouxiao(long timeYouxiao) {
		this.timeYouxiao = timeYouxiao;
	}

	public boolean isUseing() {
		return isUseing;
	}

	public void setUseing(boolean isUseing) {
		this.isUseing = isUseing;
	}

	public HashMap<String, CachePack> getMap() {
		return map;
	}

	public void setMap(HashMap<String, CachePack> map) {
		this.map = map;
	}
}

class CachePack {

	private long savingTime;
	private long currentTime;
	private String key;
	private Object object;

	public CachePack() {
	}

	public CachePack(long savingTime, long currentTime, String key,
			Object object) {
		this.savingTime = savingTime;
		this.currentTime = currentTime;
		this.key = key;
		this.object = object;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public long getSavingTime() {
		return savingTime;
	}

	public void setSavingTime(long savingTime) {
		this.savingTime = savingTime;
	}

	public long getCurrentTime() {
		return currentTime;
	}

	public void setCurrentTime(long currentTime) {
		this.currentTime = currentTime;
	}
}

class StudentPojo {

	private int id;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "StudentPojo [id=" + id + ", name=" + name + "]";
	}

}

相關文章