页面缓存和数据缓存有什么区别,具体体现在什么地方

解决方案 »

  1.   

    页面缓存就是把asp.net引擎生成的HTML标记给缓存起来。
    <%@OutputCache Duration="...以数据库为例,数据缓存是为了减少数据库访问,把数据库查询结果缓存起来。
    HttpRuntime.Cache.Insert(....);
    HttpContext.Cache["key"] = ...还有SqlDataSource/ObjectDataSource也支持数据缓存。..
      

  2.   

    页面缓存,可以将整个页面进行缓存,看一下以下代码:该文件中只有一个Label,用于显示文字<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ OutputCache Duration="60" VaryByParam="none" %><!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>
        </div>
        </form>
    </body>
    </html>
    以上文件的CS代码如下所示:using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Label1.Text = DateTime.Now.ToLongTimeString();
        }
    }
    相信大家都看得懂,以上代码的作用是将当前时间显示在网页上。如果刷新该页面,当前时间应该会改变的。
    但是,因为在aspx页面上使用了
    <%@ OutputCache Duration="60" VaryByParam="none" %>其中,OutputCache就是输出缓存,Duration为缓存时间60秒,VaryByParam说明该缓存不受post或get参数的变化而影响。因此,在打开该网页时,在60之内,无论怎么刷新,网页上的时间都不会改变的,只有60秒之后刷新才会改变。