Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
Caching Options in ASP

Caching Options in ASP

Ratings:

5.0

(1)
|Views: 579|Likes:
Published by sony

More info:

Published by: sony on May 28, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

06/16/2009

pdf

text

original

 
Caching Options in ASP.NET
ASP.NET supports three types of caching for Web-based applications:
Page Level Caching (called Output Caching)
Page Fragment Caching (often called Partial-Page Output Caching)
Programmatic or Data CachingWe'll look at each of these options, including how, and when, to use each option to increaseyour site's performance!
Output Caching
Page level, or output caching, caches the HTML output of dynamic requests to ASP.NET Webpages. The way ASP.NET implements this (roughly) is through an Output Cache engine. Eachtime an incoming ASP.NET page request comes in, this engine checks to see if the page beingrequested has a cached output entry. If it does, this cached HTML is sent as a response;otherwise, the page is dynamically rendered, it's output is stored in the Output Cache engine.Output Caching is particularly useful when you have very static pages. For example, thearticles here on 4GuysFromRolla.com are very static. The only dynamic content is the banners,the dynamic selection being performed on a separate ad server. Hence, the articles on 4Guyswould be prime candidates for Output Caching.Output caching is easy to implement. By simply using the
@OuputCache
page directive,ASP.NET Web pages can take advantage of this powerful technique. The syntax looks like this:
<%@OutputCache Duration="60" VaryByParam="none" %>
Duration
parameter specifies how long, in seconds, the HTML output of the Web pageshould be held in the cache. When the duration expires, the cache becomes invalid and, withthe next visit, the cached content is flushed, the ASP.NET Web page's HTML dynamicallygenerated, and the cache repopulated with this HTML. The
VaryByParam
parameter is used toindicate whether any GET (QueryString) or POST (via a form submit with
method="POST"
)parameters should be used in varying what gets cached. In other words, multiple versions of apage can be cached if the output used to generate the page is different for different valuespassed in via either a GET or POST.The
VaryByParam
is a useful setting that can be used to cache different "views" of a dynamicpage whose content is generated by GET or POST values. For example, you may have anASP.NET Web page that reads in a Part number from the QueryString and displays informationabout a particular widget whose part number matches the QueryString Part number. Imaginefor a moment that Output Caching ignored the QueryString parameters altogether (which youcan do by setting
VaryByParam="none"
). If the first user visited the page with QueryString
/ProductInfo.aspx?PartNo=4
, she would see information out widget #4. The HTML forthis page would be cached. The next user now visits and wished to see information on widget#8, a la
/ProductInfo.aspx?PartNo=8
. If 
VaryByParam
is set to
VaryByParam="none"
,the Output Caching engine will assume that the requests to the two pages are synonymous,and return the cached HTML for widget #4 to the person wishing to see widget #8! To solvefor this problem, you can specify that the Output Caching engine should vary its caches basedon the
PartNo
parameter by either specifying it explicitly, like
VaryByParam="PartNo"
, orby saying to vary on
all 
GET/POST parameters, like:
VaryByParam="*"
.
 
