zoukankan      html  css  js  c++  java
  • 利用MSSQL对不经常使用的表进行依赖缓存

    缓存是我们开发应用系统的一把利刃,如果用的不好,会导致数据不准确等一系列问题。

    所以在如何选择缓存的时候,我们要慎之又慎。所以在对系统中一些 不经常变化的表,我们可以采用SqlCacheDenpendency进行帮我进行缓存

    只要在数据库中的数据不更新,那么数据永远在缓存。

    但要实现sql 依赖缓存,需要数据库服务器支持。所以我们前期得进行数据库配置:

    1.启用 Service Broker。可以通过下边语句查看是否启用。

    select DatabasePropertyex('Northwind','IsBrokerEnabled')
    --返回1表示true,返加0表示false
    use master
    Alter Database Northwind set enable_broker

    切记,如果半天不执行,立马重启数据库服务器,重启好 立马执行就可以了。
    2. 给您的数据库访问帐号授予权限(对不起,不支持sa)

    GRANT SUBSCRIBE QUERY NOTIFICATIONS TO User(自己建立的帐号)

    好了,配置数据库就这么多动作。

    剩下就是贴代码了:

    1.webconfig 配置:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
        <system.web>
        <caching>
            <sqlCacheDependency enabled="true" pollTime="10000">
                <databases>
                    <add name="Northwind" connectionStringName="SQL2000"/>
                </databases>
            </sqlCacheDependency>
        </caching>
        </system.web>
    </configuration>

    2.程序执行代码:

    namespace StudySqlDenpendency {
        internal class Program {
            private static void Main(string[] args) {
                //
                SleepGetCache();
                //SqlDependency.Stop("server=10.1.0.103;database=DHCustomerPotal;uid=customerUser;pwd=123"); //开启依赖缓存
                Console.ReadKey();
    
            }
    
            public static object GetCache(string KeyName) {
                string CacheKey = KeyName;
                var objCache = HttpRuntime.Cache;
                var objModel = objCache[CacheKey];
                if (objModel != null) {
                    Console.WriteLine("已经取到缓存!");
                    return objModel;
                } else {
                    SqlDependency.Start("server=10.1.0.103;database=DHCustomerPotal;uid=customerUser;pwd=123"); //开启依赖缓存
                    using (SqlConnection cn = new SqlConnection("server=10.1.0.103;database=DHCustomerPotal;uid=customerUser;pwd=123")) {
                        using (SqlCommand cmd = cn.CreateCommand()) {
                            cn.Open();
                            cmd.CommandText = "select fid from dbo.T_PDM_ProductInfo";
                            SqlCacheDependency dep = new SqlCacheDependency(cmd);
                            DataTable dt = new DataTable();
                            using (SqlDataAdapter adapter = new SqlDataAdapter()) //查询数据
                            {
                                adapter.SelectCommand = cmd;
                                adapter.Fill(dt);
                            }
    
                            //SqlDependency dep=new SqlDependency(cmd);
                            objCache.Insert(CacheKey, dt, dep);
                            objModel = objCache[CacheKey];
                            return objModel;
                        }
                    }
                
    
                }
    
            }
    
    
            public static void start() {
                Stopwatch st = new Stopwatch();
                st.Start();
                var dts = GetCache("testKeyId") as DataTable;
                if (dts != null) {
                    Console.WriteLine("读取的数据总数为:{0}", dts.Rows.Count.ToString());
                }
                st.Stop();
                Console.WriteLine("依赖缓存时间秒:{0}", st.ElapsedMilliseconds.ToString());
            }
    
            public static void SleepGetCache() {
                while (true) {
                    ThreadStart threadStart = new ThreadStart(start);
                    Thread myThread = new Thread(threadStart);
                    myThread.Start();
                    Thread.Sleep(3000); //10秒一读取
                }
    
            }
        }
    
    
    
    }

    好了,到此为止,大家可以测试下,删除数据会不会有变化


     

  • 相关阅读:
    【GStreamer开发】GStreamer基础教程14——常用的element
    【GStreamer开发】GStreamer基础教程12——流
    【GStreamer开发】GStreamer基础教程12——流
    【GStreamer开发】GStreamer基础教程11——调试工具
    【GStreamer开发】GStreamer基础教程11——调试工具
    【GStreamer开发】GStreamer基础教程10——GStreamer工具
    【GStreamer开发】GStreamer基础教程10——GStreamer工具
    【GStreamer开发】GStreamer基础教程09——收集媒体信息
    【GStreamer开发】GStreamer基础教程08——pipeline的快捷访问
    【GStreamer开发】GStreamer基础教程08——pipeline的快捷访问
  • 原文地址:https://www.cnblogs.com/flyfish2012/p/3466933.html
Copyright © 2011-2022 走看看