.Net 的 HttpRuntime.Cache vs Page.Application 效能測試 .Net 的 HttpRuntime.Cache vs Page.Application 效能測試
  .NET       ez      2012-06-04

測試程式碼如下:

using System;
using System.Diagnostics;

namespace WebApplication
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string temp = string.Empty;
            Stopwatch sw = new Stopwatch();

            //HttpRuntime.Cache 測試
            sw.Reset();
            sw.Start();
            for (int i = 0; i < 1000000; i++) SetCache("cache" + i.ToString(), i.ToString(), 60);
            sw.Stop();
            Response.Write("HttpRuntime.Cache 寫入時間:" + sw.ElapsedMilliseconds.ToString() + " /ms <br>");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 1000000; i++) temp = GetCache("cache" + i.ToString());
            sw.Stop();
            Response.Write("HttpRuntime.Cache 讀取時間:" + sw.ElapsedMilliseconds.ToString() + " /ms <br><br>");

            //Page.Application 測試
            sw.Reset();
            sw.Start();
            for (int i = 0; i < 1000000; i++) Application["cache" + i.ToString()] = i.ToString();
            sw.Stop();
            Response.Write("Page.Application 寫入時間:" + sw.ElapsedMilliseconds.ToString() + " /ms <br>");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 1000000; i++) temp = Application["cache" + i.ToString()].ToString();
            sw.Stop();
            Response.Write("Page.Application 讀取時間:" + sw.ElapsedMilliseconds.ToString() + " /ms <br>");
        }

        /// <summary>
        /// 取的快取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected string GetCache(string key)
        {
            try
            {
                object _cache = System.Web.HttpRuntime.Cache.Get(key);
                if (_cache != null)
                    return (string)_cache;
                else
                    return "";
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 設定快取
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        protected bool SetCache(string key, string value,int timeout)
        {
            try
            {
                System.Web.HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, timeout), System.Web.Caching.CacheItemPriority.High, null);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

第一次 Insert 值到 HttpRuntime.Cache 及 Page.Application 的效能比較。

第一次後 Insert 值到 HttpRuntime.Cache 及 Page.Application 的效能比較。

從上圖可以發現,第一次 Insert 並無太大差異,但是第二次之後 Application的寫入速度優於 Cache,所以如果需要大量寫入相同變數,個人建議採用 Page.Application ,另外如果讀取機率比較常,個人建議採用 HttpRuntime.Cache 較為快速。


標籤:   .NET

我要留言