您的位置:首页 > 编程语言 > Java开发

自己动手实现java中cache

2016-05-21 16:43 609 查看
实现思路:
创建一个静态Hashtable用于保存key和value,对于cache过期后的方法回调,在cache过期后,再访问cache的时候进行,避免了使用定时器轮询过期时间,进行cache清除的效率损耗。
使用synchronized关键字进行多线程同步。
包括二个类和一个接口:
cache类:里面都是静态方法,提供基于key,value的方法进行cache的添加,修改,访问,进行cache过期后调用callback方法。

cacheitem类:用于管理每个条目的cache内容和超时时间回调方法

ICacheMethod接口:cache到期回调方法需要实现的接口

cache类:里面都是静态方法

import java.util.Date;

public class Cache {

private static java.util.Hashtable<String, Object> __cacheList = new java.util.Hashtable<String, Object>();

public Cache() {

}

// 添加cache,不过期
public synchronized static void add(String key, Object value) {
Cache.add(key, value, -1);
}

// 添加cache有过期时间
public synchronized static void add(String key, Object value, long timeOut) {
Cache.add(key, value, timeOut, null);
}

// 添加cache有过期时间并且具有回调方法
public synchronized static void add(String key, Object value, long timeOut, ICacheMethod callback) {
if (timeOut > 0) {
timeOut += new Date().getTime();
}
CacheItem item = new CacheItem(key, value, timeOut, callback);
Cache.__cacheList.put(key, item);
}

// 获取cache
public synchronized static Object get(String key) {
Object obj = Cache.__cacheList.get(key);
if (obj == null) {
return null;
}
CacheItem item = (CacheItem) obj;
boolean expired = Cache.cacheExpired(key);
if (expired == true) // 已过期
{
if (item.getCallback() == null) {
Cache.remove(key);
return null;
} else {
ICacheMethod callback = item.getCallback();
callback.execute(key);
expired = Cache.cacheExpired(key);
if (expired == true) {
Cache.remove(key);
return null;
}
}
}
return item.getValue();
}

// 移除cache
public synchronized static void remove(String key) {
Object obj = Cache.__cacheList.get(key);
if (obj != null) {
obj = null;
}
Cache.__cacheList.remove(key);
}

// 清理所有cache对象
public synchronized static void clear() {

for (String s : Cache.__cacheList.keySet()) {
Cache.__cacheList.put(s, null);
}
Cache.__cacheList.clear();
}

// 判断是否过期
private static boolean cacheExpired(String key) {
CacheItem item = (CacheItem) Cache.__cacheList.get(key);
if (item == null) {
return false;
}
long milisNow = new Date().getTime();
long milisExpire = item.getTimeOut();
if (milisExpire <= 0) { // 不过期
return false;
} else if (milisNow >= milisExpire) {
return true;
} else {
return false;
}
}
}


  

public class CacheItem {

private String key;
private Object value;
private long timeOut;
private ICacheMethod callback = null;

public CacheItem() {

}

public ICacheMethod getCallback() {
return callback;
}

public void setCallback(ICacheMethod callback) {
this.callback = callback;
}

public CacheItem(String key, Object value) {
this.key = key;
this.value = value;
this.timeOut = 0;
}

public CacheItem(String key, Object value, long timeOut) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
}

public CacheItem(String key, Object value, long timeOut, ICacheMethod callback) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
this.callback = callback;
}

public String getKey() {
return key;
}

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

public Object getValue() {
return value;
}

public void setValue(Object value) {
this.value = value;
}

public long getTimeOut() {
return timeOut;
}

public void setTimeOut(long timeOut) {
this.timeOut = timeOut;
}
}


  

public interface ICacheMethod {
public void execute(String key);
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: