前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Enterprise Library 4.1学习笔记7----缓存应用程序块之SqlDependency

Enterprise Library 4.1学习笔记7----缓存应用程序块之SqlDependency

作者头像
菩提树下的杨过
发布2018-01-23 10:36:48
5550
发布2018-01-23 10:36:48
举报

本文是在Artech“[原创]Enterprise Library深入解析与灵活应用(2): 通过SqlDependency实现Cache和Database的同步”的基础之上,将其示例移植到webform环境中而已,详细原理还请大家见Artech的文章 

应用场景:利用Enlib4.1的缓存模块,实现常用数据的缓存,同时借助SqlDependency通过"监控数据是否有改动"来决定缓存是不是过期。

1.测试数据表及数据(sql2005环境)

代码语言:js
复制
Code

 CREATE TABLE [dbo].[Messages](
   [ID] [int] IDENTITY(1,1) NOT NULL,
   [UserID] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
   [Message] [nvarchar](256) COLLATE Chinese_PRC_CI_AS NOT NULL,
CONSTRAINT [PK_Messages] PRIMARY KEY CLUSTERED 
 (
   [ID] ASC
 )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
 ) ON [PRIMARY]
GO
SET ANSI_PADDING OFF

Insert Into [Messages](UserID,[Message]) values('JIMMY','Test Message1');
Insert Into [Messages](UserID,[Message]) values('Jack','AAAAAA');

Insert Into [Messages](UserID,[Message]) values('Jack','AAAAAA');

2.实现自己的SqlDependencyCacheItemExpiration类,这里直接用Artech的代码,就不重复贴出来了

3.webform调用

代码语言:js
复制
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Data.Common;
 5 using Microsoft.Practices.EnterpriseLibrary.Caching;
 6 using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
 7 using Microsoft.Practices.EnterpriseLibrary.Data;
 8 
 9 namespace WebApp
10 {
11  public partial class _Default : System.Web.UI.Page
12     {
13 
14  string CacheKey="JIMMY_Message_SqlDependency_Cache";
15  
16 
17  protected void Page_Load(object sender, EventArgs e)
18         {
19  if (!IsPostBack) 
20             {
21  this.Repeater1.DataSource = GetMessages("JIMMY");
22  this.Repeater1.DataBind();
23             }           
24         }
25 
26  /// <summary>
27  /// 从数据库实时查询指定用户的Message
28  /// </summary>
29  /// <param name="userName"></param>
30  /// <param name="sql"></param>
31  /// <returns></returns>
32  private DataTable GetMessageByUserId(string userName,string sql) 
33         {
34             Database db = DatabaseFactory.CreateDatabase();
35  if (sql.Length==0) 
36             {
37                 sql = "Select [ID],[UserID],[Message] From [dbo].[Messages] Where [UserID]=@UserID";
38             }
39             DbCommand command = db.GetSqlStringCommand(sql);
40             db.AddInParameter(command, "UserID", DbType.String, userName);
41  return db.ExecuteDataSet(command).Tables[0];
42  
43         }
44 
45  /// <summary>
46  /// 获取用户的Message(缓存优先,数据库其次)
47  /// </summary>
48  /// <param name="userName"></param>
49  /// <returns></returns>
50  private DataTable GetMessages(string userName)
51         {
52             ICacheManager manager = CacheFactory.GetCacheManager();
53  if (manager.GetData(CacheKey) == null)
54             {
55  string sql = "Select [ID],[UserID],[Message] From [dbo].[Messages] Where [UserID]=@UserID";
56 
57                 Dictionary<string, object> Dic = new Dictionary<string, object>();
58                 Dic.Add("UserID", userName);
59                 manager.Add(CacheKey, GetMessageByUserId(userName, sql), CacheItemPriority.Normal, null, new SqlDependencyExpiration(sql,CommandType.Text, Dic));
60             }
61  
62  return manager.GetData(CacheKey) as DataTable;
63         }
64     }
65 }
66 

测试方法:

上面的示例中,缓存了[dbo].[Messages].[UserID]='JIMMY'的数据,如果首次打开页面时,直接从数据库中取数据,然后刷新一下,从Sql监视器中能看到此时并没有提交查询语句,即直接从缓存中读取数据。

然后在数据库中,直接UserID='JIMMY'的记录(比如修改Message字段值),再次刷新页面,会发现重新向数据库提交了查询语句(即更新了缓存),然后再次刷新,直接从缓存读取。

最后在数据库中,修改UserID<>'JIMMY'的记录,再次刷新页面,还是从缓存中数据(即修改UserID不为JIMMY的记录,不会触发SqlDependency的OnChange事件,缓存未过期)

源代码下载:http://files.cnblogs.com/yjmyzz/SqlDependency_Cache_Test.rar

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009-06-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档