Caching in .Net - Overview
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Caching in .Net - Overview
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Database Access Class [next Lesson]  FILE I/O
Caching in .Net - Overview
  by: kirk

.NET Caching - So you want to learn about Caching eh?

by: kirk

One of the new features in ASP.NET is a system for caching page and application data, so that you don’t have to perform the same expensive process every time someone views a page.

Page caching: Caching can be performed on a per-page basis, or on a per-user-control basis, letting you store part or whole pages in a cache, and customise their expiration.

Page caching is controlled by the <%@ OutputCache %> directive (search for output caching in MSDN), which has two compulsory parameters: · Duration: The number of seconds to keep the page in the cache · VaryByParam: The query string parameters to use when determining page uniqueness. For example, news articles may have urls like news.aspx?id=2, news.aspx?id=3. In this case, you would want to store a different entry in the cache for news article 2 than for article 3, so you could vary by the “id” parameter.

Object caching: You can insert a value into the Cache object giving it a unique key and set its absolute or relative expiration time.

For example, we may want to cache some xml resulting from a complicated process, and store it for 12 hours. Each time we use the xml we check if it’s still in the cache:


XmlDataDocument x = null;
if (Cache["xmldata"] == null) {
x = ComplexProcess();
    Cache.Insert("xmldata", x, null, DateTime.MaxValue,
TimeSpan.FromHours(12), CacheItemPriority.High, null);
} else {
    x = Cache["xmldata"];
}

Tips: (This section is an abridged version of Jonathon Goodyear’s Visual Studio Magazine article: http://www.fawcette.com/vsm)

Prioritise items in the cache: Because the cache stores items in memory, it may need to boot out some items to free up memory. Each time you insert something into the cache, use the overloaded version of Insert that allows you to indicate how important that item is, using one of the CacheItemPriority enumeration values.

Don’t cache everything: Again, because the cache is in-memory, you need to trade-off whether to regenerate items, or store them in memory.

Single point of maintenance: No-one wants to dig through code to find cache settings, so the usual rule applies here. You’ll find it easier in the long run if you store your cache settings in a central location.


1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Caching in .Net - Overview
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Database Access Class [next Lesson]  FILE I/O


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
goraperas 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center