利用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秒一读取
            }

        }
    }



}

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

 


 

 

转载于:https://www.cnblogs.com/flyfish2012/p/3466933.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值