package com
/*
設計思想來自-回欽波(qq:
*/
public class CacheConfModel implements java
private long beginTime;
private boolean isForever = false;
private int durableTime;
public long getBeginTime() {
return beginTime;
}
public void setBeginTime(long beginTime) {
this
}
public boolean isForever() {
return isForever;
}
public void setForever(boolean isForever) {
this
}
public int getDurableTime() {
return durableTime;
}
public void setDurableTime(int durableTime) {
this
}
}
package com
import java
import test
/*
設計思想來自-回欽波(qq:
*/
public class CacheMgr {
private static Map cacheMap = new HashMap();
private static Map cacheConfMap = new HashMap();
private CacheMgr(){
}
private static CacheMgr cm = null;
public static CacheMgr getInstance(){
if(cm==null){
cm = new CacheMgr();
Thread t = new ClearCache();
t
}
return cm;
}
/**
* 增加緩存
* @param key
* @param value
* @param ccm 緩存對象
* @return
*/
public boolean addCache(Object key
boolean flag = false;
cacheMap
cacheConfMap
System
return true;
}
/**
* 刪除緩存
* @param key
* @return
*/
public boolean removeCache(Object key){
cacheMap
cacheConfMap
System
return true;
}
/**
* 清除緩存的類
* @author wanglj
* 繼承Thread線程類
*/
private static class ClearCache extends Thread{
public void run(){
while(true){
Set tempSet = new HashSet();
Set set = cacheConfMap
Iterator it = erator();
while(it
Object key = it
CacheConfModel ccm = (CacheConfModel)cacheConfMap
//比較是否需要清除
if(!ccm
if((new Date()
//可以清除
tempSet
}
}
}
//真正清除
Iterator tempIt = erator();
while(tempIt
Object key = tempIt
cacheMap
cacheConfMap
}
System
//休息
try {
Thread
} catch (InterruptedException e) {
// TODO Auto
e
}
}
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25737.html