敬的世界

          常用鏈接

          統計

          最新評論

          Page.Cache. HttpContext.Cache. HttpRuntime.Cache. Hsahtable有什么區別,怎樣用

          轉自:http://zwjy.gjjblog.com/archives/3683/

             HttpContext.Cache,HttpRuntime.Cache,用法上沒有區別,就是使用范圍不同.  
             HttpContext.Cache是基于上下文,對同一個用戶起做用,如果換了一個訪問,那么這個CACHE就不起做用了.
             HttpRuntime.Cache是全局的,對任務人都有做,只要有內容,任務用戶都可以訪問.
             
          ????public?class?HttpRuntimeCache
          ????
          {
          ????
          ????????
          public?static?string?Instance()
          ????????
          {
          ????????????
          string?vc=(string)HttpRuntime.Cache["Test"];
          ????????????
          if(vc==null)
          ????????????
          {
          ????????????????HttpRuntime.Cache.Add(
          "Test","test",?null,System.DateTime.Now.AddDays(10),TimeSpan.Zero,
          ????????????????CacheItemPriority.Default,
          null);
          ????????????}

          ????????????
          return?vc;
          ????????}


          ????}


             Page.Cache實際上是訪問其它CACHE的接口.可以通過Page.Cache來訪問HttpContext.Cache,HttpRuntime.Cache里面的東西.

             還有另外一種保存Cache方式是,使用Hashtable等來保存,值的一說的是這種Cache也是全局 ,而且是不可過期的 ,我想這是與 HttpRuntime.Cache的一個區別吧.至于安全性能方面我還沒有去驗證.Hashtable保存方式比較特別,就是NEW的時候要使用同步關健字.

          ?1public?class?hashcache
          ?2????{
          ?3???????private?static?Hashtable?parmCache?=?Hashtable.Synchronized(new?Hashtable());
          ?4
          ?5????????public?static?void?CacheParameters(string?cacheKey,?string?cmdParms)?
          ?6????????{
          ?7????????????parmCache[cacheKey]?=?cmdParms;
          ?8????????}

          ?9
          10????????public?static?string?GetCachedParameters(string?cacheKey)?
          11????????{
          12????????????return?(string)parmCache[cacheKey];
          13????????}

          14????}

          posted on 2009-01-13 20:20 picture talk 閱讀(645) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 个旧市| 宜阳县| 通江县| 庆阳市| 翼城县| 洮南市| 西盟| 桂平市| 原平市| 宜章县| 越西县| 东港市| 洞头县| 新干县| 新化县| 广汉市| 上思县| 周口市| 长葛市| 黄冈市| 福州市| 嵊州市| 河曲县| 措美县| 田阳县| 平南县| 定南县| 体育| 南丰县| 隆德县| 维西| 宿迁市| 修文县| 南昌市| 巴南区| 洛川县| 贡山| 泽州县| 金湖县| 辛集市| 拜泉县|