1.SqlCacheDependency都是我們常用的一種Cache寫法了。對後面的SQL 2005算是比較成熟的一種快取模式了,這裡介紹一下DMSFrame的SqlCacheDependency是怎麼使用的
DMSFrame已內建MSSQL的通知模式(MSSQL2005以上,含2005)和輪循模式(MSSQL2005以下)的快取寫法。
檢視資料庫是否支援通知模式
SELECT DATABASEPROPERTYEX('DATABASENAME','IsBrokerEnabled') 1 表示啟用,0表示未啟用
啟用IsBrokerEnabled
ALTER DATABASE [DATABASENAME] SET NEW_BROKER WITH ROLLBACK IMMEDIATE; ALTER DATABASE [DATABASENAME] SET ENABLE_BROKER;/ALTER DATABASE [DATABASENAME] SET DISABLE_BROKER;
設定許可權資訊
GRANT SUBSCRIBE QUERY NOTIFICATIONS TO [當前資料庫登入使用者名稱,如果是sa可以跳過]
修改登入賬號資訊,這裡比較關鍵,如果未設定有可能通知是沒有許可權的。具體也可以看看SQL日誌資訊
exec sp_changedbowner @loginame = '[當前資料庫登入使用者名稱,如果是sa,則使用sa賬號]'
首先新增web.config的配置資訊
<configSections> <section name="DMSLinqCacheProvider" type="DMSFrame.Cache.DMSLinqCacheProvider,DMSFrame"/> </configSections> <DMSLinqCacheProvider> <add key="provider" providerName="MsSql" value="DMSFrame.Cache.DMSLinqSqlWebCacheNotifyProvider,DMSFrame"/> </DMSLinqCacheProvider>
最最重要的TableConfig配置資料庫加上標記CacheDependency,已便支援CacheDependency
<TableConfiguration> <Name>DefaultValue</Name> <SqlType>MsSql</SqlType> <WithLock>false</WithLock> <Author>dbo</Author> <CacheDependency>true</CacheDependency> <ConnectString>Integrated Security=False;server=127.0.0.1;database=database;User ID=sa;Password=sa;Connect Timeout=30</ConnectString> </TableConfiguration>
最後就是查詢SQL了,
ToList(bool DependencyFlag) 增加了是否使用快取的標記引數。
var resultAccess = DMS.Create<Adm_User>() .Where(q => q.UserID == 1) .Select(q => q.Columns(q.UserID, q.UpdateTime)).ToList(true);
通知模式的SQL執行跟蹤如圖:
相關下載:http://files.cnblogs.com/files/kingkoo/DMSFrameRights.zip 此版本為以前的一個版本,最新的版本請加群問群主吧
新上傳DMSFrame版本檔案:http://files.cnblogs.com/files/kingkoo/DMSFrame_Secure20150606.7z
內建程式碼引擎如下:
/// <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.GRANT SUBSCRIBE QUERY NOTIFICATIONS TO [當前資料庫登入使用者名稱,如果是sa可以跳過] /// /// 4.exec sp_changedbowner @loginame = '[當前資料庫登入使用者名稱,如果是sa,則使用sa賬號]' /// /// </summary> public class DMSLinqSqlWebCacheNotifyProvider : IDMSLinqCacheProvider { System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache; private static object syncObj = new object(); public DMSLinqSqlWebCacheNotifyProvider() { 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) { System.Web.Caching.SqlCacheDependency dependency = null; try { dependency = new System.Web.Caching.SqlCacheDependency((System.Data.SqlClient.SqlCommand)command); } 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); } } }