In ASP.NET you can cache data from your applications, parts of the website and event the whole website that is generated. The main advantage of cache is that it decreases time needed to display your application in client's web browser.
Cache is a mechanism that allows you to store data in server's memory, so after first use there is no need to prepare all content again. In ASP.NET you can cache data from your applications, parts of the website and event the whole website that is generated. The main advantage of cache is that it decreases time needed to display your application in client's web browser.
There are two different approaches to caching in ASP.NET. First one is application cache where you can store and retrieve data using key and value pattern. Second approach is page output cache where you save ready HTML code of a whole page or just a fragment of it.
Application cache
Application cache is used when you want to store objects in memory that are used many times in your application. Instead of creating the same objects few times you can save one in cache and use it from each of your pages.
Using the is cache is very easy, in the beginning I'll show you how to store a simple string in cache and then how to retrieve it. As I mentioned before objects are saved in application cache using key and value pairs. That means that you store a value using a concrete key and then to retrieve it you just have to use the same key.
Remember that you can never be sure if an object is still in cache because it could have been deleted even if you just have saved it. Data from cache is sometimes deleted when there is not enough space, I'll describe this process later in details. The general rule is to retrieve data from cache and check if it's not null. If it is than we have to save this data to cache again.
I'll show you this on an example, in the beginning let's create a new web application. Now go to the design mode and drag and drop one button from toolbox to a website. Double click this button to create a method that will be called after pressing this button. In order to analyze the progression of our program we'll use Debug.WriteLine() messages. In order to use them add following directive in the top of the cs file:
Then let's add the following piece of code to the method that is called after clicking our button:
Now let's run this application and click the button twice. The result in the debug output will be the following:
As you can see the first time the button is clicked the cache is empty so we have to save data to it, however the second time we only have to retrieve data that is already stored.
Storing other more complicated objects in cache is equally easy as storing a string so you shouldn't have problems with doing it. Just remember to use the pattern to retrieve data from cache and check if it's still there.
Page output cache
As I mentioned before this type of caching saves rendered html of a whole webpage or of just one component and then sends it to client saving time necessary to prepare source code of webpage.
To cache the whole webpage you just have to use following directive in the beginning of your webpage:
Duration parameter is required and it is simply a time in seconds for how long a concrete page will be stored in cache. After 10 seconds it will be deleted and the new version will be stored again for the same amount of time.
VaryByParam attribute decides if the cached page varies for different parameters in query strings. For instance VaryByParam="name" for two different addresses like: /Default?name=John and /Default?name=Scott will result in caching two different versions of webpage. On the other hand VarByParam="none"would result in caching only one version of the webpage.
VaryByControl attribute is used when you want to have different versions of page stored in cache for different values of a specific control such as ComboBox for instance. Underneath you can see an example of this parameter:
VaryByCustom attribute with browser value will cache different versions of webpage for different web browsers and even for different versions of these web browsers.
Please note that in the example of VaryByControl attribute there is no VaryByParam. It is cause by the fact that either of these two attributes is required.
In ASP.NET there is also a mechanism called extensible output caching. It allows you to store your data in various output cache providers, such as discs or clouds for instance. However this technique is out of the scope of this tutorial so I won't cover it there.
Cleaning cache
Cache has to be cleaned from time to time in order to ensure that there is a place to store new items. Data from cache can also be removed when the time when the item is valid has expired or when dependencies have changed. Dependency can be for example a database - then when there were some changes in database the item that depends on this database is removed from cache.
There are two mechanisms that clear data stored in cache:
- Scavenging - when there is not enough free space in cache ASP.NET looks for items which haven't been recently used and for items with low priority. Then it deletes selected items and stores new items in cache.
- Expiring - data for which time when they are valid has passed is also removed. Expiration times can be absolute - when data is valid for a specific period of time and they can also be so called "sliding" when time is counted always from the beginning from the last use.
Closing remarks
As you can see in this tutorial using cache in ASP.NET is relatively easy and it can considerably improve the speed of your website. You should especially use it when pages in your web applications are loaded repeatedly or when various pages use the same objects or controls. There are many different options for caching so you can make sure that data that is needed the most in your application will be in cache as long as necessary.