目錄
OutputCache概念學習
NoStore
bool值,用于決定是否阻止敏感信息的二級存儲,默認值為 false。
該屬性的測試代碼,我沒寫出來,不知道如何寫,各位知道的大俠請說下哈!
SqlDependency
標識一組數(shù)據(jù)庫/表名稱對的字符串值,頁或控件的輸出緩存依賴于這些名稱對。請注意,SqlCacheDependency 類監(jiān)視輸出緩存所依賴的數(shù)據(jù)庫中的表,因此當更新表中的項時,使用基于表的輪詢時將從緩存中移除這些項。如果以值 CommandNotification: 使用通知(在 Microsoft SQL Server 2005 中),則最終會使用 SqlDependency 類向 SQL Server 2005 服務(wù)器注冊查詢通知。
SqlCacheDependency
數(shù)據(jù)庫緩存依賴主要解決的是當數(shù)據(jù)庫的內(nèi)容發(fā)生改變時,如何及時通知緩存,并更新緩存中的數(shù)據(jù)的問題。本節(jié)就介紹如何使用SQL Server 2005和.NET 2.0實現(xiàn)數(shù)據(jù)庫的緩存依賴。
推薦一篇:http://www.cnblogs.com/systemxgl/archive/2009/09/03/1559828.html
使用SqlDependency需要實現(xiàn)以下步驟:
1.注冊數(shù)據(jù)庫連接池
aspnet_regsql:http://msdn.microsoft.com/zh-cn/library/ms229862
項 |
說明 |
-? |
在命令窗口中顯示 Aspnet_regsql.exe 幫助文本。 |
-W |
在向?qū)J较逻\行該工具。如果未指定任何命令行參數(shù),那么這是默認設(shè)置。 |
-C <連接字符串> |
指定要連接到正在運行 SQL Server 并且將安裝或者已經(jīng)安裝數(shù)據(jù)庫的計算機的連接字符串。如果您僅指定服務(wù)器 (-S) 和登錄(-U 和 -P,或 -E)信息,則此選項不是必需的。 |
-S <服務(wù)器> |
指定正在運行 SQL Server 并且將安裝或者已安裝數(shù)據(jù)庫的計算機的名稱。 |
-U <登錄 ID> |
要用來登錄的 SQL Server 用戶 ID。此選項還要求使用密碼 (-P) 選項。如果要使用 Windows 憑據(jù) (-E) 進行身份驗證,則此選項不是必需的。 |
-P <密碼> |
要用來登錄的 SQL Server 密碼。此選項還要求使用用戶 ID (-U) 選項。如果要使用 Windows 憑據(jù) (-E) 進行身份驗證,則此選項不是必需的。 |
-E |
使用當前登錄用戶的 Windows 憑據(jù)進行身份驗證。 |
-sqlexportonly <文件名> |
生成可用于添加或移除指定功能的 SQL 腳本文件。不執(zhí)行指定的操作。 |
示意:
aspnet_regsql.exe -S localhost -U sa -P abcd1234 -ed -d TestStaff -et -t dbo.Staff
-? 顯示該工具的幫助功能;
-S 后接的參數(shù)為數(shù)據(jù)庫服務(wù)器的名稱或者IP地址;
-U 后接的參數(shù)為數(shù)據(jù)庫的登陸用戶名;
-P 后接的參數(shù)為數(shù)據(jù)庫的登陸密碼;
-E 當使用windows集成驗證時,使用該功能;
-d 后接參數(shù)為對哪一個數(shù)據(jù)庫采用SqlCacheDependency功能;
-t 后接參數(shù)為對哪一個表采用SqlCacheDependency功能;
-ed 允許對數(shù)據(jù)庫使用SqlCacheDependency功能;
-dd 禁止對數(shù)據(jù)庫采用SqlCacheDependency功能;
-et 允許對數(shù)據(jù)表采用SqlCacheDependency功能;
-dt 禁止對數(shù)據(jù)表采用SqlCacheDependency功能;
-lt 列出當前數(shù)據(jù)庫中有哪些表已經(jīng)采用sqlcachedependency功能。
注意大小寫

注冊完成后,我們會看到數(shù)據(jù)庫中會多出一張注冊存儲緩存的表:

2.配置項目WebConfig代碼
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="TestSqlCacheDependency_connectionStrings" connectionString="SERVER=localhost;UID=SA;PWD=abcd1234;DATABASE=TestStaff;"/>
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled="true">
<databases>
<!--pollTime 設(shè)置 sqlCacheDependency 輪詢數(shù)據(jù)庫表以查看是否發(fā)生更改的頻率-->
<add connectionStringName="TestSqlCacheDependency_connectionStrings" name="TestSqlCacheDependency" pollTime="50000"/>
</databases>
</sqlCacheDependency>
</caching>
<compilation debug="true"/>
</system.web>
</configuration>
3.配置調(diào)用程序
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ OutputCache SqlDependency="TestSqlCacheDependency:Staff" Duration="100" VaryByParam="id" %>
<html xmlns="http://www./1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SqlCacheDependency="TestSqlCacheDependency" SelectCommand="SELECT TOP 10 [Password],[Gender],[Status] FROM [dbo].[Staff]"
ConnectionString="<%$ConnectionStrings:TestSqlCacheDependency_connectionStrings %>">
</asp:SqlDataSource>
<%=DateTime.Now %>
</form>
</body>
</html>
4.測試

利用sql server profiler監(jiān)測,多次刷新頁面,也不會有相關(guān)的SQL查詢請求,如下:

關(guān)于OutputCache五個章節(jié)就寫完了,如有問題,歡迎指正。
作者:釋迦苦僧 出處:http://www.cnblogs.com/woxpp/p/3986185.html 本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接。