zoukankan      html  css  js  c++  java
  • SqlCacheDependency使用轮流检测技术(轮询)使缓存无效

    系列目录:

    SqlDependency缓存用法

    Asp.net使用SqlDependency

    SqlCacheDependency使用命令通知使缓存无效

    CacheDependency用法

    AggregateCacheDependency 用法

    SqlCacheDependency使用轮流检测技术(轮询)使缓存无效 

    -------------------------------------------------------------------------------------------------

     在Sql Server 7及后继版本中都可以使用轮流检测技术(轮询)使缓存无效,但只有Sql Server 7和Sql server 2000支持这种技术。如查是Sql Server2005及后继版本可以使用查询(命令)通知技术来使缓存无效。

    用法

    1、在配置轮流检测技术时,首先要配置每个要进行检测的Sql Server数据库,然后配置要进行检测的每个表。

      1)配置数据库

      格式:

    aspnet_regsql  –S 服务器名 –E –D 数据库名  –et

        例:配置本机的NhibernateSample数据库

    aspnet_regsql  –S . –E –d "NHibernateSample"   –et

        说明:执行此命令后,会在指定的数据库中会新建一个“AspNet_SqlCacheTablesForChangeNotification”表。

        网上看到另外一个格式,如下。

    “aspnet_regsql  –S 服务器名 –U 登陆名 ID –P 密码 –d 数据库名  –ed”

      2)配置表
        格式:

    aspnet_regsql  –S 服务器名 –E –d 数据库名 -t 表名  –et

         例:配置本机NhibernateSample数据库的Customer表

    aspnet_regsql  –S "." –E –d "NHibernateSample" -"Customer"  –et

         说明:执行此命令后,会在表“AspNet_SqlCacheTablesForChangeNotification”中添加一行,并在Customer表上建立有关Insert、Update、Delete的触发器。

         网上看到另外一种格式,如下。

    aspnet_regsql –S 服务器名  –U 登陆名 ID –P 密码 –d 数据库名 –t 追踪的数据表 –et

      关闭此表的通知为:

    aspnet_regsql  -S . --d "NHibernateSample" -t "Customer"  -dt

         如果觉得手工建立麻烦。也可在程序中使用代码来替代。代码如下:

                    //要检测的数据库
                    SqlCacheDependencyAdmin.EnableNotifications(
                        ConfigurationManager.ConnectionStrings[
    "NHibernateSampleDb"].ConnectionString);
                    
    //初检测数据库中要检测的表
                    SqlCacheDependencyAdmin.EnableTableForNotifications(
                        ConfigurationManager.ConnectionStrings[
    "NHibernateSampleDb"].ConnectionString, "Customer");

     2、编写程序。

        下边程序一个Asp.net程序。所以需要配置web.config.要配置的地方如下。 注意下边加粗的部分。

    <connectionStrings>
        
    <add name="NHibernateSampleDb" providerName="System.Data.SqlClient" 
             connectionString
    ="Data Source=.; Initial Catalog=NHibernateSample; 
    Persist Security Info=True;User ID=sa;Password=123"
    />

      
    </connectionStrings>
      
    <system.web>
        
    <caching>
          
    <sqlCacheDependency enabled="true">
            
    <databases>
              
    <add name="CacheDependency_NHibernateSampleDb" connectionStringName="NHibernateSampleDb" pollTime="5000"/>
            
    </databases>
          
    </sqlCacheDependency>
        
    </caching>
     
    </system.web>

        页面上添加OutputCache,用于缓存页面。并依赖于SqlCacheDependency。页面源码如下。

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CacheDependencyPage.aspx.cs"
        Inherits
    ="SqlDependencyInAspNet.CacheDependencyPage" %>

        
    <% @ OutputCache Duration ="9999" VaryByParam ="None" SqlDependency ="CacheDependency_NHibernateSampleDb:Customer" %> 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        
    <title></title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
        
    </div>
        
    </form>
    </body>
    </html>

     编写代码如下,注意加粗部分。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using System.Configuration;
    using System.Data;
    using System.Web.Caching;

    namespace SqlDependencyInAspNet
    {
        
    public partial class AggregateCacheDependencyUse : System.Web.UI.Page
        {
            
    protected void Page_Load(object sender, EventArgs e)
            {
                
    if (!IsPostBack)
                {
                    GetData();
                }
            }

            
    private void GetData()
            {
                
    string cacheData = "内容";
                
    if (Cache["data"== null)
                {
                    
    //要检测的数据库
                    SqlCacheDependencyAdmin.EnableNotifications(
                        ConfigurationManager.ConnectionStrings[
    "NHibernateSampleDb"
    ].ConnectionString);
                    
    //初检测数据库中要检测的表

                    SqlCacheDependencyAdmin.EnableTableForNotifications(
                        ConfigurationManager.ConnectionStrings[
    "NHibernateSampleDb"].ConnectionString, "Customer");
                    SqlCacheDependency scd 
    = new SqlCacheDependency("CacheDependency_NHibernateSampleDb""Customer
    ");
                    Cache.Insert(
    "data"
    , cacheData, scd);
                    
    //此处可以加入自己的其它方法,如重新从数据库取得资料
                    Response.Write("取用新的内容");
                }
                
    else
                {
                    cacheData 
    = (string)Cache["data"];
                    Response.Write(
    "调用Cache内容");
                }
                Response.Write(cacheData);
            }

        }
    }

     最后测试。启动程序后刷新,程序将会中缓存中取数据。修改数据库中表Customer,然后刷新,这次将重新取数据,OutputCache也将随之实更新。

  • 相关阅读:
    [奇葩问题] Error Domain=NSURLErrorDomain Code=1003
    [linux] vim在源代码中自动添加作者信息(转载)
    [shell] 循环判断输入值
    [redis] linux下主从篇(2)
    [shell] sed学习
    [linux] 查看网卡UUID
    [笔记] centos6.6编译安装httpd2.4.10
    [笔记] postgresql 流复制(streaming replication)
    ORA28000: the account is locked 查哪个具体ip地址造成
    网易客户端授权密码,errormsg='authentication failed (method LOGIN)' exitcode=EX_NOPERM
  • 原文地址:https://www.cnblogs.com/scottckt/p/1966567.html
Copyright © 2011-2022 走看看