SqlCacheDependency的使用,高速缓存

来源:互联网 发布:linux命令模式启动gui 编辑:程序博客网 时间:2024/05/03 14:08

性能是任何 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);

            }

        }

    }

}

原创粉丝点击