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

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
Cloudbric
Cloudbric

博客园 - 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

上篇文章介绍的是通知模式的缓存机制,这里介绍的是数据库轮循模式处理,这种模式对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);
            }
        }
    }

View Code

同样的,代码也要配置一下.

<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(一)