简单缓存实现

来源:互联网 发布:人种 知乎 编辑:程序博客网 时间:2024/06/05 16:09

实现:添加内容到缓存,删除缓存内容,每隔一段时间就自动清空缓存

感谢百度pkzahc 用户的支持,本文纯模仿

public class CacheMgr {

private static Map cacheMap = new HashMap();
private static Map cacheConfMap = new HashMap();
private static Integer initCount  = 0;
 
private CacheMgr(){
 
}
private static CacheMgr cm = null;
public static CacheMgr getInstance(){
 if(cm==null){
System.out.println("init -- "+initCount);
initCount++;
  cm = new CacheMgr();
  Thread t = new ClearCache();


  t.start();
 }
 return cm;
}
/**
 * 增加缓存
 * @param key
 * @param value
 * @param ccm 缓存对象
 * @return 
 */
public  boolean addCache(Object key,Object value,CacheConfModel ccm){
 boolean flag = false;
 cacheMap.put(key, value);
 cacheConfMap.put(key, ccm);
 
 System.out.println("now addcache=="+cacheMap.size());
 return true;
}
/**
 * 删除缓存
 * @param key
 * @return 
 */
public  boolean removeCache(Object key){
 cacheMap.remove(key);
 cacheConfMap.remove(key);
 
 System.out.println("now removeCache=="+cacheMap.size());
 
 return true;
}
/**
 * 清除缓存的类
 * @author wanglj
 * 继承Thread线程类
 */
private static class ClearCache extends Thread{

 public void run(){
 Thread().currentThread().setName("hello");
int i = 0;
  while(true){
System.out.println(i);
   Set tempSet = new HashSet();
   Set set = cacheConfMap.keySet();
   Iterator it = set.iterator();
   while(it.hasNext()){
    Object key = it.next();
    CacheConfModel ccm = (CacheConfModel)cacheConfMap.get(key);
    //比较是否需要清除
    if(!ccm.isForever()){
     if((new Date().getTime()-ccm.getBeginTime())>= ccm.getDurableTime()*60*1000){
      //可以清除,先记录下来
      tempSet.add(key);
     }
    }
   }
   //真正清除
   Iterator tempIt = tempSet.iterator();
   while(tempIt.hasNext()){
    Object key = tempIt.next();
    cacheMap.remove(key);
    cacheConfMap.remove(key);
    
   }
   System.out.println("now thread================>"+cacheMap.size());
   //休息
   try {
    Thread.sleep(5000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   i++;
  }
 }


private Thread Thread() {
// TODO Auto-generated method stub
return null;
}
}
 
 
 
public static void main(String[] args){
CacheMgr cm = CacheMgr.getInstance();
CacheConfModel ccm = new CacheConfModel();
ccm.setBeginTime(new Date().getTime());
ccm.setDurableTime(2);
cm.addCache("A", "A", ccm);

Map map=Thread.getAllStackTraces();//获取当前运行的所有线程
System.out.println(map.size());
Set sets = map.entrySet();
Iterator it = sets.iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
Thread t = (Thread) entry.getKey();
System.out.println(t.getId()+":"+t.getName());
}
//System.exit( 0 );

}

}




public class CacheConfModel {
private long beginTime;
private boolean isForever = false;
private int durableTime;
public long getBeginTime() {
 return beginTime;
}
public void setBeginTime(long beginTime) {
 this.beginTime = beginTime;
}
public boolean isForever() {
 return isForever;
}
public void setForever(boolean isForever) {
 this.isForever = isForever;
}
public int getDurableTime() {
 return durableTime;
}
public void setDurableTime(int durableTime) {
 this.durableTime = durableTime;
}
}

原创粉丝点击