首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单SqlCacheDependency

简单SqlCacheDependency
EN

Stack Overflow用户
提问于 2019-06-27 02:37:13
回答 2查看 417关注 0票数 4

几乎我读过的每一个教程似乎都错误地设置了SqlCacheDependency.。我相信他们通常会混淆过时的投票方法和查询通知方法。

以下是许多例子中的两个:

根据我的测试,如果您正在使用代理(MSSQL 2015+),则不需要进行任何.config更改,也不需要进行任何SqlCacheDependencyAdmin调用(不需要定义表等)。

我简化了就这么做..。

代码语言:javascript
复制
SqlDependency.Start(connString)
...
queryString = "SELECT ...";
cacheName = "SqlCache" + queryString.GetHashCode();
...
using (var connection = new SqlConnection(connString))
{
    connection.Open();
    var cmd = new SqlCommand(queryString, connection)
    {
        Notification = null, 
        NotificationAutoEnlist = true
    };

    var dependency = new SqlCacheDependency(cmd);

    SqlDataReader reader = cmd.ExecuteReader();
    try
    {
        while (reader.Read())
        {
            // Set the result you want to cache
            data = ...
        }
    }
    finally
    {
        reader.Close();
    }

    HostingEnvironment.Cache.Insert(cacheName, data, dependency);
}

(检查缓存是否为空的代码不包括在内,因为这只是设置。我只想显示缓存的设置)

这似乎不需要定义查询中涉及的表并在每个表上生成复杂的触发器。只是起作用了。

更令我惊讶的是,查询规则有通知:

  • 创建通知查询 (找不到比2008年更新的文档)似乎不适用。我的目的是在我的SQL中做一个顶部,但它仍然有效。

对于测试,我让它运行一个查询1000次,涉及一个名为"Settings“的表。然后更新表中的值并重复查询。

我查看Profiler对于任何涉及单词"Settings“的查询,我看到查询只执行1次(设置缓存),然后发生update语句,然后再次执行查询(缓存无效,查询再次运行)。

我担心的是,在我2-3个小时的挣扎中,我错过了一些东西,这真的很简单吗?

我真的能把我想要的任何查询都放进去吗?我正在寻找我正在做一些危险的/不标准的事情的任何指点,或者我遗漏的任何小字。

EN

回答 2

Stack Overflow用户

发布于 2019-07-03 15:49:36

var dependency =新的SqlCacheDependency(cmd);当像这样编写查询时,在it.Your连接中自动定义表名已经有db名称。这是不明确的方式来做同样的。

捕捉异常和知道出了什么问题的明确方法是这样的。

代码语言:javascript
复制
// Declare the SqlCacheDependency instance, SqlDep. 
        SqlCacheDependency SqlDep = null; 

        // Check the Cache for the SqlSource key. 
        // If it isn't there, create it with a dependency 
        // on a SQL Server table using the SqlCacheDependency class. 
        if (Cache["SqlSource"] == null) { 

            // Because of possible exceptions thrown when this 
            // code runs, use Try...Catch...Finally syntax. 
            try { 
                // Instantiate SqlDep using the SqlCacheDependency constructor. 
                SqlDep = new SqlCacheDependency("Northwind", "Categories"); 
            } 

            // Handle the DatabaseNotEnabledForNotificationException with 
            // a call to the SqlCacheDependencyAdmin.EnableNotifications method. 
            catch (DatabaseNotEnabledForNotificationException exDBDis) { 
                try { 
                    SqlCacheDependencyAdmin.EnableNotifications("Northwind"); 
                } 

                // If the database does not have permissions set for creating tables, 
                // the UnauthorizedAccessException is thrown. Handle it by redirecting 
                // to an error page. 
                catch (UnauthorizedAccessException exPerm) { 
                    Response.Redirect(".\\ErrorPage.htm"); 
                } 
            } 

            // Handle the TableNotEnabledForNotificationException with 
            // a call to the SqlCacheDependencyAdmin.EnableTableForNotifications method. 
            catch (TableNotEnabledForNotificationException exTabDis) { 
                try { 
                    SqlCacheDependencyAdmin.EnableTableForNotifications("Northwind", "Categories"); 
                } 

                // If a SqlException is thrown, redirect to an error page. 
                catch (SqlException exc) { 
                    Response.Redirect(".\\ErrorPage.htm"); 
                } 
            } 

            // If all the other code is successful, add MySource to the Cache 
            // with a dependency on SqlDep. If the Categories table changes, 
            // MySource will be removed from the Cache. Then generate a message 
            // that the data is newly created and added to the cache. 
            finally { 
                Cache.Insert("SqlSource", Source1, SqlDep); 
                CacheMsg.Text = "The data object was created explicitly."; 

            } 
        } 

        else { 
            CacheMsg.Text = "The data was retrieved from the Cache."; 
        }
票数 1
EN

Stack Overflow用户

发布于 2019-07-04 22:05:15

正如https://learn.microsoft.com/en-us/dotnet/api/system.web.caching.sqlcachedependency?view=netframework-4.8中记录的那样,“使用带有Server 2005查询通知的SqlCacheDependency对象不需要任何显式配置。”

因此,CMD中有显式的表名,ADO.net为您发出正确的Service配置命令。更新表时,Server会发布一条Service消息,表示表已经更新。当ADO.net验证CMD时,它会检查代理中的显式表以进行更新。

这就是为什么SQlCacheDependency关联的CMD必须使用显式表的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56783162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档