Alex Mueller on Software and Technology 
Friday, May 20, 2005

Caching in ASP.NET is relatively easy to implement, intuitive to use, and provides good means to conserve server resources by storing data in memory. The interface to caching offers a number of options to help populate cache, retrieve information, set expirations, and choose cache dependencies. You may implement caching by using the @OutputCache directive on your page or control, and program against the HttpCachePolicy class. By default, when system memory starts to become low, the cache will automatically begin removal of items that are not used as frequently. Similar to garbage collection, this process, known as scavenging, is one way cache maintains its integrity and reduces server memory usage.

With multiple ways to add items to cache, it can be confusing to know which one way is the best. The answer relies on your scenario. For example, these 3 lines of code all add items to cache.

Cache["MyData"] = myObject;
Cache.Add("MyData", myObject, null, DateTime.MaxValue, TimeSpan.FromHours(1), Caching.CacheItemPriority.Low, OnRemoveCallback);
Cache.Insert("MyData", myObject, null, DateTime.MaxValue, TimeSpan.FromHours(1));

Each allows more functionality and control over your cache management. For instance, when using the Add method, you can set a delegate to be called when the item is removed from cache to notify your application. With insert and add, you can choose expiration time intervals, sliding expirations, and dependencies. When providing a dependency, cache will remove the item when the object on which it is dependent changes.

In an app of mine, I have made caching configurable in the web.config so administrators can choose to enable, disable, and set the duration of caching. It is pretty painless and it provides those maintaining the site with the ability to adjust the application's use of server resources as they see fit.

Read more on Caching
ASP.NET Caching in 1.1
New ASP.NET 2.0 Caching Features
Friday, May 20, 2005 3:58:22 PM (Mountain Standard Time, UTC-07:00) | Comments [0] | #
Comments are closed.
MuellerDesigns.net
Search
On This Page
PowerShell Documentation
Automate Daily Tasks with PowerShell
SketchPath XPath Editor
Software Testing - Revisited
Architecting Buildings and Software
NBCOlympics.com with Silverlight
Marker Interfaces and C# Attributes
The Phone Screen
Working with ASP.NET MVC and MvcContrib
Thanks to BDD
Twitter
The Opposite of a Singleton?
Removing Duplicate Code in Functions
Add Vista Themes to Longhorn
Changing File Ownership In Vista and Longhorn
Most Popular
JavaScript ReplaceAll Functionality
What is polymorphism?
What is composition?
Sorting with IComparable and IComparer
Applying the Observer Pattern in ASP.NET
MVP in ASP.NET
What is abstraction?
What is encapsulation?
What is a class?
What is inheritance?
Authentication in ASP.NET
Calendar Controls
XPathNavigator.CheckValidity new for 2.0
SQL Server 2005 Connection Issues
Auto-attach to process '[####] aspnet_wp.exe' on m...
What is an object?
FreeTextBox
VMWare and VPC
An Example of Reflection using C#
Caring for the Team
Archive
Links
Categories
My Local Blog Map
Blogroll
About
Powered by:

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009
MuellerDesigns.net

Sign In

Help Those In Need
The Hunger Site
Ronald McDonald House Charities (RMHC) of Western Washington & Alaska