Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


SSWUG

Reader rating:
Click here to read 1 comment about this article.
Article source: CoDe (2002 - May/June)


Article Pages: < Previous - 1  2  3 4 - Next >


ASP.NET Caching Strategies (Cont.)

The Namespaces (System.Web and System.Web.Caching)

There are two namespaces containing ASP.NET caching classes: System.Web and System.Web.Caching. Caching-related classes found inside System.Web are used to cache Web pages and portions of Web pages. The System.Web.Caching class provides developers direct access to the cache, which is useful for putting application data into the cache, removing items from the cache and responding to cache-related events. Table 1 provides a quick reference to the various classes used in ASP.NET caching strategies.

Cacheability (Cache on the Client/Server)

Before a page can be cached, you must indicate its "cacheability." Setting this parameter indicates the devices on which your page can be cached. Valid devices include the client making the request, the server fulfilling the request, proxy servers, etc. To indicate a page's cacheability, you use the following method call at the top of your page:

Page.Response.Cache.SetCacheability( _
  HttpCacheability.Server)

The SetCacheability method takes a member of the HttpCacheability enumeration as its only parameter. The members of this enumeration are designed to allow you to control where your page is cached. Table 2 presents the details for each enumeration member.

Version Caching (Versions of Pages)

Dynamically generated pages are typically created based on a set of parameters sent either to the page in the form of querystring parameters or posted using form fields and a submit button. ASP.NET makes it easy to cache these pages as well. The key to caching these pages is to cache versions of the page. That is, one version of the page for each value for a given parameter (or set of parameters).

"
The duration parameter in the example code represents the number of seconds the page should remain in the output cache before being refreshed.
"

For example, suppose our hypothetical regional sales report allows users to filter the data they view by month. Imagine the month parameter is passed to the page via the querystring. If we indicate that the page is cached, but do not cache based on this parameter, then only calls to the page without the querystring parameter will be cached. Each call using the querystring will override the cache and cause the page to execute its code rather than pull from cache. Of course this is not the caching behavior you want. Instead, each call to the page with a new month should result in a new page added to the cache as its own separate item. This way, subsequent requests for the same month can be delivered from the cache.

.NET makes version caching rather simple via the HttpCacheVaryByParams class. This class is accessed by a property exposed by Response.Cache. For example, the following code indicates that the page should cache versions of itself based on the Month querystring parameter.

Page.Response.Cache.VaryByParams( _
  "Month") = True

You'll be happy to know that the same line of code works for parameters posted to the page. For instance, if you submit the month data to the page based on a user form field (text box, dropdown, etc.) with the same name of "month," then the same line of code results in multiple cached versions of the page.

Of course, pages are often rendered based on more than one parameter. For instance, both month and region might be parameters in our example. Each combination of month and region should result in a separate page being added to the cache. You can indicate a group of parameters on which to base caching for a given page using the same VaryByParams property. To do so, you simply separate each parameter with a semicolon, for example:

Page.Response.Cache.VaryByParams( _
  "Month;Region") = True

You can also indicate that a page should be added to the cache based on all its parameters. To do this, you use an asterisk (*) as the VaryByParam value.

&


Code-Behind Classes

In this article, we talk a lot about implementing caching policies procedurally inside of your 'code-behind' classes. Code-behind classes are a new concept introduced with ASP.NET. Put simply, these are classes programmatically tied to an ASP page. The beauty of code-behind classes is that, for the first time, it is possible to physically separate code responsible for presentation and logic, without ever leaving the ASP.NET design environment. In other words, HTML and script developers are free to set the "look and feel" of the page by dealing solely with the .aspx page, while VB/C# developers can concentrate on the core logic by working with the code-behind class. The code becomes much more readable using this method of programming - no more in-line code scattered throughout the HTML - and it allows developers and designers to work side-by-side without trampling on each others' code base. One tip to remember: To see the code-behind files in Visual Studio, you may have to click the Show All Files button at the top of the Solution Explorer pane.



Table 1 - Caching-Related Classes Reference
Namespace.ClassSystem.Web.HttpCachePolicy
Primary UseThe HttpCachePolicy class is the primary class used to indicate page-level caching. Using this class, you can set information like cache expiration date, variables for versions of cached pages, and more.
Example Call(s)Page.Response.Cache.SetExpires(date:=Now.AddMinutes(5))


Namespace.ClassSystem.Web.HttpCacheVaryByParams
Primary UseThe HttpCacheVaryByParams class allows you to indicate different versions of your page to be cached based upon variables passed on a querystring or in a post to your page. This class is used to set the HttpCachePolicy.VaryByParams property.
Example Call(s)Page.Response.Cache.VaryByParams("Region") = true


Namespace.ClassSystem.Web.UI.PartialCachingAttribute
Primary UseThe PartialCachingAttribute class allows you to tag a user control as cached. This is the key to caching only portions of your page.
Example Call(s)Page.Response.Cache.VaryByParams("Region") = true


Namespace.ClassSystem.Web.Caching.Cache
Primary UseThe Cache class exposes the actual cache for your application. This allows you to explicitly add and remove items to and from the cache. It is best used for caching application-level (rather than pages or portions of page) data.
Example Call(s)Cache.Insert(key:="SalesData", value:=salesXml)


Namespace.ClassSystem.Web.Caching.CacheDependency
Primary UseThe CacheDependency class is used to keep track of cached items that are dependent on other objects (such as files). This class is used in conjunction with the Cache.Insert method.
Example Call(s)Cache.Insert(key:="SalesData", value:=salesXml, new _ CacheDependency(salesXmlFileName)


Article Pages: < Previous - 1  2  3 4 - Next Page: 'Fragment Caching (Page Portions / User Controls)' >>

Page 1: ASP.NET Caching Strategies
Page 2: The Namespaces (System.Web and System.Web.Caching)
Page 3: Fragment Caching (Page Portions / User Controls)
Page 4: Caching Dependencies

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.7 out of 5

3 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      AppsWorld Europe

 

SSWUG