gpt4 book ai didi

c# - 简单的 SqlCacheDependency

转载 作者:太空狗 更新时间:2023-10-29 23:35:57 26 4
gpt4 key购买 nike

我读过的几乎所有教程似乎都错误地设置了 SqlCacheDependency。我相信他们通常会混淆过时的轮询方法和查询通知方法。

这里有两个例子:


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

我简化了只是这样做...

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);
}

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

这似乎不需要定义查询中涉及哪些表并在每个表上进行复杂的触发器就可以工作。它只是工作。

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

为了进行测试,我让它运行了 1000 次涉及名为“Settings”的表的查询。然后我更新表中的值并重复查询。

我观察 Profiler 是否有任何涉及单词“Settings”的查询,我看到查询只执行了 1 次(设置缓存),然后更新语句发生,然后查询又重新执行了一次(缓存失效,再次运行查询)

我担心在 2-3 小时的努力中我遗漏了一些东西,这真的很简单吗?

我真的可以只输入我想要的任何查询,它就会正常工作吗?我正在寻找我正在做危险/非标准的事情或我遗漏的任何小字的任何指示

最佳答案

var dependency = new SqlCacheDependency(cmd);当您编写这样的查询时,您会自动在其中定义表名。您的连接已经具有数据库名称。这是非明确的方式来做同样的事情。

捕获异常并知道哪里出了问题的明确方法是这样的。

// 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.";
}

关于c# - 简单的 SqlCacheDependency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com