偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

ASP.NET性能優(yōu)化之構(gòu)建自定義文件緩存

開發(fā) 后端
今天我們將談到的是ASP.NET性能優(yōu)化之構(gòu)建自定義文件緩存,在.NET4.0前一直是基于內(nèi)存的,現(xiàn)在已經(jīng)可以有多種選擇創(chuàng)建自己的緩存。

  ASP.NET的輸出緩存(即靜態(tài)HTML)在.NET4.0前一直是基于內(nèi)存的。這意味著如果我們的站點(diǎn)含有大量的緩存,則很容易消耗掉本機(jī)內(nèi)存?,F(xiàn)在,借助于.NET4.0中的OutputCacheProvider,我們可以有多種選擇創(chuàng)建自己的緩存。如,我們可以把HTML輸出緩存存儲(chǔ)到memcached分布式集群服務(wù)器,或者M(jìn)ongoDB中(一種常用的面向文檔數(shù)據(jù)庫,不妨閱讀本篇http://msdn.microsoft.com/zh-cn/magazine/gg650661.aspx)。當(dāng)然,我們也可以把緩存作為文件存儲(chǔ)到硬盤上,考慮到可擴(kuò)展性,這是一種最廉價(jià)的做法,本文就是介紹如果構(gòu)建自定義文件緩存。

  1:OutputCacheProvider

  OutputCacheProvider是一個(gè)抽象基類,我們需要override其中的四個(gè)方法,它們分別是:

  Add 方法,將指定項(xiàng)插入輸出緩存中。

  Get 方法,返回對輸出緩存中指定項(xiàng)的引用。

  Remove 方法,從輸出緩存中移除指定項(xiàng)。

  Set 方法,將指定項(xiàng)插入輸出緩存中,如果該項(xiàng)已緩存,則覆蓋該項(xiàng)。

  2:創(chuàng)建自己的文件緩存處理類

  該類型為FileCacheProvider,代碼如下:

  1.   public class FileCacheProvider : OutputCacheProvider  
  2.   {  
  3.   private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);  
  4.  public override void Initialize(string name, NameValueCollection attributes)  
  5.   {  
  6.   base.Initialize(name, attributes);  
  7.   CachePath = HttpContext.Current.Server.MapPath(attributes["cachePath"]);  
  8.   }  
  9.   public override object Add(string key, object entry, DateTime utcExpiry)  
  10.   {  
  11.   Object obj = Get(key);  
  12.   if (obj != null//這一步很重要  
  13.   {  
  14.   return obj;  
  15.   }  
  16.   Set(key,entry,utcExpiry);  
  17.   return entry;  
  18.   }  
  19.   public override object Get(string key)  
  20.   {  
  21.   string path = ConvertKeyToPath(key);  
  22.   if (!File.Exists(path))  
  23.   {  
  24.   return null;  
  25.   }  
  26.   CacheItem item = null;  
  27.   using (FileStream file = File.OpenRead(path))  
  28.   {  
  29.   var formatter = new BinaryFormatter();  
  30.   item = (CacheItem)formatter.Deserialize(file);  
  31.   }  
  32.   if (item.ExpiryDate <= DateTime.Now.ToUniversalTime())  
  33.   {  
  34.   log.Info(item.ExpiryDate + "*" + key);  
  35.   Remove(key);  
  36.   return null;  
  37.   }  
  38.   return item.Item;  
  39.   }  
  40.   public override void Set(string key, object entry, DateTime utcExpiry)  
  41.   {  
  42.   CacheItem item = new CacheItem(entry, utcExpiry);  
  43.   string path = ConvertKeyToPath(key);  
  44.   using (FileStream file = File.OpenWrite(path))  
  45.   {  
  46.   BinaryFormatter formatter = new BinaryFormatter();  
  47.   formatter.Serialize(file, item);  
  48.   }  
  49.   }  
  50.   public override void Remove(string key)  
  51.   {  
  52.   string path = ConvertKeyToPath(key);  
  53.   if (File.Exists(path))  
  54.   File.Delete(path);  
  55.  }  
  56.   public string CachePath  
  57.   {  
  58.   get;  
  59.   set;  
  60.   }  
  61.   private string ConvertKeyToPath(string key)  
  62.   {  
  63.   string file = key.Replace('/''-');  
  64.   file += ".txt";  
  65.   return Path.Combine(CachePath, file);  
  66.   }  
  67.   }  
  68.   [Serializable]  
  69.   public class CacheItem  
  70.   {  
  71.   public DateTime ExpiryDate;  
  72.   public object Item;  
  73.   public CacheItem(object entry, DateTime utcExpiry)  
  74.   {  
  75.   Item = entry;  
  76.   ExpiryDate = utcExpiry;  
  77.   }  
  78.   } 

  有兩個(gè)地方需要特別說明:

  在Add方法中,有一個(gè)條件判斷,必須做出這樣的處理,否則緩存機(jī)制將會(huì)緩存***次的結(jié)果,過了有效期后緩存講失效并不再重建;

  在示例程序中,我們簡單的將緩存放到了Cache目錄下,在實(shí)際的項(xiàng)目實(shí)踐中,考慮到緩存的頁面將是成千上萬的,所以我們必須要做目錄分級,否則尋找并讀取緩存文件將會(huì)成為效率瓶頸,這會(huì)耗盡CPU。

  3:配置文件

  我們需要在Web.config中配置緩存處理程序是自定義的FileCacheProvider,即在 <system.web>下添加節(jié)點(diǎn):

  1.   <caching>  
  2.   <outputCache defaultProvider="FileCache">  
  3.   <providers>  
  4.   <add name="FileCache" type="MvcApplication2.Common.FileCacheProvider" cachePath="~/Cache" />  
  5.   </providers>  
  6.   </outputCache>  
  7.  </caching> 

  4:緩存的使用

  我們假設(shè)在MVC的控制中使用(如果要在ASP.NET頁面中使用,則在頁面中包含<%@OutputCache VaryByParam="none" Duration="10" %>),可以看到,Index是未進(jìn)行輸出緩存的,而Index2進(jìn)行了輸出緩存,緩存時(shí)間為10秒。

  1.   public class HomeController : Controller  
  2.   {  
  3.   private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);  
  4.   static string s_conn = "Data Source=192.168.0.77;Initial Catalog=luminjidb;User Id=sa;Password=sa;";  
  5.   public ActionResult Index()  
  6.   {  
  7.   using (DataSet ds = Common.SqlHelper.ExecuteDataset(s_conn, CommandType.Text, "select top 1* from NameTb a, DepTb b where a.DepID = b.ID ORDER BY NEWID()"))  
  8.   {  
  9.   ViewBag.Message = ds.Tables[0].Rows[0]["name"].ToString();  
  10.   }  
  11.   return View();  
  12.   }  
  13.   [OutputCache(Duration = 10, VaryByParam = "none")]  
  14.   public ActionResult Index2()  
  15.   {  
  16.   using (DataSet ds = Common.SqlHelper.ExecuteDataset(s_conn, CommandType.Text, "select top 1* from NameTb a, DepTb b where a.DepID = b.ID ORDER BY NEWID()"))  
  17.   {  
  18.   ViewBag.Message = ds.Tables[0].Rows[0]["name"].ToString();  
  19.   }  
  20.   return View();  
  21.   }  
  22.   } 

  5:查看下效果

  上面的代碼,在訪問了Index2后,將會(huì)在Cache文件夾下產(chǎn)生緩存文件,如下:

  現(xiàn)在,我們開始評價(jià)下有輸出緩存和無輸出緩存的性能對比,模擬100個(gè)用戶并發(fā)1000次請求如下:

