您的位置:首页 > 数据库

[转]使用基于轮询的SQL数据缓存依赖

2007-05-25 09:34 459 查看
使用基于轮询的SQL数据缓存依赖,主要有三个过程:

一.配置数据库

配置数据库可以使用aspnet_regsql命令,也可以使用SqlCacheDependencyAdmin类.这里我们选择用SqlCacheDependencyAdmin类..具体做法为:在Global.asax的Application_OnStart中用SqlCacheDependencyAdmin类启用相关数据库和数据表的缓存依赖.示例代码如下:

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.Caching" %>

<script runat="server">

void Application_Start(object sender, EventArgs e)

..

</script>

二.配置Web.config文件.

另外,还需要配置Web.config文件,以设置连接字符串及缓存设置:

<?xml version="1.0" encoding="utf-8"?>

<configuration>

<connectionStrings>//设置连接字符串
<add name="pubsConn" connectionString="Data Source=localhost;Initial Catalog=pubs;IntegratedSecurity=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>

<system.web>

<caching>//设置缓存依赖
<sqlCacheDependency enabled="true" pollTime="600">
<databases>
<add name="pubs" connectionStringName="pubsConn"/>
</databases>
</sqlCacheDependency>
</caching>
..
</system.web>
</configuration>

三.开始使用.

开启了数据库的缓存依赖功能,并在Web.config中配置了缓存属性后,便可开始使用该缓存依赖.

可以在页面缓存中应用该缓存依赖:

<%@ OutputCache Duration="100" SqlDependency="pubs:titles" VaryByParam="none" %>

也可以在数据库源中运用该缓存依赖:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" EnableCaching="true" SqlCacheDependency="pubs:titles" SelectCommand="select titledid,price from dbo.titles" ConnectionString="<%$ ConnectionStrings:connString %>" />

当然,也可以在应用程序数据缓存中使用该缓存信赖:

SqlCacheDependency scd=new SqlCacheDependency("pubs","titles");

Cache.Insert("TitleCount",count,scd);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: