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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - venjiang

发布Asp.Net Forums V2.2.1929 官方中文Beta版 转换DotNetNuke为C#版 Asp.Net Forums 2.0.1 内部开发版系统文件说明 发布Asp.Net Forums V2 中文官方 10.1 国庆版 asp.net forums2 本地化版本0831发布(开源) Asp.Net Forums2 本地化工作记录0831 获取的本地区域为何不变?搞得我头都大了! Asp.Net Forums2组件库简要说明 [转贴]IIS6.0服务器无法访问解决方案总结 测试你的Blog价值 Data Access Application Block V2 类库中文文档 Data Access Application Block 3.1 博客园LOGO[修改版] DotNet开发人员现在应该下载的十种必备工具下载说明 模拟Asp.Net Forums 2.0 数据提供者类 卸载Windows Messenger 和高手过招 解决Automation 服务器不能创建对象 [Asp.Net Forums 2.0]增加Pager控件没有的跳转页功能
Caching ASP.NET pages
venjiang · 2004-07-30 · via 博客园 - venjiang

Want a faster ASP.NET application? Try this tutorial from Tony Patton that will show you how to cache your pages.

The most glaring difference between Web and stand-alone applications is the disconnected nature of the Web. That is, a Web application isn't constantly connected (to a database server, application server, etc.), and a stand-alone application is. The constant server calls by the Web application can degrade performance. One way to circumvent the server calls is through caching, which is relatively simple on ASP.NET.

Where is the data?
Caching is a method for storing data for a specific period of time. In a Web application, the cached data is used as opposed to making another call to the server, which positively affects performance. ASP.NET allows the developer to specify caching on a page-by-page basis, or it may be controlled programmatically. Controlling caching at the page level involves an ASP.NET page directive.

Caching ASP.NET pages
You can use the OutputCache page directive to cache a page and all of its data. The current state of the page is stored when it's used. This means that all contained data is stored.

The directive contains two required attributes: Duration and VaryByParams. The Duration attribute specifies how long (in seconds) the data is cached. The VaryByParams attribute allows a page to be cached by a corresponding HTTP parameter; it also allows multiple versions of a page to be cached. (Multiple values are supported; they should be separated by semicolons.) The directive has the following syntax:

<%@ OutputCache Duration="300" VaryByParams="None" %>

This example will cache the page for 300 seconds without using any parameters. The VaryByParams attribute often confuses beginning ASP.NET developers, but it's really very simple. It can be used to specify an HTTP GET or POST variable as the basis for caching; these values are submitted by HTTP forms (field values), so the VaryByParams attribute allows you to cache a page by data submitted to it. I've seen it frequently used on search pages where it allows the page to be cached according to the search term submitted, so it allows different versions of the page to be cached. The following OutputCache directive caches the corresponding page for 600 seconds (10 minutes) according to the value submitted in the SearchValue field:

<%@ OutputCache Duration="600" VaryByParams="SearchValue" %>

Any different SearchValue field value results in a different cached page. Caching the entire page is a powerful option, but it may not be necessary--you may need to cache only a portion of the page. This may be accomplished with what is called fragment caching. The OutputCache attribute may be tied to an ASP.NET user control as well as an entire page. This allows only the portion of the page contained within the user control to be cached; thus only a page fragment is cached. The OutputCache direction for a user control is the same as the page syntax, so no change is necessary and the VaryByParams attribute is supported as well.

The OutputCache directive contains three additional optional attributes that may be used in conjunction with the previous three:

  • Location: Specifies where the page is cached.
  • VaryByHeader: Allows a page to be cached based on different HTTP headers. Multiple values are supported; they should be separated by semicolons.
  • VaryByCustom: Defines custom criteria for when a page should be loaded from the cache or when it should be regenerated.

The Location attribute has four accepted values:

  • NoCache: Forces proxy server to revalidate the request with the originating Web server.
  • Private: Specifies page is cacheable only on the client. This is the default value.
  • Public: Specifies page may be cached only by both client and proxy servers.
  • Server: Specifies page may be cached only at the originating Web server.

The VaryByHeader attribute can be useful if varying HTTP headers are causing problems in an application. One good example is the support of different browsers; this may be used as the basis for caching by using the VaryByHeader attribute and the User-Agent HTTP variable:

<%@ OutputCache Duration="600" VaryByParam="none" VaryByHeader="User-Agent" %>

This creates a cached version for each different client that requests the page. The last attribute, VaryByCustom, allows the developer to choose when requested content should be loaded from cache. This is accomplished by overriding the GetVaryByCustomString method inside Global.asax. One way I've seen this method used is for caching pages according to a user's session id. The OuputCache directive appears first:

<%@ OutputCache Duration="600" VaryByParam="None" VaryByCustom="SessionID" %>

This is utilised by inserting the code in the Global.asax file. The session id is stored as a cookie, so this is used to retrieve the value for the VaryByCustom attribute. The following C# code shows how this is accomplished:

public override string GetVaryByCustomString (HttpContext context, string arg){
if (arg.ToLower () == "sessionid") {
HttpCookie cookie = context.Request.Cookies["ASP.NET_SessionId"];
if (cookie != null)
return cookie.Value;
}
return base.GetVaryByCustomString (context, arg);
}

You decide what to cache
Caching can increase user response time, but it also consumes memory so be careful not to cache too much. This article just scratches the surface of caching; the other side of the coin involves controlling caching from within your ASP.NET code (C#, VB.NET, etc.).