Skip navigation

The ASP.NET Cache is the old faithful Application object you may know from classic ASP, just done better. Done better here means various things. First and foremost, the Cache object can get rid of some contained objects if the amount of memory exceeds a certain threshold. This marks a key difference with Applicationan object still supported in ASP.NET. Any element stored in Application can only be removed manually by nulling the corresponding entry. This technique is still available in ASP.NET but it is now partnered with automatic clean-up (in case of necessity), and items expiration policies.

An expiration policy determines when, and under which conditions, the item is removed. Examples are absolute and sliding expiration and cache dependencies. Absolute expiration indicates the absolute time at which a given item will be removed. Sliding expiration associates a timeout to a cached element; if the element is not accessed for the specified duration, itll be then removed. The timeout is silently renewed if the element is read or written meanwhile. Finally, cache dependencies let you associate items with other items in the cache or disk files. Any change in the bound object will cause the item to be automatically removed.

In addition to these removal techniques, the Cache object supports a Remove method wit the following prototype:

Public Function Remove(ByVal key As String) As Object

You pass the name of the element to remove and obtain the live instance of the removed object. If the key is not found, the method returns null.

The ASP.NET cache is made of two distinct and completely separated partsone is private to the ASP.NET runtime and inaccessible to programmers; one is public and available to users code. The public part of the cache is accessible through the Cache object. The Cache object is declared as follows:

NotInheritable Public Class Cache : Implements IEnumerable

As you can see, it is an enumerable object, but it is not a collection class. It doesnt supply, therefore, a Clear method that might be useful in all those applications with lots of entries that require to be refreshed periodically. Heres how to write a Clear method to free up all the public contents of the ASP.NET cache.

Sub Clear()

    For Each elem As DictionaryEntry in Cache



End Sub

When you iterate on the public cache, you really work on a copy of the dataa hashtablethat is obtained locking the Cache and enumerating its contents. For this reason, theres no need to lock the cache for the preceding loop. And theres no guarantee either that the cache is really empty when youre done. The underlying Cache isnt really locked and concurrent requests can modify it. This is by design and to privilege in terms of performance the most common uses of the Cache object. 

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.