Asp.net页面数据缓存技术

下面我将介绍如何将数据放在缓存中,这样下次页面请求时,直接从缓存中读数据就可以啦!

这里我用的是从XML中读数据

先创建XML文件

View Code
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <good>
 3 <goods>
 4   <name>苹果</name>
 5   <price>7元</price>
 6 </goods>
 7 
 8 <goods>
 9   <name>荔枝</name>
10   <price>7元</price>
11 </goods>
12 
13 <goods>
14   <name>黄瓜</name>
15   <price>7元</price>
16 </goods>
17 
18   <goods>
19   <name>青菜</name>
20   <price>7元</price>
21   </goods>
22 </good>

接着是前台:

View Code
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="pageDataCache.aspx.cs" Inherits="CacheTechnology_pageDataCache" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <title>无标题页</title>
 8 </head>
 9 <body>
10     <form id="form1" runat="server">
11     <div>
12     页面缓存应用:
13         <asp:Repeater ID="Repeater1" runat="server">
14         
15         <HeaderTemplate>
16         <table>
17         <tr><td>种类</td><td>价格</td></tr>
18         </HeaderTemplate>
19         
20         <ItemTemplate>
21         <tr>
22         <td><%#Eval("name") %></td>
23         <td><%#Eval("price") %></td>
24         </tr>
25         </ItemTemplate>
26         
27         <FooterTemplate>
28         </table>
29         </FooterTemplate>
30         
31         </asp:Repeater>
32         
33         <asp:Button ID="btnAdd" runat="server" Text="ADD" onclick="btnAdd_Click" />  
34         <asp:Button ID="btnSearch" runat="server" Text="SEARCH" 
35             onclick="btnSearch_Click" />
36         <asp:Button ID="btnMove" runat="server" Text="MOVE" onclick="btnMove_Click" />
37       <br /> <asp:Label ID="lbHas" runat="server" Text="Label"></asp:Label>
38         <br /> <asp:Label ID="lbNo" runat="server" Text="Label"></asp:Label>
39     </div>
40     </form>
41 </body>
42 </html>

说明:ADD主要是把数据放到缓存中,Search是检测缓存中是否存在数据.Remove是清空缓存
后台:

View Code
 1 using System;
 2 using System.Collections;
 3 using System.Configuration;
 4 using System.Data;
 5 using System.Linq;

 6 using System.Web;
 7 using System.Web.Security;
 8 using System.Web.UI;
 9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 
14 public partial class CacheTechnology_pageDataCache : System.Web.UI.Page
15 {
16   
17     //页面加载时先判断缓存中是否有数据,有就直接从缓存读数据,没有就从xml中读数据
18     protected void Page_Load(object sender, EventArgs e)
19     {
20         DataSet ds = new DataSet();
21         //缓存数据存在
22         if (Cache["key"] != null)
23         {
24             ds = (DataSet)Cache["key"];
25             Repeater1.DataSource = ds;
26             Repeater1.DataBind();
27         }
28        //缓存数据为空
29         else
30         {
31             //ds直接从Xml中读数据
32             ds.ReadXml(MapPath("pageDataCache.xml"));
33             Repeater1.DataSource = ds;
34             Repeater1.DataBind();
35          
36         }
37     }
38 
39         //将数据放入缓存
40         protected void btnAdd_Click(object sender, EventArgs e)
41         {
42             if (Cache["key"] == null)
43             {
44                 DataSet ds = new DataSet();
45                 ds.ReadXml(MapPath("pageDataCache.xml"));
46                 Cache.Insert("key",ds,new System.Web.Caching.CacheDependency(MapPath("pageDataCache.xml")));
47             }
48         }
49 
50       //检测缓存中是否存在数据
51         protected void btnSearch_Click(object sender, EventArgs e)
52         {
53             if (Cache["key"] == null)
54             {
55                 lbNo.Text = "缓存中不存在任何内容";
56             }
57             else
58             {
59                 lbHas.Text = "缓存中部包含任何数据";
60             }
61         }
62 
63         //删除缓存中的数据
64         protected void btnMove_Click(object sender, EventArgs e)
65         {
66             if (Cache["key"] == null)
67             {
68               
69                 lbNo.Text = "缓存中没有数据,删除失效";
70             }
71             else
72             {
73                 //删除缓存数据
74                 Cache.Remove("key");
75                 lbHas.Text = "缓存删除成功";
76             }
77         }
78 }

上面只是页面数据缓存(即应用程序数据缓存)的一个比较简单的应用!
实际上它是将数据保存在客户端浏览器的缓存中.如果客户端内存比较充裕的话该cache可能永远存在.

原文地址:https://www.cnblogs.com/fjsnail/p/2764941.html