第三篇 Entity Framework Plus 之 Query Cache

   离上一篇博客,快一周,工做太忙,只能利用休息日来写一些跟你们分享,Entity Framework Plus 组件系列文章,以前已经写过两篇html

第一篇 Entity Framework Plus 之 Auditgit

第二篇 Entity Framework Plus 之 Query Futuregithub

计划还会写两篇,一篇是关于查询缓存的(二级缓存),一篇是批量操做(只讲更新,删除)。数据库

   今天写查询缓存,标题 第三篇 Entity Framework Plus 之 Query Cache  ,废话很少说,直接实做。缓存

一. 代码实现ide

   1.  解决方案仍是前两篇的用的 “EntityFrameworkPlusSolution” 解决方案,在“01.Demo” 解决方案文件夹,新增“EntityFrameworkPlus.QueryCache.Demo” Windows 项目(为何要用Windows 项目,方便Demo和测试而已。)post

 2. “EntityFrameworkPlus.QueryCache.Demo” 项目中,删除原来Form1 窗口相关文件,分别新增“QueryCache” 后,“NoExpirationQueryCache”,“AbsoluteExpirationQueryCache”,“SlidingExpirationQueryCache” 窗口,界面设计分别以下图。测试

  QueryCacheurl

NoExpirationQueryCachespa

AbsoluteExpirationQueryCache

SlidingExpirationQueryCache

四个界面很简单,这里大概介绍一下, 

QueryCache 就像操做台同样,是其余三个界面入口点。

NoExpirationQueryCache 不作缓存过时时间设置查询缓存

AbsoluteExpirationQueryCache 指定缓存过时时间(以秒为单位) 设置查询缓存

SlidingExpirationQueryCache 最后一次访问缓存时间间隔(以秒为单位) 设置查询缓存

3. 代码

QueryCache 

using System; using System.Windows.Forms; namespace EntityFrameworkPlus.QueryCache.Demo { public partial class QueryCache : Form { public QueryCache() { InitializeComponent(); } private void btnNoExpirationQueryCache_Click(object sender, EventArgs e) { new NoExpirationQueryCache().ShowDialog(); } private void btnAbsoluteExpirationQueryCache_Click(object sender, EventArgs e) { new AbsoluteExpirationQueryCache().ShowDialog(); } private void btnSlidingExpirationQueryCache_Click(object sender, EventArgs e) { new SlidingExpirationQueryCache().ShowDialog(); } } }
View Code

NoExpirationQueryCache 

 1 using System;  2 using System.Windows.Forms;  3 using EntityFrameworkPlus.DbContext;  4 using Z.EntityFramework.Plus;  5 
 6 namespace EntityFrameworkPlus.QueryCache.Demo  7 {  8     public partial class NoExpirationQueryCache : Form  9  { 10         public NoExpirationQueryCache() 11  { 12  InitializeComponent(); 13  } 14 
15         private void btnSearch_Click(object sender, EventArgs e) 16  { 17             using (var db = new EntityFrameworkPlusDbContext()) 18  { 19                 var orders = db.Goodses.FromCache(); 20                 dgList.DataSource = orders; 21  } 22  } 23  } 24 }
View Code

AbsoluteExpirationQueryCache 

 1 using System;  2 using System.Windows.Forms;  3 using EntityFrameworkPlus.DbContext;  4 using Z.EntityFramework.Plus;  5 
 6 namespace EntityFrameworkPlus.QueryCache.Demo  7 {  8     public partial class AbsoluteExpirationQueryCache : Form  9  { 10         public AbsoluteExpirationQueryCache() 11  { 12  InitializeComponent(); 13  } 14 
15         private void btnSearch_Click(object sender, EventArgs e) 16  { 17             using (var db = new EntityFrameworkPlusDbContext()) 18  { 19                 var second = Convert.ToDouble(txtAbsoluteExpiration.Text.Trim()); 20                 var absoluteExpirationSecond = DateTime.Now.AddSeconds(second); 21                 var orders = db.Goodses.FromCache(absoluteExpirationSecond); 22                 dgList.DataSource = orders; 23  } 24  } 25  } 26 }
View Code

SlidingExpirationQueryCache 

 1 using System;  2 using System.Runtime.Caching;  3 using System.Windows.Forms;  4 using EntityFrameworkPlus.DbContext;  5 using Z.EntityFramework.Plus;  6 
 7 namespace EntityFrameworkPlus.QueryCache.Demo  8 {  9     public partial class SlidingExpirationQueryCache : Form 10  { 11         public SlidingExpirationQueryCache() 12  { 13  InitializeComponent(); 14  } 15 
16         private void btnSearch_Click(object sender, EventArgs e) 17  { 18             using (var db = new EntityFrameworkPlusDbContext()) 19  { 20                 var second = Convert.ToDouble(txtSlidingExpiration.Text.Trim()); 21                 var options = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromSeconds(second) }; 22                 var orders = db.Goodses.FromCache(options); 23                 dgList.DataSource = orders; 24  } 25  } 26  } 27 }
View Code

代码就不作解释,等一下逐个测试一下给你们看,查询的信息是上一篇第二篇 Entity Framework Plus 之 Query Future商品信息(Sample_Goods),查询后会直接展现到DataGridView里面。

二.测试效果(记得打开SQL Profiler 追踪SQL)

NoExpirationQueryCache  不作缓存过时时间设置查询缓存

1. 点击查询按钮一次,Demo以下图

2. 清空一下Sql Profiler 执行的SQL(这个应该不用我教你们都会),接着连续点击查询按钮,Demo如图

DataGridView 依旧会有数据展现出来,可是SQL Profiler 是没有执行查询商品信息的SQL,说明以后查询是取缓存中的,若是缓存中存在,就不去执行SQL.

AbsoluteExpirationQueryCache 指定缓存过时时间(以秒为单位) 设置查询缓存 (每次Demo另一个场景,把程序关闭一次,避免Demo不会有问题。)

1. 设置缓存过时时间为10秒,点一次查询,Demo以下图

 

2. 清空一下Sql Profiler 执行的SQL,而后在10范围内,连续点查询,Demo以下图 

3. 过了10秒在点查询按钮,Demo 以下图

从上面三种操做,说明10秒内,点击查询,商品信息,已经被缓存,就不会和数据库进行交流,当过了10秒商品信息的缓存就会自动清除,从新到数据库取数据。

 

SlidingExpirationQueryCache 最后一次访问缓存时间间隔(以秒为单位) 设置查询缓存

1. 最后访问缓存时间间隔设置10s ,点击查询,Demo以下图

2. 清空一下Sql Profiler 执行的SQL,连续点击查询按钮,只要最后一次访问缓存不超过10s,Demo以下图

3. 最后一次访问缓存时间间隔晚于10s ,Demo以下图

从上面三种操做来看,只要最后一次访问缓存不超过设置时间间隔,即就不会和数据库打交道,老是会取缓存数据,不然否则。

到此 Entity Framework Plus 之 Query Cache 就写完,你们能够更加深刻的了解 Entity Framework Plus Query Cache 能够自行看源码

Entity Framework Plus GitHub :https://github.com/zzzprojects/EntityFramework-Plus

本博文源代码 :https://github.com/haibozhou1011/EntityFramework-PlusSample

相关文章
相关标签/搜索