ASP.NET 中 Cache 丟失的問題

來源:互聯網
上載者:User
  本部落格程式使用過程中,查看錯誤記錄檔,發現以下錯誤:

<ErrorItem>
  <Date>2007-12-13 18:07:26    出錯檔案:http://xianfen.net/Default.aspx</Date>
  <Message>錯誤資訊:引發類型為“System.Web.HttpUnhandledException”的異常。    內部錯誤資訊:未將對象引用設定到對象的執行個體。</Message>
</ErrorItem>
<ErrorItem>
  <Date>2007-12-13 18:07:27    出錯檔案:http://xianfen.net/Default.aspx</Date>
  <Message>錯誤資訊:引發類型為“System.Web.HttpUnhandledException”的異常。    內部錯誤資訊:未將對象引用設定到對象的執行個體。</Message>
</ErrorItem>
<ErrorItem>
  <Date>2007-12-13 21:01:47    出錯檔案:http://www.xianfen.net/Category5_1.aspx?ClassID=5&amp;page=1</Date>
  <Message>錯誤資訊:引發類型為“System.Web.HttpUnhandledException”的異常。    內部錯誤資訊:未將對象引用設定到對象的執行個體。</Message>
</ErrorItem>
<ErrorItem>
  <Date>2007-12-14 9:21:02    出錯檔案:http://www.xianfen.net/Archive200712.aspx?Year=2007&amp;Month=12</Date>
  <Message>錯誤資訊:引發類型為“System.Web.HttpUnhandledException”的異常。    內部錯誤資訊:未將對象引用設定到對象的執行個體。</Message>
</ErrorItem>

...

很多“未將對象引用設定到對象的執行個體。”,仔細審查每一行程式,沒發現使用Null 物件的情況。無奈之時,在本機上隨便調試,發現異常程式碼片段之一為:

public static string BlogTitle
{
    get
    {
        if (HttpContext.Current.Cache["BlogTitle"] == null)
        {
            HttpContext.Current.Cache["BlogTitle"] = ConfigurationManager.AppSettings["BlogTitle"];
        }

        return HttpContext.Current.Cache["BlogTitle"].ToString();  //在此拋出異常"未將對象引用設定到對象執行個體"
    }
}

為了提高效能,本部落格系統多處使用緩衝,但每次取快取資料時都檢查是否為空白啊?只好 Google,發現有和我遇到一樣問題的,原來緩衝 Cache 在記憶體不足時會移除,看來虛擬空間的記憶體緊張到極點了,剛設定的緩衝就被移除了!
將Cache用Application代替:

public static string BlogTitle
{
    get
    {
        if (HttpContext.Current.Application["BlogTitle"] == null)
        {
            HttpContext.Current.Application["BlogTitle"] = ConfigurationManager.AppSettings["BlogTitle"];
        }

        return HttpContext.Current.Application["BlogTitle"].ToString();
    }
}

問題解決!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.