天天看點

ASP.NET性能優化之建構自定義檔案緩存

ASP.NET的輸出緩存(即靜态HTML)在.NET4.0前一直是基于記憶體的。這意味着如果我們的站點含有大量的緩存,則很容易消耗掉本機記憶體。現在,借助于.NET4.0中的OutputCacheProvider,我們可以有多種選擇建立自己的緩存。如,我們可以把HTML輸出緩存存儲到memcached分布式叢集伺服器,或者MongoDB中(一種常用的面向文檔資料庫,不妨閱讀本篇 http://msdn.microsoft.com/zh-cn/magazine/gg650661.aspx

)。當然,我們也可以把緩存作為檔案存儲到硬碟上,考慮到可擴充性,這是一種最廉價的做法,本文就是介紹如果建構自定義檔案緩存。

1:OutputCacheProvider

OutputCacheProvider是一個抽象基類,我們需要override其中的四個方法,它們分别是:

Add 方法,将指定項插入輸出緩存中。

Get 方法,傳回對輸出緩存中指定項的引用。

Remove 方法,從輸出緩存中移除指定項。

Set 方法,将指定項插入輸出緩存中,如果該項已緩存,則覆寫該項。

2:建立自己的檔案緩存處理類

該類型為FileCacheProvider,代碼如下:

public class FileCacheProvider : OutputCacheProvider
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public override void Initialize(string name, NameValueCollection attributes)
        {
            base.Initialize(name, attributes);
            CachePath = HttpContext.Current.Server.MapPath(attributes["cachePath"]);
        }

        public override object Add(string key, object entry, DateTime utcExpiry)
        {
            Object obj = Get(key);
            if (obj != null)    //這一步很重要
            {
                return obj;
            }
            Set(key,entry,utcExpiry);
            return entry;
        }

        public override object Get(string key)
        {
            string path = ConvertKeyToPath(key);
            if (!File.Exists(path))
            {
                return null;
            }
            CacheItem item = null;
            using (FileStream file = File.OpenRead(path))
            {
                var formatter = new BinaryFormatter();
                item = (CacheItem)formatter.Deserialize(file);
            }

            if (item.ExpiryDate <= DateTime.Now.ToUniversalTime())
            {
                log.Info(item.ExpiryDate + "*" + key);
                Remove(key);
                return null;
            }
            return item.Item;
        }


        public override void Set(string key, object entry, DateTime utcExpiry)
        {
            CacheItem item = new CacheItem(entry, utcExpiry);
            string path = ConvertKeyToPath(key);
            using (FileStream file = File.OpenWrite(path))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(file, item);
            }
        }
        
        public override void Remove(string key)
        {
            string path = ConvertKeyToPath(key);
            if (File.Exists(path))
                File.Delete(path);
        }

        public string CachePath
        {
            get;
            set;
        }

        private string ConvertKeyToPath(string key)
        {
            string file = key.Replace('/', '-');
            file += ".txt";
            return Path.Combine(CachePath, file);
        }
    }

    [Serializable]
    public class CacheItem
    {
        public DateTime ExpiryDate;
        public object Item;

        public CacheItem(object entry, DateTime utcExpiry)
        {
            Item = entry;
            ExpiryDate = utcExpiry;
        }
    }      

有兩個地方需要特别說明:

在Add方法中,有一個條件判斷,必須做出這樣的處理,否則緩存機制将會緩存第一次的結果,過了有效期後緩存講失效并不再重建;

在示例程式中,我們簡單的将緩存放到了Cache目錄下,在實際的項目實踐中,考慮到緩存的頁面将是成千上萬的,是以我們必須要做目錄分級,否則尋找并讀取緩存檔案将會成為效率瓶頸,這會耗盡CPU。

3:配置檔案

我們需要在Web.config中配置緩存處理程式是自定義的FileCacheProvider,即在  <system.web>下添加節點:

<caching>
      <outputCache defaultProvider="FileCache">
        <providers>
          <add name="FileCache" type="MvcApplication2.Common.FileCacheProvider" cachePath="~/Cache" />
        </providers>
      </outputCache>
    </caching>      

4:緩存的使用

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

public class HomeController : Controller
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static string s_conn = "Data Source=192.168.0.77;Initial Catalog=luminjidb;User Id=sa;Password=sa;";
        public ActionResult Index()
        {
            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()"))
            {
                ViewBag.Message = ds.Tables[0].Rows[0]["name"].ToString();
            }
            return View();
        }

        [OutputCache(Duration = 10, VaryByParam = "none")]
        public ActionResult Index2()
        {
            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()"))
            {
                ViewBag.Message = ds.Tables[0].Rows[0]["name"].ToString();
            }
            return View();
        }
    }      

5:檢視下效果

上面的代碼,在通路了Index2後,将會在Cache檔案夾下産生緩存檔案,如下:

ASP.NET性能優化之建構自定義檔案緩存

現在,我們開始評價下有輸出緩存和無輸出緩存的性能對比,模拟100個使用者并發1000次請求如下:

ASP.NET性能優化之建構自定義檔案緩存

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

6:代碼下載下傳

FileCacheProvider的原始代碼來自于網絡,我修改了其中的BUG,全部代碼下載下傳如下:

MvcApplication20110907.rar
ASP.NET性能優化之建構自定義檔案緩存

本文基于

Creative Commons Attribution 2.5 China Mainland License

釋出,歡迎轉載,演繹或用于商業目的,但是必須保留本文的署名

http://www.cnblogs.com/luminji

(包含連結)。如您有任何疑問或者授權方面的協商,請給我留言。