DMSFrame 之SqlCacheDependency(二)
上篇文章介绍的是通知模式的缓存机制,这里介绍的是数据库轮循模式处理,这种模式对SQL2005以下的支持还是比较好的
引擎源码如下:
/// <summary> /// 轮循模式 /// 数据库缓存通知模式 /// 1.SELECT DATABASEPROPERTYEX('DATABASENAME','IsBrokerEnabled') 1 表示启用,0表示未启用 /// 2.启用IsBrokerEnabled /// ALTER DATABASE [DATABASENAME] SET NEW_BROKER WITH ROLLBACK IMMEDIATE; /// ALTER DATABASE [DATABASENAME] SET ENABLE_BROKER;/ALTER DATABASE [DATABASENAME] SET DISABLE_BROKER; /// 3.web.config 添加配置信息 /// connectionStrings /// 4.设置aspnet_regsql.exe的信息,aspnet_regsql –S 服务器名 –U 登陆名 ID –P 密码 –d 数据库名 –ed /// </summary> [Obsolete("适用于数据库2005以下,2005以上请使用DMSLinqSqlWebCacheNotifyProvider")] public class DMSLinqSqlWebCacheProvider : IDMSLinqCacheProvider { System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache; private static object syncObj = new object(); public DMSLinqSqlWebCacheProvider() { lock (syncObj) { System.Web.HttpContext context = System.Web.HttpContext.Current; if (context != null) webCache = context.Cache; else webCache = System.Web.HttpRuntime.Cache; } } public string GetDependencyKey(System.Data.IDbCommand command, string[] tableNames) { string dependencyKey = command.CommandText; foreach (System.Data.IDbDataParameter item in command.Parameters) { dependencyKey += string.Format("-{0}-{1}", item.ParameterName, item.Value); } #if DEBUG System.Diagnostics.Debug.WriteLine(string.Format("{0},use dependency key successfully", dependencyKey)); #endif return dependencyKey; } public object GetCache(string dependencyKey) { object resultValue = webCache[dependencyKey]; #if DEBUG System.Diagnostics.Debug.WriteLine(string.Format("this cache is empty?:{0}", resultValue == null ? "true" : "false")); #endif return resultValue; } public System.Web.Caching.CacheDependency GetCacheDependency(string connectionString, System.Data.IDbCommand command, string[] tableNames, ref string dependencyKey) { CacheDependency dependency = null; try { SqlCacheDependencyAdmin.EnableNotifications(connectionString); if (!SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connectionString).Contains(tableNames[0])) { SqlCacheDependencyAdmin.EnableTableForNotifications(connectionString, tableNames[0]); } if (tableNames.Length == 1) { dependency = new SqlCacheDependency("DefaultValue", tableNames[0]); } else { AggregateCacheDependency dependency0 = new AggregateCacheDependency(); foreach (var item in tableNames) { dependency0.Add(new SqlCacheDependency("DefaultValue", item)); } dependency = dependency0; } } catch (Exception ex) { DMSFrame.Loggers.LoggerManager.Logger.Log(ex, ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), DMSFrame.Loggers.ErrorLevel.Fatal); } #if DEBUG System.Diagnostics.Debug.WriteLine(string.Format("Get the sqlcachedependency successfully.{0}", dependency == null ? "false" : "true")); #endif return dependency; } public void SetCache(System.Web.Caching.CacheDependency dependency, string dependencyKey, object Value) { if (dependency != null) { #if DEBUG System.Diagnostics.Debug.WriteLine(string.Format("Add cache is successfully,{0}", dependencyKey)); #endif webCache.Insert(dependencyKey, Value, dependency); } } }
同样的,代码也要配置一下.
<configSections> <section name="DMSLinqCacheProvider" type="DMSFrame.Cache.DMSLinqCacheProvider,DMSFrame"/> </configSections> <DMSLinqCacheProvider> <add key="provider" providerName="MsSql" value="DMSFrame.Cache.DMSLinqSqlWebCacheProvider,DMSFrame"/> </DMSLinqCacheProvider>
注意,此处理配置的数据库连接name必须是 DefaultValue,至于为什么是这个,可以看下引擎源码..呵呵,这里就不解释了.....
<connectionStrings> <add name="DefaultValue" providerName="System.Data.SqlClient" connectionString="Integrated Security=False;server=127.0.0.1;database=DATABASE;User ID=sa;Password=sa;Connect Timeout=30;"/> </connectionStrings>
增加支持 sqlCacheDependency 的配置,轮循时间为1000毫秒
<system.web> <caching> <sqlCacheDependency enabled="true" pollTime="1000"> <databases> <add name="DefaultValue" connectionStringName="DefaultValue" pollTime="500" /> </databases> </sqlCacheDependency> </caching> </system.web>
注: databases也可以重写轮循时间的哦...呼呼,不要以为这样就可以了哦..
还要再检查我们数据库是否支持这个模式的方式呢.这种模式查询SQL会自动在表 AspNet_SqlCacheTablesForChangeNotification 添加一行数据..每更新,删除,插入一次都会增加(更新)一数据的.
怎么开启AspNet_SqlCacheTablesForChangeNotification 这个..具体可以参考
1.为 SQL Server 启用缓存通知
aspnet_regsql.exe -S <Server> -U <Username> -P <Password> -ed -d Northwind -et -t Employees
为 Northwind 数据库中的 Employees 表启用缓存通知
aspnet_regsqlcache –S 服务器名称 –U 登陆ID –P 密码 –d 数据库名称 –t 要追踪的数据表的名称 –et
注:这种模式比较耗数据库性能哦!!!!强烈建议用通知模式吧,具体参考文章:
DMSFrame 之SqlCacheDependency(一)
千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs