上篇文章介紹的是通知模式的緩存機制,這裏介紹的是數據庫輪循模式處理,這種模式對SQL2005如下的支持仍是比較好的html
引擎源碼以下:web
/// <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); } } }
一樣的,代碼也要配置一下.sql
<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 添加一行數據..每更新,刪除,插入一次都會增長(更新)一數據的.ide
怎麼開啓AspNet_SqlCacheTablesForChangeNotification 這個..具體能夠參考 post
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性能
注:這種模式比較耗數據庫性能哦!!!!強烈建議用通知模式吧,具體參考文章:this