SqlCacheDependency使用

性能是任何 Web 應用程序的關鍵方面。必須減少 Web 服務器的處理量,以便使單個請求結果響應速度更快、服務器有能力處理更多併發請求並減少中間和後端數據系統的負荷。
使用輸出緩存以減少服務器的工作負荷,能夠實現更佳的 ASP.NET 性能。輸出緩存是一種優化方案,可以縮短 Web 服務器響應的時間。
通常,瀏覽器請求 ASP.NET 頁時,ASP.NET 將創建該頁的實例,運行該頁中的任何代碼,運行數據庫查詢(如果有),動態彙編此頁,然後將產生的輸出發送到瀏覽器。輸出緩存使 ASP.NET 能夠發送該頁的預處理副本,而不用爲每個請求完成此過程。這個區別降低了 Web 服務器的處理量,從而提高了性能並獲得更大的可伸縮性。


三步搞定 緩存依賴於 SQL Server 數據庫中數據的ASP.NET頁

1.爲 SQL Server 啓用緩存通知
aspnet_regsql.exe -S <Server> -U <Username> -P <Password> -ed -d Northwind -et -t Employees
爲 Northwind 數據庫中的 Employees 表啓用緩存通知
aspnet_regsqlcache –S 服務器名稱 –U 登陸ID –P 密碼 –d 數據庫名稱 –t 要追蹤的數據表的名稱 –et
2.爲緩存功能配置網頁
<%@ OutputCache Duration="3600" SqlDependency="Northwind:Employees" VaryByParam="none" %>
VaryByParam 屬性指示緩存時 ASP.NET 是否應考慮頁參數(如查詢字符串或發送值)。當 VaryByParam 設置爲 none 時,將不考慮任何參數;無論提供什麼附加參數,都將向所有用戶發送相同的頁。將 VaryByParam 設置爲 *(星號)表明,對於每個唯一的請求參數組合,將緩存一個唯一頁。但是,將 VaryByParam 設置爲 * 會緩存頁的許多不同版本,所以如果您知道緩存變化所依據的參數,建議您在 VaryByParam 屬性中顯式指定這些參數。


3.除了前面部分中網頁的 OutputCache 聲明外,您需要在 Web.config 文件中指定緩存詳細信息。
<connectionStrings>
   <add name="DefaultConnectionString" connectionString="Data Source=./sql2005;Initial Catalog=mysite.com;Persist Security Info=True;User ID=mysite;Password=123" providerName="System.Data.SqlClient"/>
</connectionStrings>

<system.web>
<caching>
    <sqlCacheDependency enabled = "true" pollTime = "60000" >
        <databases>
           <add name="DefaultConnectionString" connectionStringName="DefaultConnectionString" pollTime = "9000000"/>
        </databases>
    </sqlCacheDependency>
</caching>
</system.web>

兩步搞定 應用程序級緩存
將 <%@ OutputCache Duration="15" VaryByParam="none" %>(無參數形式) 或<%@ OutputCache Location="Server" Duration="60" VaryByParam="txb_LoginName" %>(參數形式進行緩存控制,當控件ID爲txb_LoginName的值發生變化時更新請求緩存頁) 形式指令添加至頁的頂部,用於配置單個頁面的緩存。在某些情況下,可能希望爲網站中的所有頁配置緩存。可能還希望建立不同的緩存規則(配置文件),並將緩存配置文件應用到各組單獨頁面。設置應用程序級別緩存使您能夠從單個配置文件更改緩存行爲,而無需編輯各個頁面的 @ OutputCache 指令。

1.在Web.config中添加system.web元素子項
<caching>
<outputCacheSettings>
    <outputCacheProfiles>
        <add name="AppCache1" enabled="true" duration="60"/>
    </outputCacheProfiles>
</outputCacheSettings>
</caching>
2.將 @ OutputCache 指令更改爲以下內容:<%@ OutputCache CacheProfile="AppCache1" VaryByParam="none" %>

System.Web.Caching.SqlCacheDependency 讓緩存更新的更及時更提高節能
在以下兩者之間建立關係:
一是在 ASP.NET 應用程序的 Cache 對象中存儲的項;
二是特定 SQL Server 數據庫表或 SQL Server 2005 查詢的結果。

無法繼承此類。在所有受支持的 SQL Server 版本 (7.0, 2000, 2005) 上監視特定的 SQL Server 數據庫表,以便在該表發生更改時,自動從 Cache 中刪除與該表關聯的項。

使用此構造函數時通常會引發兩個異常:DatabaseNotEnabledForNotificationException 和 TableNotEnabledForNotificationException。如果引發 DatabaseNotEnabledForNotificationException,可在異常處理代碼中調用 SqlCacheDependencyAdmin.EnableNotifications 方法,或使用命令行工具 Aspnet_regsql.exe 設置數據庫的通知。如果引發 TableNotEnabledForNotificationException,可調用 SqlCacheDependencyAdmin.EnableTableForNotifications 方法或使用 Aspnet_regsql.exe 設置表的通知。

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.UI;

using System.Data.SqlClient;

using System.Web.Caching;


public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        SqlCacheDependency SqlDep = null;

        if (Cache["SqlSource"]!=null)

        {

            try

            {

                SqlDep = new SqlCacheDependency("Northwind", "Categories");

            }

            catch (DatabaseNotEnabledForNotificationException)

            {

                try

                {

                    SqlCacheDependencyAdmin.EnableNotifications("Northwind");

                }

                catch (UnauthorizedAccessException)

                {

                    Response.Redirect("ErrorPage.htm");

                }

            }

            catch (TableNotEnabledForNotificationException)

            {

                try

                {

                    SqlCacheDependencyAdmin.EnableTableForNotifications("Northwind", "Categories");

                }

                catch (SqlException)

                {

                    Response.Redirect("ErrorPage.htm");

                }

            }

            finally

            {

                Cache.Insert("SqlSource", "", SqlDep);

            }

        }

    }

}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章