您的位置:首页 > 编程语言 > ASP

Asp.net2.0中的缓存机制

2006-02-08 13:57 281 查看
Asp.net2.0中,缓存机制在原来1.1中的“页面缓存”、“用户控件缓存”的基础上又多了一种:数据源缓存。
新增的DataSource控件(数据源控件)将可以很好的解决了“缓存失效”问题。

这里不谈文件型数据源,主要看看 Sql 数据源。

Asp.net2.0和sqlserver配合进行缓存有2种方式:轮询查询通知
轮询-----------主要是针对SQL (7.0, 2000, 2005);
查询通知-----只针对Sql2005。

1、轮询方式编程步骤:

使用 aspnet_regsql.exe 工具为数据库启用通知。 注:aspnet_regsql工具在后台使用SqlCacheDependencyAdmin类的方法,所以可以直接从ASP.NET页面中使用此类的方法。
aspnet_regsql.exe -S . -E -d databasename -ed
(-S 数据库实例名,这里的“点”代表本机实例; -E 集成身份验证; -d 数据库名称)
此步骤只需为每个数据库执行一次。

使用 aspnet_regsql.exe 工具为希望依赖的表启用通知。
aspnet_regsql -S . -E -d databasename -t tablename -et
(-t 表名)

在应用程序的配置中注册通知。
<connectionStrings>
 <add name="LocalSqlServer" connectionString="Data Source=.;Initial Catalog=Morning2;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
。。。。
<system.web>
<caching>
<sqlCacheDependency enabled="true" pollTime="1000" >
<databases>
<add name="databasename" connectionStringName="LocalSqlServer" /> //这里的LocalSqlServer必须在前面声明
</databases>
</sqlCacheDependency>
</caching>
</system.web>
轮询时间指定应用程序每隔多长时间检查数据是否已更改。

然后就可以在 OutputCache 指令上使用一个 SQL 依赖项:
<%@ OutputCache Duration="999999" SqlDependency="databasename:tablename" VaryByParam="none" %>
或者依赖项也可以直接在数据源控件上指定:
<asp:SqlDataSource EnableCaching="true" CacheDuration="Infinite" SqlCacheDependency="databasename:tablename" ... />

2、查询通知方式编程步骤:

与基于轮询的失效不同,无需在应用程序的配置中注册任何 <sqlCacheDependency>。而且,无需使用 aspnet_regsql.exe 工具进行任何特殊配置。

基于通知的依赖项是使用字符串 CommandNotification 在 OutputCache 指令上 配置的。此值告知 ASP.NET 应为页或数据源控件创建基于通知的 依赖项。
在页上:
<%@ OutputCache Duration="999999" SqlDependency="CommandNotification" VaryByParam="none" %>
在数据源控件上:
<asp:SqlDataSource EnableCaching="true" SqlCacheDependency="CommandNotification" CacheDuration="Infinite" ... />

在首次执行某 SQL 查询之前,必须在应用程序某处调用 System.Data.SqlClient.SqlDependency.Start() 方法。此方法应放在 global.asax 文件的 Application_Start() 事件中。

=============================
使用 Sql Server 2005 查询通知的常见问题是:

查询的 SELECT 语句中必须显式包含列名。使用“SELECT *”将使查询不会在 Sql Server 2005 查询通知中注册。

查询中的表名必须包括所有者名称。例如,如果对 pubs 数据库中的 authors 表发出一个查询,则该查询必须用“dbo.authors” 来引用表。

运行查询的安全标识必须具有在 Sql Server 2005 中注册查询通知的权限。此权限可以使用下面的 T-SQL 命令来授予:GRANT SUBSCRIBE QUERY NOTIFICATIONS TO username。

运行查询的安全标识还必须具有从 Sql Server 2005 发送查询通知的权限。此权限可以使用下面的 T-SQL 命令来授予:GRANT SEND ON SERVICE::SqlQueryNotificationService TO username。

注意: 对支持查询通知的查询语法有许多限制。有关具体约束的列表,请参见 Sql Server 2005 Books Online(《Sql Server 2005 联机丛书》)中的主题“创建用于通知的查询”。另外,如果出现查询未被缓存,而是在每个页请求上都执行的情况,则可能是查询没有遵守 Sql Server 2005 所要求的约束,或是 Sql Server 2005 在尝试设置该查询的通知时产生错误。目前,当尝试在 ASP.NET 中设置缓存依赖项时,这些条件中的任何一个都会导致没有任何提示的错误,其最终结果是缓存依赖项始终失效,并因此使得所有相关联的查询始终会在每个页请求上执行。

附:sql2005联机文档(部分)--全文见上面链接ms-help://MS.VSCC.v80/MS.VSIPCC.v80/MS.SQLSVR.v9.en/udb9/html/3ba5271c-7efa-4a73-a2c4-b4730df3fdc3.htm

Supported SELECT Statements
Query notifications are supported for SELECT statements that meet the following requirements:

The projected columns in the SELECT statement must be explicitly stated, and table names must be qualified with two-part names. Notice that this means that all tables referenced in the statement must be in the same database.

The statement may not use the asterisk (*) or table_name.* syntax to specify columns.

The statement may not use unnamed columns or duplicate column names.

The statement must reference a base table.

The projected columns in the SELECT statement may not contain aggregate expressions unless the statement uses a GROUP BY expression. When a GROUP BY expression is provided, the select list may contain the aggregate functions COUNT_BIG() or SUM(). However, SUM() may not be specified for a nullable column. The statement may not specify HAVING, CUBE, or ROLLUP.

A projected column in the SELECT statement that is used as a simple expression must not appear more than once.

The statement must not include PIVOT or UNPIVOT operators.

The statement must not include the INTERSECT or EXCEPT operators.

The statement must not reference a view.

The statement must not contain any of the following: DISTINCT, COMPUTE or COMPUTE BY, or INTO.

The statement must not reference server global variables (@@variable_name).

The statement must not reference derived tables, temporary tables, or table variables.

The statement must not reference tables or views from other databases or servers.

The statement must not contain subqueries, outer joins, or self-joins.

The statement must not reference the large object types: text, ntext, and image.

The statement must not use the CONTAINS or FREETEXT full-text predicates.

The statement must not use rowset functions, including OPENROWSET and OPENQUERY.

The statement must not use any of the following aggregate functions: AVG, COUNT(*), MAX, MIN, STDEV, STDEVP, VAR, or VARP.

The statement must not use any nondeterministic functions, including ranking and windowing functions.

The statement must not contain user-defined aggregates.

The statement must not reference system tables or views, including catalog views and dynamic management views.

The statement must not include FOR BROWSE information.

The statement must not reference a queue.

The statement must not contain conditional statements that cannot change and cannot return results (for example, WHERE 1=0).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: