下面详细介绍在《系统缓存全解析一》中提及的动态缓存技术:
一,传统缓存方式:
比如将可重复利用的东西放到Application或是Session中去保存。
Session["Style"] =
val;
Application["Count"] = 0;
二,页面输出缓存:全页缓存到服务器内存中
页面输出缓存是最为简单的缓存机制,该机制将整个ASP.NET页面内容保存在服务器内存中。当用户请求该页面时,系统从内存中输出相关数据,直到缓存数据过期。在这个过程中,缓存内容直接发送给用户,而不必再次经过页面处理生命周期。通常情况下,页面输出缓存对于那些包含不需要经常修改内容的,但需要大量处理才能编译完成的页面特别有用。需要读者注意的是,页面输出缓存是将页面全部内容都保存在内存中,并用于完成客户端请求。
在ASP.NET中页面缓存的使用方法非常的简单,只需要在aspx页的顶部加这样一句声明即可:
<%@ OutputCache
Duration="60" VaryByParam="none" %>
Duration
缓存的时间(秒)。这是必选属性。如果未包含该属性,将出现分析器错误。
1 <%@ Page
2 Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
3 Inherits="CacheWebApp._16_4_3.WebForm1" %>
4
5 <%@ OutputCache
6 Duration="60" VaryByParam="none" %>
7
8 <html
9 xmlns="http://www.w3.org/1999/xhtml" >
10
11 <head
12 runat="server">
13
14 <title>页面缓存示例</title>
15
16 </head>
17
18 <body>
19
20 <form
21 id="form1" runat="server">
22
23 <div>
24
25 <asp:Label
26 ID="Label1" runat="server"
27 Text="Label"></asp:Label>
28
29 </div>
30
31 </form>
32
33 </body>
34
35 </html>
2 Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
3 Inherits="CacheWebApp._16_4_3.WebForm1" %>
4
5 <%@ OutputCache
6 Duration="60" VaryByParam="none" %>
7
8 <html
9 xmlns="http://www.w3.org/1999/xhtml" >
10
11 <head
12 runat="server">
13
14 <title>页面缓存示例</title>
15
16 </head>
17
18 <body>
19
20 <form
21 id="form1" runat="server">
22
23 <div>
24
25 <asp:Label
26 ID="Label1" runat="server"
27 Text="Label"></asp:Label>
28
29 </div>
30
31 </form>
32
33 </body>
34
35 </html>