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

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

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

    Terry.Li-彬

    虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks

    1、OSCache是什么?
    ???? OSCache標記庫由OpenSymphony設計,它是一種開創性的緩存方案,它提供了在現有JSP頁面之內實現內存緩存的功能。OSCache是個 一個被廣泛采用的高性能的J2EE緩存框架,OSCache還能應用于任何Java應用程序的普通的緩存解決方案。
    2、OSCache的特點
    ????(1) 緩存任何對象:你可以不受限制的緩存部分jsp頁面或HTTP請求,任何java對象都可以緩存。
    ????(2) 擁有全面的API:OSCache API允許你通過編程的方式來控制所有的OSCache特性。
    ????(3) 永久緩存:緩存能被配置寫入硬盤,因此允許在應用服務器的多次生命周期間緩存創建開銷昂貴的數據。
    ????(4) 支持集群:集群緩存數據能被單個的進行參數配置,不需要修改代碼。
    ????(5) 緩存過期:你可以有最大限度的控制緩存對象的過期,包括可插入式的刷新策略(如果默認性能不能滿足需要時)。
    3、OSCache的安裝與配置
    ????網上已經有一個不錯的使用教程:http://blog.csdn.net/ezerg/archive/2004/10/14/135769.aspx
    4、有關“用OSCache進行緩存對象”的研究
    ????這個是我今天要說的東西。網上對于OSCache緩存Web頁面很多說明和例子,但對于緩存對象方面說得不多,我就把自已寫得一些東西放出來,讓大家看一看是怎樣緩存對象的!
    ??? 我基于GeneralCacheAdministrator類來寫的BaseCache類
    ???

    1. package ?com.klstudio.cache; ??
    2. ??
    3. import ?java.util.Date; ??
    4. ??
    5. import ?com.opensymphony.oscache.base.NeedsRefreshException; ??
    6. import ?com.opensymphony.oscache.general.GeneralCacheAdministrator; ??
    7. ??
    8. public ? class ?BaseCache? extends ?GeneralCacheAdministrator?{ ??
    9. ???? //過期時間(單位為秒); ??
    10. ???? private ? int ?refreshPeriod; ??
    11. ???? //關鍵字前綴字符; ??
    12. ???? private ?String?keyPrefix; ??
    13. ???? ??
    14. ???? private ? static ? final ? long ?serialVersionUID?=?-4397192926052141162L; ??
    15. ???? ??
    16. ???? public ?BaseCache(String?keyPrefix, int ?refreshPeriod){ ??
    17. ???????? super (); ??
    18. ???????? this .keyPrefix?=?keyPrefix; ??
    19. ???????? this .refreshPeriod?=?refreshPeriod; ??
    20. ????} ??
    21. ???? //添加被緩存的對象; ??
    22. ???? public ? void ?put(String?key,Object?value){ ??
    23. ???????? this .putInCache( this .keyPrefix+ "_" +key,value); ??
    24. ????} ??
    25. ???? //刪除被緩存的對象; ??
    26. ???? public ? void ?remove(String?key){ ??
    27. ???????? this .flushEntry( this .keyPrefix+ "_" +key); ??
    28. ????} ??
    29. ???? //刪除所有被緩存的對象; ??
    30. ???? public ? void ?removeAll(Date?date){ ??
    31. ???????? this .flushAll(date); ??
    32. ????} ??
    33. ???? ??
    34. ???? public ? void ?removeAll(){ ??
    35. ???????? this .flushAll(); ??
    36. ????} ??
    37. ???? //獲取被緩存的對象; ??
    38. ???? public ?Object?get(String?key)? throws ?Exception{ ??
    39. ???????? try { ??
    40. ???????????? return ? this .getFromCache( this .keyPrefix+ "_" +key, this .refreshPeriod); ??
    41. ????????}? catch ?(NeedsRefreshException?e)?{ ??
    42. ???????????? this .cancelUpdate( this .keyPrefix+ "_" +key); ??
    43. ???????????? throw ?e; ??
    44. ????????} ??
    45. ??
    46. ????} ??
    47. ???? ??
    48. } ??
    49. ??
    50. ??
    Java代碼
    1. package ?com.klstudio.cache;??
    2. ??
    3. import ?java.util.Date;??
    4. ??
    5. import ?com.opensymphony.oscache.base.NeedsRefreshException;??
    6. import ?com.opensymphony.oscache.general.GeneralCacheAdministrator;??
    7. ??
    8. public ? class ?BaseCache? extends ?GeneralCacheAdministrator?{??
    9. ????//過期時間(單位為秒);??
    10. ????private?int?refreshPeriod;??
    11. ????//關鍵字前綴字符;??
    12. ????private?String?keyPrefix;??
    13. ??????
    14. ????private?static?final?long?serialVersionUID?=?-4397192926052141162L;??
    15. ??????
    16. ????public?BaseCache(String?keyPrefix,int?refreshPeriod){??
    17. ????????super();??
    18. ????????this.keyPrefix?=?keyPrefix;??
    19. ????????this.refreshPeriod?=?refreshPeriod;??
    20. ????}??
    21. ????//添加被緩存的對象;??
    22. ????public?void?put(String?key,Object?value){??
    23. ????????this.putInCache(this.keyPrefix+"_"+key,value);??
    24. ????}??
    25. ????//刪除被緩存的對象;??
    26. ????public?void?remove(String?key){??
    27. ????????this.flushEntry(this.keyPrefix+"_"+key);??
    28. ????}??
    29. ????//刪除所有被緩存的對象;??
    30. ????public?void?removeAll(Date?date){??
    31. ????????this.flushAll(date);??
    32. ????}??
    33. ??????
    34. ????public?void?removeAll(){??
    35. ????????this.flushAll();??
    36. ????}??
    37. ????//獲取被緩存的對象;??
    38. ????public?Object?get(String?key)?throws?Exception{??
    39. ????????try{??
    40. ????????????return?this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);??
    41. ????????}?catch?(NeedsRefreshException?e)?{??
    42. ????????????this.cancelUpdate(this.keyPrefix+"_"+key);??
    43. ????????????throw?e;??
    44. ????????}??
    45. ??
    46. ????}??
    47. ??????
    48. }??

    ?? 通過CacheManager類來看怎樣緩存對象的,這個類中所用的News只是具體功能的類,我就不貼出來了,你可以自己寫一個!
    ???
    1. package?com.klstudio; ??
    2. ??
    3. import?com.klstudio.News; ??
    4. import?com.klstudio.cache.BaseCache; ??
    5. ??
    6. public?class?CacheManager?{ ??
    7. ???? ??
    8. ????private?BaseCache?newsCache; ??
    9. ??
    10. ???? ??
    11. ????private?static?CacheManager?instance; ??
    12. ????private?static?Object?lock?=?new?Object(); ??
    13. ???? ??
    14. ????public?CacheManager()?{ ??
    15. ????????//這個根據配置文件來,初始BaseCache而已; ??
    16. ????????newsCache?=?new?BaseCache("news",1800);????? ??
    17. ????} ??
    18. ???? ??
    19. ????public?static?CacheManager?getInstance(){ ??
    20. ????????if?(instance?==?null){ ??
    21. ????????????synchronized(?lock?){ ??
    22. ????????????????if?(instance?==?null){ ??
    23. ????????????????????instance?=?new?CacheManager(); ??
    24. ????????????????} ??
    25. ????????????} ??
    26. ????????} ??
    27. ????????return?instance; ??
    28. ????} ??
    29. ??
    30. ????public?void?putNews(News?news)?{ ??
    31. ????????//?TODO?自動生成方法存根 ??
    32. ????????newsCache.put(news.getID(),news); ??
    33. ????} ??
    34. ??
    35. ????public?void?removeNews(String?newsID)?{ ??
    36. ????????//?TODO?自動生成方法存根 ??
    37. ????????newsCache.remove(newsID); ??
    38. ????} ??
    39. ??
    40. ????public?News?getNews(String?newsID)?{ ??
    41. ????????//?TODO?自動生成方法存根 ??
    42. ????????try?{ ??
    43. ????????????return?(News)?newsCache.get(newsID); ??
    44. ????????}?catch?(Exception?e)?{ ??
    45. ????????????//?TODO?自動生成?catch?塊 ??
    46. ????????????System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage()); ??
    47. ????????????News?news?=?new?News(newsID); ??
    48. ????????????this.putNews(news); ??
    49. ????????????return?news; ??
    50. ????????} ??
    51. ????} ??
    52. ??
    53. ????public?void?removeAllNews()?{ ??
    54. ????????//?TODO?自動生成方法存根 ??
    55. ????????newsCache.removeAll(); ??
    56. ????} ??
    57. ??
    58. } ??
    59. ??
    Java代碼
    1. package?com.klstudio;??
    2. ??
    3. import?com.klstudio.News;??
    4. import?com.klstudio.cache.BaseCache;??
    5. ??
    6. public?class?CacheManager?{??
    7. ??????
    8. ????private?BaseCache?newsCache;??
    9. ??
    10. ??????
    11. ????private?static?CacheManager?instance;??
    12. ????private?static?Object?lock?=?new?Object();??
    13. ??????
    14. ????public?CacheManager()?{??
    15. ????????//這個根據配置文件來,初始BaseCache而已;??
    16. ????????newsCache?=?new?BaseCache("news",1800);???????
    17. ????}??
    18. ??????
    19. ????public?static?CacheManager?getInstance(){??
    20. ????????if?(instance?==?null){??
    21. ????????????synchronized(?lock?){??
    22. ????????????????if?(instance?==?null){??
    23. ????????????????????instance?=?new?CacheManager();??
    24. ????????????????}??
    25. ????????????}??
    26. ????????}??
    27. ????????return?instance;??
    28. ????}??
    29. ??
    30. ????public?void?putNews(News?news)?{??
    31. ????????//?TODO?自動生成方法存根??
    32. ????????newsCache.put(news.getID(),news);??
    33. ????}??
    34. ??
    35. ????public?void?removeNews(String?newsID)?{??
    36. ????????//?TODO?自動生成方法存根??
    37. ????????newsCache.remove(newsID);??
    38. ????}??
    39. ??
    40. ????public?News?getNews(String?newsID)?{??
    41. ????????//?TODO?自動生成方法存根??
    42. ????????try?{??
    43. ????????????return?(News)?newsCache.get(newsID);??
    44. ????????}?catch?(Exception?e)?{??
    45. ????????????//?TODO?自動生成?catch?塊??
    46. ????????????System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());??
    47. ????????????News?news?=?new?News(newsID);??
    48. ????????????this.putNews(news);??
    49. ????????????return?news;??
    50. ????????}??
    51. ????}??
    52. ??
    53. ????public?void?removeAllNews()?{??
    54. ????????//?TODO?自動生成方法存根??
    55. ????????newsCache.removeAll();??
    56. ????}??
    57. ??
    58. }?
    posted on 2010-08-09 15:57 禮物 閱讀(387) 評論(0)  編輯  收藏 所屬分類: cache
    主站蜘蛛池模板: 国产亚洲成人在线播放va| 亚洲av无码专区国产不乱码| 久久久久久毛片免费播放| 亚洲成a人片在线观看无码| 黄色视屏在线免费播放| 亚洲精品无码高潮喷水在线| 亚洲精品视频免费| 亚洲最大av无码网址| 国产V片在线播放免费无码| 亚洲免费视频一区二区三区| 亚洲精品无码久久久久久久| 99精品视频免费观看| 久久久无码精品亚洲日韩蜜臀浪潮 | 一区二区3区免费视频| 亚洲国产精品自在拍在线播放| 国产成人在线观看免费网站 | 男女猛烈激情xx00免费视频 | 亚洲制服丝袜第一页| 久久久久久国产a免费观看黄色大片| 亚洲免费在线视频播放| 无码一区二区三区免费视频| 毛片a级毛片免费播放下载| 亚洲一卡2卡三卡4卡无卡下载 | 精品一区二区三区免费毛片| 日韩成人免费aa在线看| 亚洲色婷婷一区二区三区| 日韩精品无码免费专区午夜| 亚洲国产国产综合一区首页| 国产亚洲美女精品久久| 亚洲无码视频在线| 免费国产在线视频| 亚洲福利中文字幕在线网址| 久草免费福利在线| 久久亚洲精品无码AV红樱桃| 一二三四视频在线观看中文版免费| 亚洲乱色伦图片区小说| 亚洲精品色婷婷在线影院| 伊人久久精品亚洲午夜| 人妻18毛片a级毛片免费看| 亚洲国产精品va在线播放| 国产精品视频免费观看|