Home > ASP.NET, Development > Application Caching

Application Caching


ASP.NET offers several forms of caching. They are output caching, HttpRuntime.Cache and finally Enterprise Library Cache. Each of these aims to meet the requirements of different scenarios.

This article shall explore the scenarios which each of these best fit.

Output caching

There are 2 variants of output caching, you can either cache the whole page or portions of the page (these portions must be in the form of user controls)

This is commonly used when the steps to reproduce the page is huge (e.g large database calls, long file read times, huge processing requirements), but the output does not change frequently, e.g a FAQ page listing all the FAQs and employing javascript for inline fuzzy search (employing different scripts for different browsers)

When you use output caching, the output is cached for a specific duration e.g 120 seconds and can be set to vary based on different parameters e.g querystring, browser. In the previous scenario of the FAQ page, you can use the VaryByCustom=”browser” parameter to cache and vary the output based on different browsers.

MSDN on Output Caching


HttpRuntime.Cache is used to store serializable objects in a cache for future use. This cache is more powerful compared to output caching, but using it requires more work. This is frequently used to cache the return data in business layers.

If you do not wish to bother about the technicalities behind this cache, you can easily add to it via the following code

HttpRuntime.Cache[“key”] = value

This will add the item to the cache with default values for all parameters. Alternatively you can look at the complete  declaration for adding an item to the cache

public Object Add(
string key,
Object value,
CacheDependency dependencies,
DateTime absoluteExpiration,
TimeSpan slidingExpiration,
CacheItemPriority priority,
CacheItemRemovedCallback onRemoveCallback

This allows you to add a serializable object to the cache with

  • Dependencies: This indicates that this cache item is dependent on another object (usually a file), so when the dependency is changed, the cache object is automatically removed from the cache.
  • Absolute Expiration: This indicates that the cache item will expire after a fixed amount of time. Once the object expires, it is removed from the cache
  • Sliding Expiration: This tells the cache to extend the cache item a fixed amount of time each time the cache item is accessed. Once this amount of time is reached and the cached item is not accessed, it will expire
  • Priority: This tells the cache the priority of the cache item. This comes into effect when the cache is full and items needs to be removed. Items with a lower priorities are removed first.
  • CacheItemRemovedCallback: This is a delegate function which is run when the item is removed from the cache, the key, value and reason for removal is sent to this function so that you can do the appropriate processing.

MSDN on HttpRuntime.Cache

Enterprise Library Cache

There are 5 main differences between this and HttpRuntime.Cache. If your needs meet any one of these points, then it is recommended that you use Enterprise Library Cache.

  1. The cache is stored independently of the web server, so even if you were to do an iisreset, the cache still remains
  2. You can configure the cache to be stored in a persistent backing store e.g SQL Server
  3. There can be multiple caches serving different needs in the same web application
  4. You need more control over expiration policies
  5. You do not need to know when the item expires, but you want to be able to reload the item if required

MSDN on Enterprise Library Caching Block


  1. Output caching: Used to store the page output so that ASP.NET does not need to render the content again
  2. HttpRuntime.Cache: Used by your web application to store items in a cache, the items will disappear when the website is reset
  3. Enterprise Library Cache: Separate cache mechanism which has its own memory and data store, is not affected by server restarts (if using persistent backing store)


  1. All of these caches is restricted to application domain scope, i.e you cannot share them across different application domains (e.g in web garden or multi server environment)
Categories: ASP.NET, Development
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: