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.
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)
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.
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(
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.
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.
- The cache is stored independently of the web server, so even if you were to do an iisreset, the cache still remains
- You can configure the cache to be stored in a persistent backing store e.g SQL Server
- There can be multiple caches serving different needs in the same web application
- You need more control over expiration policies
- You do not need to know when the item expires, but you want to be able to reload the item if required
- Output caching: Used to store the page output so that ASP.NET does not need to render the content again
- HttpRuntime.Cache: Used by your web application to store items in a cache, the items will disappear when the website is reset
- 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)
- 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)