闲谈ASP.NET 2.0缓存技术

    这一个学期一直都生活在忙忙碌碌、犹犹豫豫之中,也一直没有什么心情写一些技术性的东西。这两天刚从考研的囫囵里跳出来,顿感轻松了许多。人也活力了很多。遂决定时不时的写点东西。今天刚好把袁阔成的评三国下下来了,一边听着三国,一边写。今天想闲谈一下ASP.NET 2.0缓存技术。
    1.在.aspx页面加<%@ OutputCache Duration="60" VaryByParam="none" %>  Duration为缓存时间,VaryByParam为QueryString,none表示地址没有参数。
    ......
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    ......
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        lblTime.Text = DateTime.Now.ToString();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblTime" Text="" runat="server"></asp:Label>
    </div>
    </form>
    2.同1,只是VaryByParam值不为none,可以任设,比如state。例如:链接<a href="default.aspx?state=...">链接</a>,state的值做为一个GridView数据源的参数,为了能看出效果,最好加一个lable放时间,点链接之后,选出了一部分数据,这就是对一个页面不同的参数的分别缓存效果。
    ......
    <%@ OutputCache Duration="60" VaryByParam="state" %>
    ......
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["state"]==null)
            lblTime.Text = DateTime.Now.ToString();
        else if(Request.QueryString["state"].ToString()=="1")
            lblTime1.Text = DateTime.Now.ToString();
        else if(Request.QueryString["state"].ToString()=="2")
            lblTime2.Text = DateTime.Now.ToString();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
        <a href="OutputCacheWithParam.aspx">刷新</a><br />
        <a href="OutputCacheWithParam.aspx?state=1">刷新1</a><br />
        <a href="OutputCacheWithParam.aspx?state=2">刷新2</a><br />
        <asp:Label ID="lblTime" ForeColor="red" Text="" runat="server"></asp:Label><br />
        <asp:Label ID="lblTime1" ForeColor="green" Text="" runat="server"></asp:Label><br />
        <asp:Label ID="lblTime2" ForeColor="blue" Text="" runat="server"></asp:Label><br />
    </div>
    </form>
    3.上面两种方法的缺点就是页面的数据不能及时更新。有的时候要给用户显示一些很重要的数据,这样的方式就不能满足了,下面的部分缓存可以解决这以问题。
    ......
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    ......
    <script runat="server">
    static string GetCurentTime(HttpContext context)
    {
        return DateTime.Now.ToString();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
    缓存时间:
    <asp:Label ID="lblTime" ForeColor="red" Text="" runat="server"><%=DateTime.Now.ToString() %></asp:Label><br /><br />
    现在时间:
    <asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurentTime" />
    </div>
    </form>
    
    这里有一个方法静态GetCurentTime,返回值作为动态数据传给Substitution,这样,第一个时间在缓存期内不改变,第二个时间不受缓存影响。
    4.用API
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));//相当于duration
        Response.Cache.SetCacheability(HttpCacheability.Public);//相当于VaryByParam
        lblTime.Text = DateTime.Now.ToString();
    }
    </script>
    ......
    5.利用Page.Cache缓存数据
    ......
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        DataView source = (DataView)Cache["UsersCache"];

        if (source == null)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["UsersConnectionString"].ConnectionString);
            SqlDataAdapter adp = new SqlDataAdapter("select * from Users", conn);
            DataSet ds = new DataSet();
            adp.Fill(ds, "UsersTable");

            source = new DataView(ds.Tables["UsersTable"]);
            Cache["UsersCache"] = source;
        }
        this.GridView1.DataSource = source;
        this.GridView1.DataBind();
    }
    </script>
    ......
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>

    6.实现用户控件缓存,但页面其他地方不缓冲,也是实现部分缓存
    --WebUserControl.ascx部分代码
    ......
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    <%--这里可以是很大的数据库数据,所以缓存非常有必要--%>
    这是用户控件中的内容:<br />
    <asp:Label runat="server" ID="lblTime1" ForeColor="red"><%=DateTime.Now.ToString() %></asp:Label>

    --PageFragmentCaching.aspx部分代码
    ......
    <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
    .......
    <form id="form1" runat="server">
    <div>
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
        <hr />
        这里是主文件的内容:<br />
        <asp:Label runat="server" ID="lblTime" ForeColor="green"><%=DateTime.Now.ToString() %></asp:Label>
    </div>
    </form>

    今天就写到这里了。唉,不写还好,一些发现了太多的问题,文笔不好不说,写出来的代码估计都是有问题的。看来以后不管多紧张,还是要坚持写博啊。

原文地址:https://www.cnblogs.com/myself/p/1229981.html