惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
罗磊的独立博客
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
博客园 - 叶小钗
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
博客园 - 【当耐特】
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
量子位
宝玉的分享
宝玉的分享
V
Visual Studio Blog
博客园_首页
IT之家
IT之家
V
V2EX
腾讯CDC
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
有赞技术团队
有赞技术团队
J
Java Code Geeks
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security

博客园 - kingkoo

ReaHat7.6/7.7 最小化安装更新yum源 java程序员经常使用的Intellij Idea插件 NDK版本 下载地址 在Intellij IDEA下用X-debug调试PHP DMSFrame 之查询表达式用法(一) Wise 打包细节 将Centos的yum源更换为国内的阿里云(163)源 Centos下安装 .net Core运行程序 使用 Docker 一步搞定 ZooKeeper 集群的搭建 docker 安装与学习 maven中scope标签以及exclusions 记录 Centos MySQL数据库迁移详细步骤 Maven 本地仓库明明有jar包,pom文件还是报错解决办法 位与,位或,位异或运算符的理解 编程中位运算用法总结 C++ CompletionPort(完成端口)示例 消息中间件Notify和MetaQ-阿里中间件 DMSFrame 之SqlCacheDependency(二) DMSFrame 之简单用法(二)
DMSFrame 之SqlCacheDependency(一)
kingkoo · 2015-06-08 · via 博客园 - kingkoo

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运行跟踪如图:

 相关下载:https://files.cnblogs.com/files/kingkoo/DMSFrameRights.zip 此版本为以前的一个版本,最新的版本请加群问群主吧

新上传DMSFrame版本文件:https://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);
            }
        }
    }

View Code