Java愛好者

          一個(gè)堅(jiān)定的Java愛好者,歡迎和我討論
          隨筆 - 7, 文章 - 8, 評(píng)論 - 6, 引用 - 0
          數(shù)據(jù)加載中……

          一個(gè)簡(jiǎn)單的Cache工具類,支持多線程


          import java.util.Calendar;
          import java.util.HashMap;
          import java.util.Hashtable;

          public class CacheHelper {
          ??? public final static int default_interval_second = 20;
          ??? public static boolean?? ISDEBUG???????????????? = true;
          ??? private static HashMap? map???????????????????? = new HashMap();
          ??? private static Object?? obj???????????????????? = new Object();

          ??? public static ICache getCache(String uniquekey) {
          ??????? synchronized (obj) {
          ??????????? ICache ic = (ICache) map.get(uniquekey);
          ??????????? if (ic == null) {
          ??????????????? ic = new CacheEntity(uniquekey);
          ??????????????? map.put(uniquekey, ic);
          ??????????? }
          ??????????? return ic;
          ??????? }
          ??? }

          ??? public interface ICache {
          ??????? Object get();

          ??????? Object get(long timestamp);

          ??????? void set(Object obj);

          ??????? void set(long timestamp, Object obj);

          ??????? void clear(String regx);

          ??????? void clearAll();
          ??? }

          ??? private static class CacheEntity implements ICache {
          ??????? private Hashtable hm??????? = new Hashtable();
          ??????? private Object??? uniquekey = this;
          ??????? private long????? timestamp = Long.MIN_VALUE;

          ??????? public CacheEntity(Object uniquekey) {
          ??????????? this.uniquekey = uniquekey;
          ??????? }

          ??????? public Object get(long timestamp) {
          ??????????? if (timestamp <= this.timestamp) {
          ??????????????? // 當(dāng)前時(shí)間在設(shè)定的過期時(shí)間之前,表示還沒有過期。
          ??????????????? // 如果還沒過期,那么就返回。
          ??????????????? Object obj = hm.get(uniquekey);
          ??????????????? if (ISDEBUG && obj != null) {
          ??????????????????? // 輸出日志。
          ??????????????????? System.out.println("[" + timestamp + "][Reading cache][" + uniquekey + "]...");
          ??????????????? }
          ??????????????? return obj;
          ??????????? }
          ??????????? else {
          ??????????????? // 清理垃圾。
          ??????????????? hm.remove(uniquekey);
          ??????????????? return null;
          ??????????? }
          ??????? }

          ??????? public void set(long timestamp, Object obj) {
          ??????????? this.timestamp = timestamp;
          ??????????? hm.put(uniquekey, obj);
          ??????? }

          ??????? public Object get() {
          ??????????? Calendar cal = Calendar.getInstance();
          ??????????? return get(cal.getTimeInMillis());
          ??????? }

          ??????? public void set(Object obj) {
          ??????????? Calendar cal = Calendar.getInstance();
          ??????????? cal.add(Calendar.SECOND, default_interval_second);
          ??????????? set(cal.getTimeInMillis(), obj);
          ??????? }

          ??????? public void clear(String regx) {
          ??????????? java.util.Enumeration keys = hm.keys();
          ??????????? while (keys.hasMoreElements()) {
          ??????????????? String key = (String) keys.nextElement();
          ??????????????? if (key.matches(regx)) {
          ??????????????????? hm.remove(key);
          ??????????????? }
          ??????????? }
          ??????? }

          ??????? public void clearAll() {
          ??????????? hm.clear();
          ??????? }
          ??? }
          }

          posted on 2006-08-03 09:29 JStar 閱讀(1281) 評(píng)論(1)  編輯  收藏

          評(píng)論

          # re: 一個(gè)簡(jiǎn)單的Cache工具類,支持多線程[未登錄]  回復(fù)  更多評(píng)論   

          我覺得寫的一般
          有很多問題存在
          2009-10-09 15:56 | 新手

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 绥化市| 西峡县| 新化县| 台北市| 邢台县| 盐亭县| 九龙城区| 桂林市| 云林县| 上虞市| 平原县| 五家渠市| 洞头县| 内黄县| 什邡市| 大化| 天水市| 达拉特旗| 辽阳市| 福鼎市| 饶河县| 东乌珠穆沁旗| 金山区| 景泰县| 射洪县| 依兰县| 五常市| 花莲县| 庆元县| 彰化市| 奉新县| 中江县| 藁城市| 长丰县| 桐梓县| 区。| 苏尼特右旗| 甘德县| 荃湾区| 云林县| 宁陕县|