
[英]Why isn't the Cache invalidated after table update using the SqlCacheDependency?
[英]SqlCacheDependency remove callback fires directly after Cache.Add
我对SqlCacheDependency有问题,无法解决问题。 当我使用通知查询时,只要将某些内容添加到缓存中,CacheItemRemovedCallback就会触发(当我使用databaseEntryName和tableName时起作用,但这对我来说是钝的)。 我已经检查了http://msdn.microsoft.com/en-us/library/ms181122.aspx大约20次,但仍然找不到我做错的事情。
我正在使用的代码:
string connString = MsSqlUtil.GetConnectionString();
System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connString);
System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connString, "Product");
SqlDependency.Start(connString);
Product product = ProductProvider.Get(10);
using (SqlConnection connection = new SqlConnection(connString))
{
SqlCommand cmdProduct = new SqlCommand(@"
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
SET NUMERIC_ROUNDABORT OFF
SET ARITHABORT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SELECT dbo.Product.ProductId, dbo.Product.Name FROM dbo.Product WHERE dbo.Product.ProductId = 10", connection);
SqlCacheDependency myProductDependency = new SqlCacheDependency(cmdProduct);
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
using (SqlDataReader reader = cmdProduct.ExecuteReader())
{
while (reader.Read())
{
}
}
HttpContext.Current.Cache.Add("Product:10", product, myProductDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, OnRemove);
}
//Callback
public static void OnRemove(string key, object cacheItem, System.Web.Caching.CacheItemRemovedReason reason)
{
//this fires directly and the reason is always DependencyChanged however nothing has changed in the database, weird!
}
我知道查询肯定有些麻烦,因为http://msdn.microsoft.com/zh-cn/library/ms181122.aspx告诉我“如果这些选项或隔离级别设置不正确,则会触发通知立即执行SELECT语句。” 但是我不知道出什么问题了。 ProductId列的类型为int,名称为nvarchar(50)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.