使用EnterpriseLibrary5实现数据的缓存(附完整代码下载)
2011-04-28 10:05
866 查看
本例子演示了如何使用微软企业类库5.0实现数据的缓存。
本文代码下载
1,打开visual studio 2010,新建一个winform项目,并命名为CachingByEnterpriseLibrary5。
2,添加如下引用
Microsoft.Practices.EnterpriseLibrary.Caching (C:\Program Files\Microsoft Enterprise Library 5.0\Bin\Microsoft.Practices.EnterpriseLibrary.Caching.dll)
Microsoft.Practices.EnterpriseLibrary.Common (C:\Program Files\Microsoft Enterprise Library 5.0\Bin\Microsoft.Practices.EnterpriseLibrary.Common.dll)
Microsoft.Practices.ServiceLocation (C:\Program Files\Microsoft Enterprise Library 5.0\Bin\Microsoft.Practices.ServiceLocation.dll)
说明:EnterpriseLibrary5.0 下载地址
3,新增数据访问类 DataAccessLayer.cs,数据库使用的微软示例数据库Northwind。
4,增加数据缓存类CachingInfrastructure.cs
5,增加App.Config文件,然后右键该文件。
在弹出的对话框中选中 ‘Blocks’-> ‘Add Caching Settings’
6,Form1界面效果如下图所示
代码如下:
为了手工控制ComboBox,添加自定义类文件Item.cs
7,如何验证是从缓存中读取的数据还是从数据库读取的数据?可以使用SQL Server的事件探查器来跟踪应用程序是否执行了相应的
SQL脚本来验证。打开事件探查器(sql server profiler)后,连续双击“加载数据“按钮即可!
看上图中两条被选中的记录间隔的时间正好10s,这也验证了下列代码
本文代码下载
1,打开visual studio 2010,新建一个winform项目,并命名为CachingByEnterpriseLibrary5。
2,添加如下引用
Microsoft.Practices.EnterpriseLibrary.Caching (C:\Program Files\Microsoft Enterprise Library 5.0\Bin\Microsoft.Practices.EnterpriseLibrary.Caching.dll)
Microsoft.Practices.EnterpriseLibrary.Common (C:\Program Files\Microsoft Enterprise Library 5.0\Bin\Microsoft.Practices.EnterpriseLibrary.Common.dll)
Microsoft.Practices.ServiceLocation (C:\Program Files\Microsoft Enterprise Library 5.0\Bin\Microsoft.Practices.ServiceLocation.dll)
说明:EnterpriseLibrary5.0 下载地址
3,新增数据访问类 DataAccessLayer.cs,数据库使用的微软示例数据库Northwind。
using System; using System.Collections.ObjectModel; using System.Data.SqlClient; namespace CachingByEnterpriseLibrary5 { public class Employee { public int Id { get; set; } public string LastName { get; set; } public string FirstName { get; set; } public string Title { get; set; } public DateTime BirthDate { get; set; } public string City { get; set; } } class DataAccess { SqlConnection Conn; SqlCommand Command; SqlDataReader Reader; public DataAccess() { Conn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=SSPI"); } public ObservableCollection<Employee> GetEmployees() { ObservableCollection<Employee> lstEmployees = new ObservableCollection<Employee>(); Conn.Open(); Command = new SqlCommand(); Command.Connection = Conn; Command.CommandText = "select * from Employees"; Reader = Command.ExecuteReader(); while (Reader.Read()) { lstEmployees.Add(new Employee() { Id = Convert.ToInt32(Reader["EmployeeID"]), LastName = Reader["LastName"].ToString(), FirstName = Reader["FirstName"].ToString(), Title = Reader["Title"].ToString(), BirthDate = Convert.ToDateTime(Reader["BirthDate"]), City = Reader["City"].ToString() }); } Reader.Close(); Conn.Close(); return lstEmployees; } } }
4,增加数据缓存类CachingInfrastructure.cs
using System; using System.Collections.ObjectModel; using System.Linq; using Microsoft.Practices.EnterpriseLibrary.Caching; using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; namespace CachingByEnterpriseLibrary5 { public class CachingInfrastructure { static ObservableCollection<Employee> lstEmployee = null; static ICacheManager cacheEmployeeData; public CachingInfrastructure() { } public static ObservableCollection<Employee> GetEmployeeData(out string cacheStatus) { //尝试从缓存中读取数据 cacheEmployeeData = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>(); lstEmployee = (ObservableCollection<Employee>)cacheEmployeeData["EmployeeDataCache"]; cacheStatus = "从缓存中读取全部员工数据"; //如果缓存中没有任何数据,那么直接从数据访问层读取 if (lstEmployee == null) { DataAccess da = new DataAccess(); lstEmployee = da.GetEmployees(); //定义缓存的有效期限(为了演示缓存效果,设置为10s) AbsoluteTime CacheExpiretionTime = new AbsoluteTime(new TimeSpan(0, 0, 10)); //把数据放入缓存 cacheEmployeeData.Add("EmployeeDataCache", lstEmployee, CacheItemPriority.High, null, new ICacheItemExpiration[] { CacheExpiretionTime }); cacheStatus = "员工的信息刚被放入缓存中"; } return lstEmployee; } public static Employee GetEmployeeById(int id, out string cacheStatus) { Employee emp = null; emp = (from e in lstEmployee where e.Id == id select e).FirstOrDefault(); cacheStatus = "从缓存中读取指定id的员工信息"; return emp; } public static void ClearCache(out string cacheStatus) { ICacheManager cacheEmployeeData = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>(); cacheEmployeeData.Flush(); cacheStatus = "缓存已被清空"; } } }
5,增加App.Config文件,然后右键该文件。
在弹出的对话框中选中 ‘Blocks’-> ‘Add Caching Settings’
6,Form1界面效果如下图所示
代码如下:
using System; using System.Collections.ObjectModel; using System.Windows.Forms; namespace CachingByEnterpriseLibrary5 { public partial class Form1 : Form { ObservableCollection<Employee> lstEmployee = new ObservableCollection<Employee>(); string cacheStauts = string.Empty;//缓存状态 public Form1() { InitializeComponent(); } //加载员工信息 private void btnLoadData_Click(object sender, EventArgs e) { lstEmployee = CachingInfrastructure.GetEmployeeData(out cacheStauts); lblCacheStatus.Text = cacheStauts; foreach (var emp in lstEmployee) { cmbEmployee.Items.Add(new Item(emp.LastName, emp.Id)); } cmbEmployee.DroppedDown = true; cmbEmployee.SelectedValueChanged += new System.EventHandler(cmbEmployee_SelectedIndexChanged); } //读取指定Id的员工信息 private void cmbEmployee_SelectedIndexChanged(object sender, EventArgs e) { int Id = Convert.ToInt32(((Item)cmbEmployee.SelectedItem).Value); Employee Emp = CachingInfrastructure.GetEmployeeById(Id, out cacheStauts); lblCacheStatus.Text = cacheStauts; txtId.Text = Emp.Id.ToString(); txtFirstName.Text = Emp.FirstName; txtLastName.Text = Emp.LastName; txtTitle.Text = Emp.Title; txtCity.Text = Emp.City; txtBirthDate.Text = Emp.BirthDate.ToShortDateString(); } //手工清空缓存 private void btnClearCache_Click(object sender, EventArgs e) { CachingInfrastructure.ClearCache(out cacheStauts); lblCacheStatus.Text = cacheStauts; cmbEmployee.Items.Clear(); cmbEmployee.Text = ""; txtId.Text = ""; txtFirstName.Text = ""; txtLastName.Text = ""; txtTitle.Text = ""; txtCity.Text = ""; txtBirthDate.Text = ""; lstEmployee.Clear(); } } }
为了手工控制ComboBox,添加自定义类文件Item.cs
namespace CachingByEnterpriseLibrary5 { //为了方便控制combobox,采用自定义类Item给combobox添加项目 public class Item { public string Name; public int Value; public Item(string name, int value) { Name = name; Value = value; } public override string ToString() { // Generates the text shown in the combo box return Name; } } }
7,如何验证是从缓存中读取的数据还是从数据库读取的数据?可以使用SQL Server的事件探查器来跟踪应用程序是否执行了相应的
SQL脚本来验证。打开事件探查器(sql server profiler)后,连续双击“加载数据“按钮即可!
看上图中两条被选中的记录间隔的时间正好10s,这也验证了下列代码
//定义缓存的有效期限(为了演示缓存效果,设置为10s) AbsoluteTime CacheExpiretionTime = new AbsoluteTime(new TimeSpan(0, 0, 10)); //把数据放入缓存cacheEmployeeData.Add("EmployeeDataCache", lstEmployee, CacheItemPriority.High, null, new ICacheItemExpiration[] { CacheExpiretionTime });
相关文章推荐
- 使用secondary sort实现数据关联 完整示例代码
- Delphi中使用ISuperObject解析Json数据的实现代码
- Python使用sftp实现上传和下载功能(实例代码)
- 使用PHP自带zlib函数 几行代码实现PHP文件打包下载zip
- 使用httpclient实现上传下载(javaWeb系统数据传输http实现)
- 使用VS2010 C#开发ActiveX控件(下),完整代码下载
- 【代码练习5】使用套接字实现客户端和服务器的数据传输
- 使用javascript实现json数据以csv格式下载
- java代码实现highchart与数据库数据结合完整案例分析(一)---饼状图
- 使用javamail实现邮件发送功能完整代码
- 使用volley+universal image loader实现数据缓存和读取
- [导入]AjaxPro.NET实现TextBox智能获取服务端数据功能(Asp.net 2.0)(示例代码下载)
- [导入]Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 使用xom实现xml文件数据的查找,删除,修改(完整beta版,daya原创)
- 使用JSON实现数据的跨域传输的php代码
- IONIC 中使用拦截功能来实现API接口访问的数据缓存
- 使用python提取html文件中的特定数据的实现代码
- STM32F4板子使用LWIP进行组播收发数据的完整过程,附代码
- 使用httpclient实现上传下载(javaWeb系统数据传输http实现)