WebCache Class

Provides a cache to store frequently accessed data.

Inheritance Hierarchy


Namespace:  System.Web.Helpers
Assembly:  System.Web.Helpers (in System.Web.Helpers.dll)


Public NotInheritable Class WebCache
public static class WebCache
public ref class WebCache abstract sealed
type WebCache =  class end
public final class WebCache

The WebCache type exposes the following members.


  Name Description
Public methodStatic member Get Retrieves the specified item from the WebCache object.
Public methodStatic member Remove Removes the specified item from the WebCache object.
Public methodStatic member Set Inserts an item into the WebCache object.



This class represents a helper, which is a component that simplifies web programming in ASP.NET Web Pages. Use the WebCache class to cache frequently accessed data, which can improve website performance. Information that is often accessed but rarely changed is a good candidate for caching. For example, you might cache a list of phone area codes or postal codes. Or you might cache weather forecast information that is unique to the region of a user, and then update the cached forecast data at set intervals. In a web page, you can retrieve cached items instead of fetching the information again from its source each time the page is requested. The data that you add to the cache can be any type of object, including complex, nested data objects that are returned from database queries, arrays of values, or simple objects such as a number, a time stamp, or a string.

Items that you put into the cache are removed automatically. Typically, you specify a timeout as an interval (by default, 20 minutes) after which the item expires and is no longer cached. However, items can also be removed before the timeout period if the server runs low on memory or for other reasons. (For example, the web server is restarted, the cache is cleared.) In general, you should never count on an item that you have cached to be in the cache; when you get an item out of the cache, make sure you have actually gotten the item and not null reference (Nothing in Visual Basic). If necessary, re-fetch the item and put it into the cache again.

You can use the following WebCache class methods to create, retrieve, and remove data from the cache:

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also


System.Web.Helpers Namespace

Other Resources

ASP.NET Caching

Caching to Improve the Performance of Your Web Site