簡單緩存

package com.william.job;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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.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
  * @param value
  * @param beginTime
  * @param durableTime -----分鐘
  * @param isForever
  * @return
  */
 public boolean addCache(Object key, Object value, long beginTime,int durableTime,boolean isForever) {
  cacheMap.put(key, value);
  CacheConfModel ccm = new CacheConfModel();
  ccm.setBeginTime(beginTime);
  ccm.setDurableTime(durableTime);
  ccm.setForever(isForever);
  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;
 }
 /**
  * 獲取緩存
  * @param key
  * @return
  */
 public Object getData(Object key)
 {
  return cacheMap.get(key);
 }
 
 
 /**
  * 清除緩存的類
  *
  * @author wanglj 繼承Thread線程類
  */
 private static class ClearCache extends Thread {
  public void run() {
   while (true) {
    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(60 * 1000L);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
}

 

 

 

package com.william.job;

public class CacheConfModel implements java.io.Serializable {
 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;
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章