package cn.zyx;
import java.io.Serializable;
public class CacheEntity implements Serializable{
private static final long serialVersionUID = -4653471779730170360L;
/**
* 值
*/
private Object value;
/**
* 时间戳
*/
private long timestamp;
/**
* 过期时间
*/
private int expire;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public int getExpire() {
return expire;
}
public void setExpire(int expire) {
this.expire = expire;
}
public CacheEntity(Object value , long timestamp , int expire){
super();
this.value = value;
this.timestamp = timestamp;
this.expire = expire;
}
}
/**
* 简易的本地缓存实现类
*
* @author Administrator
*
*/
public class LocalCache {
// 默认的缓存容量
private static int DEFAULT_CAPACITY = 512;
// 做大的缓存容量
private static int MAX_CAPACITY = 100000;
// 刷新缓存的频率
private static int MONITOR_DERATION = 2;
static {
new Thread(new TimeoutTimerThread()).start();
}
private static ConcurrentHashMap<String, CacheEntity> cache = new ConcurrentHashMap<String, CacheEntity>(
DEFAULT_CAPACITY);
/**
* 将值通过序列化clone 处理后保存到缓存中,可以解决值引用的问题
*
* @param key
* @param value
* @param expire
* @return
*/
public boolean putKey(String key, Object value, int expire) {
return putCloneValue(key, value, expire);
}
/**
* 将值通过序列化clone 处理后保存到缓存中,可以解决值引用的问题
*
* @param key
* @param value
* @param timestamp
* @param expire
* @return
*/
private boolean putCloneValue(String key, Object value, int expire) {
if (cache.size() > MAX_CAPACITY) {
return false;
}
CacheEntity cacheEntity = clone(new CacheEntity(value,
System.nanoTime(), expire));
cache.put(key, cacheEntity);
return true;
}
/**
*
* 序列化 克隆处理
*
* @param object
* @return
*/
private <T extends Serializable> T clone(T object) {
T cloneObject = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(
baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
cloneObject = (T) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return cloneObject;
}
/**
* 从本地缓存中获取key对应的值,如果该值不存则则返回null
*
* @param key
* @return
*/
public Object getValue(String key) {
return cache.get(key).getValue();
}
/**
* 清空所有
*/
public void clear() {
cache.clear();
}
/**
* 过期处理线程
*
* @author Administrator
*
*/
static class TimeoutTimerThread implements Runnable {
public void run() {
while (true) {
try {
System.out.println("Cache monitor");
TimeUnit.SECONDS.sleep(MONITOR_DERATION);
checkTime();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 过期缓存的具体处理方法
*/
private static void checkTime() {
for (String key : cache.keySet()) {
CacheEntity cacheEntity = cache.get(key);
long timeout = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime()
- cacheEntity.getTimestamp());
if (cacheEntity.getExpire() > timeout) {
continue;
}
System.out.println(" 清除过期缓存 : " + key);
// 清除过期缓存和删除对应的缓存队列
cache.remove(key);
}
}
}
package cn.zyx;
public class TestCache {
public static void main(String []args){
LocalCache cache = new LocalCache();
cache.putKey(“name”, “zyx”, 0);
System.out.println(“name:”+cache.getValue(“name”));
cache.clear();
System.out.println(“name:”+cache.getValue(“name”));
}
}