What is ASP.NET caching mechanism? When to use caching? and how can I use it in order to improve my site performance? are the questions this post series is going to answer. In today’s post I’ll introduce the output cache. You can read the previous posts in the subject:
- Improving performance by using ASP.NET caching
- Improving performance by using ASP.NET caching – application caching
What is Output Cache?
Output cache is a mechanism that keep a copy of a rendered ASP.NET web page in memory. This behavior helps to improve performance by returning a response of the cached web page instantly and by reducing the need to render the page in every client request. If a page takes a lot of time to render using output cache can significantly improve the performance. Even though, the output cache has a very big drawback. If your site needs to enable customization for users or needs to show dynamic information you don’t want your users to have the same version of the page. ASP.NET supports configuration options to avoid this drawback with the output cache making it very flexible and powerful.
Configure Output Cache for a Single Page in Declarative Way you can control the output cache’s behavior in a declarative way by using the @OutputCache page directive. The only required parameter for the output cache is the Duration all the other parameters can be used if you want to. Lets describe some of the parameters:
- Duration – the duration in seconds for the web page to be cached.
- Location – the location to save the cached object. You can save the cached object on the server, client, both or all the other options in the OutputCacheLocation enum.
- VaryBy* (where * is in the set of [Param/Header/Custom/Control]) - The most useful parameters in the parameters list is the VaryBy parameters. With these parameters you indicate semicolon-separated list of strings to vary the output cache. These strings can be parameters if query string (Param), ID property values of ASP.NET server controls that where declared in a user contorl (Control) and etc. These parameters are used to vary caching of page/control by the given parameters therefore enabling the caching of the same page/control with different behaviors.
- SqlDependency – a string value that identifies a set of database and table name pairs that the page or control’s output cache depends on. Every update of items in the table will result in the removal of the items from the cache when using table based polling.
Lets look at an example of the declarative way:
<%@ OutputCache Duration="30" Location="Client" VaryByParam="PageNumber;Count" %>
In the example the output cache will last for 30 seconds, will be saved on the client side and will vary the the PageNumber and Count query string parameters.
Configure Output Cache for a Single Page in Runtime
We can use the declarative way but sometimes we want to use caching in runtime. The output cache can be configured in runtime using the Response.Cache property. In the programmatic way there are few methods to use than in the declarative way. The available methods are:
- SetExpires – indicates the number of seconds that the page will be cached.
- SetCacheability – uses an enum to indicate where the cache will be stored.
- SetValidUntilExpires – if the method get true as parameter the cache ignores
Invalidating Cached Pages
There are a lot of reasons to invalidate a cached page such as items that where inserted or updated in a database, file changes which the page use or etc. When we need to invalidate a cached page there are some ways to do it. Lets explore the ways:
Determine whether to use a cached page – in order to determine whether to use a cached page you need to respond to the ValidateCacheOutput event. An example of how to do it can be:
protected void ValidateCacheOutput(HttpContext context, object data, ref HttpValidationStatus status)
string pageNumber = context.Request.QueryString["PageNumber"];
status = HttpValidationStatus.Valid;
status = HttpValidationStatus.Invalid;
protected void Page_Load(object sender, EventArgs e)
Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(ValidateCacheOutput), null);
What you see in the example is the use of a AddValidationCallback which is a part of the Response’s Cache object. ASP.NET uses this callback method to determine whether to use the cached version of the page or not according to the HttpValidationStatus enum. In the example I provided if there is no pageNumber in the query string the cache is valid else the cache isn’t valid. This example is very simple and I won’t recommend you to use it but it shows the concept of how to use the ValidatecacheOutput callback. Pay attention to register the AddValidationCallback in the Page_Load event and not after it.
Create output dependency for cache – in the Response there are methods that help to create a cache page output dependency. These methods are AddCacheDependency, AddCacheItemDependency, AddCacheItemDependencies, AddFileDependency and AddFileDependencies.
Configuring Caching in Web Configuration File
In the previous section I described how to use output cache programmatically but sometimes you want to use configurations. In the configuration file we create cache profiles that the pages of the application can use with the @OutputCache page directive. This can be done by using the caching element under the system.web element of the web.config file. In the caching element we add cache profiles which can be used in the application easily. The next example shows how to build a cache profile:
<add name="profile" duration="30" enabled="true" varyByParam="pageNumber"/>
We use the profile like this in the page:
<%@ OutputCache Duration="30" CacheProfile="profile" %>
Other things that you can configure are: sqlCacheDependency, outputCache for the entire appliaction and cache policy for the entire application.
Lets sum up the post, in the post I showed ways to use the output cache which is very powerful tool to improve performance. As you could see in the post there are a lot of ways to use the output cache either programmatically or declarative. The use of the output cache is easy but you should use it very carefully. There will be no mercy from the application users if the pages of the web application will become static because you used the output cache poorly. I suggest to learn output cache and it’s consequences very well before using it. This is the last post in the series of how to use ASP.NET caching.