在这里,我浏览了来自该站点http://www.dotnetcurry.com/showarticle.aspx?ID=263的有关 sql 缓存依赖项的文章。特别是调用了一个例程,该例程第一次缓存表数据,当基础表数据将被更改时,例程将再次从表中加载数据。我只是不明白数据库中的数据何时更改,那么缓存将如何自动为空或无效。所以在这里我粘贴那个例程,我只是不明白当数据库中发生数据更改时缓存数据是如何失效的?
只需查看例程并讨论这一点:当数据库中发生数据更改时,缓存数据如何失效
public static class MyExtensions
{
public static List<T> LinqCache<T>(this Table<T> query) where T : class
{
string tableName = query.Context.Mapping.GetTable(typeof(T)).TableName;
List<T> result = HttpContext.Current.Cache[tableName] as List<T>;
if (result == null)
{
using (SqlConnection cn = new SqlConnection(query.Context.Connection.ConnectionString))
{
cn.Open();
SqlCommand cmd = new SqlCommand(query.Context.GetCommand(query).CommandText, cn);
cmd.Notification = null;
cmd.NotificationAutoEnlist = true;
SqlCacheDependencyAdmin.EnableNotifications(query.Context.Connection.ConnectionString);
if (!SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(query.Context.Connection.ConnectionString).Contains(tableName))
{
SqlCacheDependencyAdmin.EnableTableForNotifications(query.Context.Connection.ConnectionString, tableName);
}
SqlCacheDependency dependency = new SqlCacheDependency(cmd);
cmd.ExecuteNonQuery();
result = query.ToList();
HttpContext.Current.Cache.Insert(tableName, result, dependency);
}
}
return result;
}
}
who will create this table AspNet_SqlCacheTablesForChangeNotification ?
what is the importance of this table AspNet_SqlCacheTablesForChangeNotification ?
假设我的员工表中的数据发生了变化,那么该表中会发生什么AspNet_SqlCacheTablesForChangeNotification
请讨论我的所有观点,因此我对 sql 依赖项如何工作以及如何自动缓存无效的所有疑问都将清楚?
谢谢