Terry.Li-彬

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

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

          1、OSCache是什么?
          ???? OSCache標記庫由OpenSymphony設計,它是一種開創(chuàng)性的緩存方案,它提供了在現(xiàn)有JSP頁面之內(nèi)實現(xiàn)內(nèi)存緩存的功能。OSCache是個 一個被廣泛采用的高性能的J2EE緩存框架,OSCache還能應用于任何Java應用程序的普通的緩存解決方案。
          2、OSCache的特點
          ????(1) 緩存任何對象:你可以不受限制的緩存部分jsp頁面或HTTP請求,任何java對象都可以緩存。
          ????(2) 擁有全面的API:OSCache API允許你通過編程的方式來控制所有的OSCache特性。
          ????(3) 永久緩存:緩存能被配置寫入硬盤,因此允許在應用服務器的多次生命周期間緩存創(chuàng)建開銷昂貴的數(shù)據(jù)。
          ????(4) 支持集群:集群緩存數(shù)據(jù)能被單個的進行參數(shù)配置,不需要修改代碼。
          ????(5) 緩存過期:你可以有最大限度的控制緩存對象的過期,包括可插入式的刷新策略(如果默認性能不能滿足需要時)。
          3、OSCache的安裝與配置
          ????網(wǎng)上已經(jīng)有一個不錯的使用教程:http://blog.csdn.net/ezerg/archive/2004/10/14/135769.aspx
          4、有關“用OSCache進行緩存對象”的研究
          ????這個是我今天要說的東西。網(wǎng)上對于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. ????????//這個根據(jù)配置文件來,初始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. ????????//這個根據(jù)配置文件來,初始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 禮物 閱讀(390) 評論(0)  編輯  收藏 所屬分類: cache
          主站蜘蛛池模板: 泾阳县| 天长市| 宣化县| 雷州市| 财经| 杂多县| 宜宾县| 尼木县| 金湖县| 安阳县| 托里县| 集贤县| 上饶市| 岑巩县| 泰和县| 诏安县| 中宁县| 梨树县| 延寿县| 中超| 乌海市| 四子王旗| 淮南市| 东城区| 类乌齐县| 阜南县| 新绛县| 图木舒克市| 昭觉县| 南丰县| 陕西省| 黄浦区| 岐山县| 当阳市| 太原市| 黄平县| 开平市| 塔河县| 靖州| 荥阳市| 广东省|