可以看到,有輸出緩存后,吞吐率明顯提高了10倍。

  6:代碼下載

  FileCacheProvider的原始代碼來自于網(wǎng)絡(luò),我修改了其中的BUG,全部代碼下載如下:MvcApplication20110907.rar

  職業(yè)指導(dǎo):

  在使用某一技能三個(gè)月后,你還不是專家,即便使用時(shí)間是三年,你還不是。馬爾科姆·格萊德威爾在《異類》一書中指出,成為一名真正的專家,需要10000小時(shí)。10000小時(shí)!如果一天用10小時(shí),每天都學(xué)習(xí),則大概需要3年時(shí)間。如果一天5小時(shí),一年學(xué)習(xí)200天,則大概需要10年時(shí)間。10年!

原文:http://www.cnblogs.com/luminji/archive/2011/09/08/2169955.html

【編輯推薦】

  1. 詳解ASP.NET MVC 3新的Layout布局系統(tǒng)
  2. ASP.NET MVC中很酷的jQuery驗(yàn)證插件
  3. ASP.NET MVC 3新特性全解析
  4. ASP.NET MVC 3讓你瘋狂的五大理由
  5. 詳解ASP.NET MVC 3 beta新特性
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2011-10-19 09:41:15

ASP.NET性能優(yōu)化

2011-10-17 09:54:18

ASP.NET性能

2009-07-31 10:23:09

ASP.NET源碼DateTimePic

2011-02-13 09:37:55

ASP.NET

2011-02-22 09:16:24

高性能ASP.NET

2009-08-10 16:58:45

ASP.NET安裝部署

2009-08-13 16:22:18

ASP.NET性能優(yōu)化

2012-05-16 10:24:26

ASP.NET性能優(yōu)化

2009-08-13 15:49:18

ASP.NET性能優(yōu)化

2011-02-17 09:13:57

ASP.NET

2009-07-28 09:32:41

ASP.NET自定義控

2009-08-10 14:16:59

ASP.NET自定義控

2009-08-04 13:35:16

ASP.NET自定義樣

2009-08-06 17:13:56

ASP.NET自定義控

2011-02-13 09:17:02

ASP.NET

2009-08-06 09:18:01

ASP.NET自定義控ASP.NET控件開發(fā)

2011-02-23 09:49:40

ASP.NET

2009-11-24 15:11:21

ASP.NET MVC

2009-08-12 14:38:05

ASP.NET Dat

2011-04-19 10:33:16

ASP.NET自定義控
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號