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); } } }