Precise .NET Server Content Caching

Caching in .NET allows you to maintain high-performance pages and scalable applications. Simple Page output caching for just three seconds can dramatically boost page speed. NET is already up to 3x faster than legacy ASP. Now .NET gives you the option for caching server controls or (Fragment caching) and finally offers the powerful Cache API which gives you the ability to programmatically cache any objects whether they are strings or entire datasets, and within a page output or a user control.

This tip is for specifically programming and caching server control content, especially that which will be commonly included in multiple pages (through any type of code-behind server control or even create a custom control) whereby each page would necessitate a distinction in knowing which cache it must read. Caching server controls with the VaryByControl control is usually not recommended as this adds an overhead to performance. Programmatic caching is more precise in allowing you to name your cache and even have it depend on some file on the server. Remember, one of the main objectives of .NET is creating reusable objects.

Therefore, how would you be able via one method, one cache name, and individual page calls create several different cached items and then upon each page loading, delegate each a customized cache? Take for example, suppose you have a series of pages where each page differs from one other based on topic for instance. Now, since each topic page needs to display its own top 10 results, how would you be able to use one server control to read specific data pertaining to each topic, thereby each topic page getting its own cached data? Here's how:

Cache It

When you insert an object into a cache it is in the form of

(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)

- this being the final of four overloaded forms available from the Cache API. For example,

 Cache.Insert ("Cachename", Source, 
Nothing, DateTime.Now.AddDays(1), TimeSpan.Zero)

tells it to create a new cache to quickly expire in 1 day. For more detailed info on Data Caching check out my article - .NET Data Caching.

Here's the quick tip that can make all the difference. When you want to have one cache method for as many pages as you want, again you'll need a distinguishing factor among them. How is the cache going to know that you need to create 5 separate caches all with one Cache Insert method? Simple. Just append any variable or any value to the end of the "CacheName", like so:


Cache.Insert ("Cachename" + Variable, Source, Nothing, DateTime.Now.AddDays(1), TimeSpan.Zero));


Cache.Insert ("Cachename" & Variable, Source, Nothing, DateTime.Now.AddDays(1), TimeSpan.Zero))

Read It

Now after you insert your custom cache object within the one method, here is how you can retrieve that cache based on its certain name value.


//Declare your cache variable
string CustomCache = Cache.Get("CustomCache" + Variable);
if (CustomCache == null){ //Check to see if cache already exists
    //'... Your object gets inserted into the cache
} else {
    //Read your data from the cache
    YourDatasource or DataOutput = CustomCache;


'Declare your cache variable
Dim CustomCache As String = Cache.Get("CustomCache" & Variable)
If IsNothing(CustomCache) Then 'Check to see if cache already exists
    '... Your object gets inserted into the cache
    ' Read your data from the cache
    YourDatasource or DataOutput = CustomCache
End If

and viola each page (rather that one control placed within each page) will read its own cached information, thus encouraging code reusability and customizable, scalable data controls and applications.


So, there you have it a simple way of extending the Cache API and adding even more functionality to your .NET pages.

You might also like...


Dimitrios Markatos Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and deskto...


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“To iterate is human, to recurse divine” - L. Peter Deutsch