Library code snippets

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

Cache.Insert 
(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:

C#

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

VB

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.

C#

//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;
}

VB

'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
Else
    ' 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.

Conclusion

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

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Precise .NET Server Content Caching.

Leave a comment

Sign in or Join us (it's free).

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...

Related podcasts

  • More jQuery in ASP.NET

    In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!