/*
設計理念來自——惠欽波(qq:)
*/
公共類CacheConfModel實現java io Serializable{
private long beginTime
private boolean isForever = false
private int durableTime
public long getBeginTime(){
返回開始時間;
}
public void set begin time(long begin time ){
this beginTime = beginTime
}
public boolean is forever(){
return isForever
}
public void set forever(boolean is forever ){
this isForever = isForever
}
public int getDurableTime(){
返回durableTime
}
public void setDurableTime(int durable time ){
this durableTime = durableTime
}
}
包覃伯惠;
導入Java util *;
導入測試CacheConfModel
/*
設計理念來自——惠欽波(qq:)
*/
公共類CacheMgr {
私有靜態映射cache Map = new HashMap();
私有靜態映射cacheConfMap = new HashMap();
私有cache mgr(){
}
private static CacheMgr cm = null
公共靜態cache mgr getInstance(){
if(cm = = null ){
cm = new cache mgr();
thread t = new clear cache();
t start();
}
返回cm;
}
/**
*增加緩存
* @param key
* @param值
* @param ccm緩存對象
* @返回
*/
公共?布爾addCache(對象鍵對象值CacheConfModel CCM ){
布爾標誌= false
cacheMap put(鍵值);
cacheConfMap put(key CCM);
System out println(現在是add cache = =+cache map size());
返回true
}
/**
*刪除緩存
* @param key
* @返回
*/
公共?布爾removeCache(對象鍵){
cacheMap remove(鍵);
cacheConfMap remove(鍵);
System out println(現在為remove cache = =+cache map size());
返回true
}
/**
*清除緩存的類
* @作者wanglj
*繼承線程線程類
*/
私有靜態類ClearCache擴展線程{
公共void運行(){
while(true ){
set tempSet = new HashSet();
Set set = cacheConfMap鍵集();
叠代器it = erator();
while(它有Next())
object key = it next();
CacheConfModel CCM =(CacheConfModel)cacheConfMap get(key);
//比較是否需要清除。
如果(!CCM is forever()){
if((new Date()getTime()CCM getBeginTime())& gt;= CCM getDurableTime()* *){
//可以先清除再錄。
tempSet add(鍵);
}
}
}
//非常清晰
iterator tempIt = erator();
while(tempIt has next()){
object key = tempIt next();
cacheMap remove(鍵);
cacheConfMap remove(鍵);
}
系統輸出println(現在線程= = = = = = = = = = = = = = = = = = = & gt;+cache map size());
//休息
嘗試{
線程睡眠(* L);
} catch(中斷異常e ){
// TODO自動生成的catch塊
e printStackTrace();
}
}
}
}
lishixinzhi/Article/program/Java/hx/201311/25737