数据缓存:缓存是一种在计算机中广泛用来提高性能的技术。在Web应用程序的上下文中,缓存用于在Http请求间保留页或者数据,并在无需新创建的情况下多次使用它们。目的:节省应用程序处理时间和资源。
一、页面输出缓存
存储在输出流,适用于不需要频繁更新的数据,对于数据经常更新的页面,不适合。
设置输出缓存可以使用两种方式
a.@OutputCache指令
对于OutputCache指令 Duration和VaryByParam两个属性是必须的
b.API-HttpCachePolicy类
二、页面部分缓存
a.控件缓存-Substitution控件
1.在使用Substitution时,首先我们将整个页面缓存起来,然后将页面中需要动态内容改变的地方用Substitution控件代替即可。
2.Substitution控件需要设置一个重要的属性MethodName,该属性用获取或设置当Substitution控件执行时为回调而调用的方 法名称
3.回调方法必须要符合3点:
方法必须被定义为静态方法
方法必须接受HttpContext类型的参数
方法必须返回String类型的值
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Lianxi._Default" %>
<%@ OutputCache Duration="60" VaryByParam="none" %><%--当前设置的页面缓存时间为60秒--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
页面部分缓存: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Substitution控件动态内容:<asp:Substitution ID="Substitution1" runat="server" MethodName="GetTime" /></div> </form></body></html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Lianxi
{
public partial class _Default : System.Web.UI.Page
{
//页面部分缓存
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString();//获取当前时间
}
// Substitution控件动态内容
public static string GetTime(HttpContext contenxt)
{
return DateTime.Now.ToLongTimeString();//获取当前时间
}
}
}
运行
刷新:因为设置的页面缓存时间为60秒,所以刷新页面时间不变,而Substitution控件动态内容时间改变
b.缓存后替换
三、应用程序缓存
1.应用程序数据缓存的主要功能是在内存中存储各种与应用程序相关的对象。通常这些对象都需要耗费大量的服务器资源才能建 立
2.应用程序数据缓存由Cache类实现
Cache["Categoryld"]=Request.QueryString["Categoryld"];
应用程序缓存实例
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Lianxi.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="ADD" onclick="Button1_Click"
style="height: 21px" />
<asp:Button ID="Button2" runat="server" Text="INSERT" onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="Get" onclick="Button3_Click"
style="height: 21px" />
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Caching;//添加引用
namespace Lianxi
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//Add方法
protected void Button1_Click(object sender, EventArgs e)
{
try
{
//Add方法添加缓存
Cache.Add("aaa", "ADD CACHE", null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
}
catch
{
Response.Write("ERROR");
}
}
//Insert方法
protected void Button2_Click(object sender, EventArgs e)
{
//Insert方法添加缓存
Cache.Insert("aaa", "INSERT CACHE");
}
//查看缓存
protected void Button3_Click(object sender, EventArgs e)
{
//判断当前缓存是否过期
Cache.Get("");
if (Cache["aaa"] != null)
{
string str = (string)Cache["aaa"];
}
else{
Response.Write("缓存无效");
}
}
}
}
四、缓存依赖
通过缓存依赖,可以在被依赖对象(如文件、目录、数据库表等)与缓存对象之间建立一个有效关联。当被依赖对象发生变化时缓存对象将变得不可用,并被自动从缓存中移除。
在构造Sql数据缓存依赖对象时,我们要注意:应用SQL server2005时,必须使用构造函数,在sqlcmd中将涉及想关SQL查询语句(select),这些语句必须满足以下要求:
1.必须定义完全限定的表名,包含表所有者的名称如dbo.Users
2.必须在Select语句中显示指定列名,不能使用*号通配符选择表中的所有列。
3.不能在查询语句中使用聚合函数。