惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
Cloudbric
Cloudbric

博客园 - Lavenders

Java 正则表达式 量词 --- 三种匹配模式【贪婪型、勉强型、占有型】 信号量与PV操作 java的(PO,VO,TO,BO,DAO,POJO)解释 使用Application对象简单完成网站总访问人数的统计 更新记录后关闭子窗口并刷新父窗口的Javascript jquery jqPlot API 中文使用教程 一款基于bootstrap的datetimepicker ASP.NET下CKEditor3.6.4结合CKFinder2.3实现文本编辑器的上传功能 VS2010创建Web Service程序 【原创】VS2010中水晶报表与VS2008水晶报表版本冲突问题 【转】XSL和XSLT jQuery历史版本 【转】让Iframe自适应高度 【转】框架集中framespacing、border和frameborder属性的关系 【转】Meta标签详解 ASP.NET保存信息总结(Application、Session、Cookie、ViewState和Cache等) . 【原创】VS2008 Web应用程序带数据库制作安装程序 asp.net中的缓存(三)应用程序数据缓存 asp.net中的缓存(一)页面输出缓存
asp.net中的缓存(二)局部数据缓存
Lavenders · 2011-11-04 · via 博客园 - Lavenders

局部数据缓存

有时我们需要的功能不是缓存全部的页面,而是缓存页面上的某一个部分,其余部分是动态的,这时,就需要用到页面局部缓存,该缓存包括两个部分:控件缓存替换后缓存

      控件缓存(片段缓存)

      本缓存是之前提到过的,类似于页面输出缓存那样,建立一个用户控件,将需要缓存的信息写在控件内,并标记其为可缓存的,这样就缓存了控件内的信息,但是并没有缓存整个页面,这样就达到了页面局部缓存的目的。这种缓存比较适合页面的信息有较多的动态信息,少部分的静态信息,这样的情况可用页面局部缓存。

      声明方式也很简单,先建立一个用户控件(ascx),并且在其内部声明为可缓存的,如下所示:

可缓存的用户控件
<%@ Control Language="C#"  CodeBehind="ControlCache1.ascx.cs" Inherits="TestWebCache.UserControls.ControlCache1" %>
<%@ OutputCache Duration="60" VaryByParam="none" %>

<asp:Literal ID="lblControlContent" runat="server" />

      用户控件的后台代码为获取当前时间。

      再建立一个aspx页面来调用该控件,并且在页面上也方式一个Literal,来获取当前时间,以此产生对比的效果,页面前台如下所示:

控件缓存 - 前台代码

 1 <%@ Page Language="C#"  CodeBehind="TestPartCache1.aspx.cs" Inherits="TestWebCache.TestPartCache1" %>
 2 <%@ Register Src="UserControls/ControlCache1.ascx" TagName="ControlCache1" TagPrefix="uc1" %>
 3 
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5 
 6 <html xmlns="http://www.w3.org/1999/xhtml" >
 7 <head runat="server">
 8     <title>测试页面局部缓存 - 控件缓存</title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         控件时间:<uc1:ControlCache1 ID="ControlCache1_1" runat="server" />
13         <p></p>
14         页面时间:<asp:Literal ID="lblPageContent" runat="server" />
15     </form>
16 </body>
17 </html>

      浏览该页面,在每次刷新的时候可以看到,控件的时间每60秒才变化一次,而页面时间是伴随着每次刷新操作都在改变,这样就达到了页面局部缓存的目的。

      替换后缓存

      本缓存与上述的控件缓存正好相反,它是对整个页面除部分动态数据之外的部分进行缓存。有时,我们需要对整个aspx页面进行缓存,但是在这个页面中,又有部分数据需要和数据库实时交互的,其余的部分则是一成不变(或者很少变化)的静态数据,这时,就可以使用替换后缓存。

      在ASP.NET 2.0中,替换后缓存主要有三种方式:以声明方式使用Substitution控件、以编程方式使用Substitution控件API、以隐式方式使用控件。

      可以看到,前两种方法都是以Substitution控件为核心的,那么,何为Substitution控件呢?

      Substitution控件能够指定页面输出缓存中需要以动态内容替换该控件的部分,即:允许对整个页面进行输出缓存,然后使用该控件指出页面中不用缓存的部分。页面中可缓存的区域在和数据库交互完一次之后,直到缓存过期或被清除,才会再和数据库进行交互,而动态区域(即:Substitution控件所指定的区域)则每次收到请求时,都要和数据库进行交互。

      Substitution控件的使用

      先将页面设置成输出缓存,然后再在页面上放置该控件,即可实现替换后缓存,实例代码如下所示:

替换后缓存 - 前台代码

 1 <%@ Page Language="C#"  CodeBehind="TestPartCache2.aspx.cs" Inherits="TestWebCache.TestPartCache2" %>
 2 <%@ OutputCache Duration="60" VaryByParam="none" %>
 3 
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5 
 6 <html xmlns="http://www.w3.org/1999/xhtml" >
 7 <head runat="server">
 8     <title>测试页面局部缓存 - 替换后缓存</title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         页面缓存时间:<asp:Literal ID="lblPageContent" runat="server" />
13         <p></p>
14         替换后的时间:<asp:Substitution ID="Substitution1" runat="server"
15                            MethodName
="GetDateTime" />
16     </form>
17 </body>
18 </html>

替换后缓存 - 后台代码

 

1 protected void Page_Load(object sender, EventArgs e)
 2 {
 3     if (!IsPostBack)
 4     {
 5         lblPageContent.Text = DateTime.Now.ToString();
 6     }
 7 }
 8 
 9 /// <summary>
10 /// 获取系统当前时间
11 /// 必须是静态方法
12 /// </summary>
13 /// <param name="context">必须接受此类型的参数</param>
14 /// <returns>必须返回字符串</returns>
15 protected static string GetDateTime(HttpContext context)
16 {
17     return DateTime.Now.ToString();
18 }

      根据上面的代码,可以看出,Substitution控件有一个很重要的属性:MethodName。该属性就是设置当前Substitution控件执行时所调用的方法名,不过此方法相当特殊,有三个条件必须满足(代码中注释写了的):

      1、此方法必须为静态的方法(private、protected、public 均可)

      2、此方法必须接受 HttpContext 类型的参数

      3、此方法必须返回 string 类型的值

      在页面请求时,Substitution控件会自动执行MethodName所指定的后台方法,而其返回值则直接显示在页面上。