Partial-Page Output Caching
More often than not, it is impractical to cache entire pages. For example, you may have somecontent on your page that is fairly static, such as a listing of current inventory, but you mayhave other information, such as the user's shopping cart, or the current stock price of thecompany, that you wish to not be cached at all. Since Output Caching caches the HTML of the
entire
ASP.NET Web page, clearly Output Caching cannot be used for these scenarios: enterPartial-Page Output Caching.Partial-Page Output Caching, or page fragment caching, allows specific regions of pages to becached. ASP.NET provides a way to take advantage of this powerful technique, requiring thatthe part(s) of the page you wish to have cached appear in a User Control. One way to specifythat the contents of a User Control should be cached is to supply an
OutputCache
directive atthe top of the User Control. That's it! The content inside the User Control will now be cachedfor the specified period, while the ASP.NET Web page that contains the User Control willcontinue to serve dynamic content. (Note that for this you should not place an
OutputCache
directive in the ASP.NET Web page that contains the User Control - just inside of the UserControl.)Now that we've tackled Output Caching and Fragment Caching, there is still one more cachingtechnique worth discussing: Data Caching. InPart 2 we'll examine what, exactly, Data Caching is and how you can use it to improve the performance of your ASP.NET Web pages. We'll alsoexamine a really cool, real-world caching demo!InPart 1we looked at how to use Output Caching and Fragement Caching of an ASP.NET Webpage. These two techniques cached either the full HTML output of an ASP.NET Web page, or aportion of the HTML output of an ASP.NET Web page (by caching the HTML output of a UserControl). In this part, we'll examine Data Caching, which is an in-memory cache used forcaching objects.
Data Caching
Sometimes, more control over what gets cached is desired. ASP.NET provides this power andflexibility by providing a cache engine. Programmatic or data caching takes advantage of the.NET Runtime cache engine to store any data or object between responses. That is, you canstore objects into a cache, similar to the storing of objects in Application scope in classic ASP.(As with classic ASP, do
not
store open database connections in the cache!)Realize that this data cache is kept in memory and "lives" as long as the host application does.In other words, when the ASP.NET application using data caching is restarted, the cache isdestroyed and recreated. Data Caching is almost as easy to use as Output Caching orFragment caching: you simply interact with it as you would any simple dictionary object. Tostore a value in the cache, use syntax like this:
Cache["foo"] = bar; // C#Cache("foo") = bar ' VB.NET
 To retrieve a value, simply reverse the syntax like this:
bar = Cache["foo"]; // C#bar = Cache("foo") ' VB.NET
 Note that after you retrieve a cache value in the above manner you should first verify that thecache value is not null prior to doing something with the data. Since Data Caching uses an in-memory cache, there are times when cache elements may need to be evicted. That is, if thereis not enough memory and you attempt to insert something new into the cache, somethingelse has gotta go! The Data Cache engine does all of this scavenging for your behind thescenes, of course. However, don't forget that you should
always
check to ensure that the
 
cache value is there before using it. This is fairly simply to do - just check to ensure that thevalue isn't null/Nothing. If it is, then you need to dynamically retrieve the object and restore itinto the cache.For example, if we were storing a string
myString
in the cache whose value was set fromsome method named
SetStringToSomething()
, and we wanted to read the value of 
myString
from the cache, we'd want to:
1.
Read the
myString
from the cache:
str = Cache("myString")
 
2.
Ensure that
str
wasn't null/Nothing. If it was, we'd want to get the value of 
str
from
SetStringToSomething()
, and then put it in the cache, like so:
'Try to read the cache entry MyString into strstr = Cache("myString")'Check if str is NothingIf str is Nothing then'If it is, populate str from SetStringToSomething()str = SetStringToSomething() 'Now insert str into the cache entry myStringCache("myString") = strEnd If
Besides using the dictionary-like key/value assignment, as shown in the example above, youcan also use the
Insert
or
Add
method to add items to the cache. Both of these methods areoverloaded to accommodate a variety of situations. The
Add
and the
Insert
methods operateexactly the same except the
Add
method returns a reference to the object being inserted tothe cache. Because of the similarity of the two methods, I will concentrate on the
Insert
method. Note that the
Insert
method allows you to simply add items to the cache using akey and value notation as well. For example to simply add an instance of the object
bar
to thecache named
foo
, use syntax like this:
Cache.Insert("foo", bar); // C#Cache.Insert("foo", bar) ' VB.NET
 (Note that this is synonymous to using the
Cache("foo") = bar
syntax we looked atearlier.)Note that with inserting items into the Data Cache using the
Cache(
key 
) =
value
methodor the
Cache.Insert(
key 
,
value
)
we have no control over when (if ever) the items areevicted from the cache. However, there are times when we'd like to have control over whenitems leave the cache. For example, perhaps we want to have an inserted item in the cache toonly live for
n
seconds, as with Output Caching. Or perhaps we'd like to have it exit the cache
n
seconds after it's last accessed. With Data Caching, you can optionally specify when thecache should have a member evicted.Additionally, you can have an item evicted from the cache when a file changes. Such aneviction dependency is called a file dependency, and has many real-world applications,especially when working with XML files. For example, if you want to pull data out of an XMLfile, but you don't want to constantly go to disk to read the data, you can tell the ASP.NETcaching engine to expire the cached XML file whenever the XML file on disk is changed. To dothis, use the following syntax:

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->