<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java愛好者

    一個堅定的Java愛好者,歡迎和我討論
    隨筆 - 7, 文章 - 8, 評論 - 6, 引用 - 0
    數據加載中……

    一個簡單的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) {
    ??????????????? // 當前時間在設定的過期時間之前,表示還沒有過期。
    ??????????????? // 如果還沒過期,那么就返回。
    ??????????????? 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 閱讀(1275) 評論(1)  編輯  收藏

    評論

    # re: 一個簡單的Cache工具類,支持多線程[未登錄]  回復  更多評論   

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

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 好久久免费视频高清| 久久国产乱子伦免费精品| 成人免费无遮挡无码黄漫视频| 亚洲国产精品网站久久| 亚洲免费中文字幕| 亚洲经典在线中文字幕| 国产又黄又爽又猛免费app| 亚洲伦理中文字幕| 妞干网在线免费视频| 西西人体大胆免费视频| 亚洲区日韩区无码区| 日本一区午夜艳熟免费| 亚洲男人的天堂在线播放| 99爱在线精品免费观看| 亚洲欧美日韩中文无线码| 国产成人无码免费视频97| 乱爱性全过程免费视频| 亚洲中文字幕久久精品无码喷水 | 亚洲AV综合色区无码一区爱AV| 国产一级高青免费| 亚洲视频一区二区三区| 成年女人毛片免费观看97| 特级毛片A级毛片100免费播放| 亚洲中文字幕无码不卡电影| 亚洲成年人免费网站| 亚洲乱码在线观看| 国产L精品国产亚洲区久久| 99精品一区二区免费视频| 亚洲熟妇AV乱码在线观看| 亚洲最大av无码网址| 久99久精品免费视频热77| 亚洲五月综合缴情婷婷| 亚洲区日韩区无码区| 亚洲免费在线视频播放| 免费一区二区三区在线视频| 青青草原精品国产亚洲av| 国产hs免费高清在线观看| 男女作爱在线播放免费网站| 亚洲狠狠婷婷综合久久蜜芽| 久久亚洲国产欧洲精品一| 午夜毛片不卡高清免费|