第三篇 Entity Framework Plus 之 Query Cache

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

第一篇 Entity Framework Plus 之 Audit

第二篇 Entity Framework Plus 之 Query Future

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

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

一. 代码实现

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

第三篇 Entity Framework Plus 之 Query Cache

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

QueryCache

第三篇 Entity Framework Plus 之 Query Cache

NoExpirationQueryCache

第三篇 Entity Framework Plus 之 Query Cache

AbsoluteExpirationQueryCache

第三篇 Entity Framework Plus 之 Query Cache

SlidingExpirationQueryCache

第三篇 Entity Framework Plus 之 Query Cache

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

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();
}
}
}

NoExpirationQueryCache

 using System;
using System.Windows.Forms;
using EntityFrameworkPlus.DbContext;
using Z.EntityFramework.Plus; namespace EntityFrameworkPlus.QueryCache.Demo
{
public partial class NoExpirationQueryCache : Form
{
public NoExpirationQueryCache()
{
InitializeComponent();
} private void btnSearch_Click(object sender, EventArgs e)
{
using (var db = new EntityFrameworkPlusDbContext())
{
var orders = db.Goodses.FromCache();
dgList.DataSource = orders;
}
}
}
}

AbsoluteExpirationQueryCache

 using System;
using System.Windows.Forms;
using EntityFrameworkPlus.DbContext;
using Z.EntityFramework.Plus; namespace EntityFrameworkPlus.QueryCache.Demo
{
public partial class AbsoluteExpirationQueryCache : Form
{
public AbsoluteExpirationQueryCache()
{
InitializeComponent();
} private void btnSearch_Click(object sender, EventArgs e)
{
using (var db = new EntityFrameworkPlusDbContext())
{
var second = Convert.ToDouble(txtAbsoluteExpiration.Text.Trim());
var absoluteExpirationSecond = DateTime.Now.AddSeconds(second);
var orders = db.Goodses.FromCache(absoluteExpirationSecond);
dgList.DataSource = orders;
}
}
}
}

SlidingExpirationQueryCache

 using System;
using System.Runtime.Caching;
using System.Windows.Forms;
using EntityFrameworkPlus.DbContext;
using Z.EntityFramework.Plus; namespace EntityFrameworkPlus.QueryCache.Demo
{
public partial class SlidingExpirationQueryCache : Form
{
public SlidingExpirationQueryCache()
{
InitializeComponent();
} private void btnSearch_Click(object sender, EventArgs e)
{
using (var db = new EntityFrameworkPlusDbContext())
{
var second = Convert.ToDouble(txtSlidingExpiration.Text.Trim());
var options = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromSeconds(second) };
var orders = db.Goodses.FromCache(options);
dgList.DataSource = orders;
}
}
}
}

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

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

NoExpirationQueryCache  不做缓存过期时间设置查询缓存

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

第三篇 Entity Framework Plus 之 Query Cache

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

第三篇 Entity Framework Plus 之 Query Cache

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

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

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

第三篇 Entity Framework Plus 之 Query Cache

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

第三篇 Entity Framework Plus 之 Query Cache

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

第三篇 Entity Framework Plus 之 Query Cache

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

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

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

第三篇 Entity Framework Plus 之 Query Cache

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

第三篇 Entity Framework Plus 之 Query Cache

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

第三篇 Entity Framework Plus 之 Query Cache

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

到此 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

上一篇:linux异常处理:selinux配置错误导致无法重启


下一篇:Python基础之字符串