One of the biggest concerns for developers these days is how to increase the performance of delivered applications. User expectations have changed over the past few years and the expectations are very, very high in regards to page load times and how quickly information should be returned to the users. Thankfully, as part of DotNetNuke there are a number of different features that can help applications work quickly. Some of these features are known by everyone, things such as the DotNetNuke performance settings under "Host Settings", Cache Time settings on a module by module basis, and if on Professional Edition the built in page Output Cache Provider. However, one often overlooked API that is helpful for developers is the DataCache API. In this post I'll do a deep dive into why this API is so helpful and some scenarios where leveraging this API it can reduce page load times and system resources needed for each page request.
What is the DataCache
Before I get into the specifics of how to use the DataCache API I want to give a bit of detail on what exactly DataCache is. If you are an experienced ASP.NET developer you should be familiar with the ASP.NET Application object, a cache repository for storing information at the application level. Items stored in application are stored using code like the following. Application["MyKey"] = MyObject; This shouldn't be anything new to you, but within DotNetNuke, just like "SessionState" it is a recommended practice to not utilize this object due to the fact that Application is not aware of the DotNetNuke caching API's or Web Farm configurations. Enter the DataCache API.
DotNetNuke's DataCache object comes in to fill the gap, used at the base level in a very similar fasion it allows you to store any object that you desire using the DotNetNuke cache provider. If the site is configured to use File caching your content is stored in a file, if Memory it wil be stored in memory. With this you can rest assured that your cached objects will work just like the rest of the DotNetNuke infrastructure.
What do I Cache?
So great, we know we can cache objects, but what do we cache? This is the age old question and something that I cannot give a 100% answer here that will be a solution for everyone reading this. The key here is that we can store ANY objects necessary. A few examples of diffent cache strategies can be illustrated by discussing what I do with two of our most popular open source modules. You can cache individual objects, and implement this caching seamlessly in your controller class. We are doing this for the our custom settings for the "Expandable Text/HTML" module. The advantage here is that if you are calling ExpandableTextController.GetSettings(12) with cache checking code inside the "GetSettings" method we can be sure that we only hit the database if the object isn't in cache already. This helps reduce database hits for commonly used objects.
In addition to doing this you can also cache generated content. Lets assume that we have a module that grabs a number of objects from the database and then does a bunch of processing and eventually loads the content to an ASP.NET Literal control on the UI. Sure, we could cache the database objects to prevent the database call, but we could also think about storing the fully generated content. Doing this, even with a 15 minute cache time can reduce CPU usage, Memory usage, and page load times for your custom modules.
Using the DataCache Object
When it comes to using the data cache object there are a number of situations where a similar pattern can be used to help gain access to the Cache and store items. To help take away some of the duplicate code that can occur I often include a "CacheHelper" object that helps greatly. Looking at the code snippet below you can see what we use on all projects.
1: public static bool CacheExists(string key)
2: {
3: return DataCache.GetCache(key) != null;
4: }
5:
6: public static void SetCache<T>(T toSet, string key)
7: {
8: DataCache.SetCache(key, toSet);
9: }
10:
11: public static T GetItemFromCache<T>(string key)
12: {
13: return (T) DataCache.GetCache(key);
14: }
This makes the process this much easier for you as a developer you don't need to perform casting along the way and you can use very simple patterns to get objects from cache. For example I can do something like this: var myObject = CacheHelper.CacheExists("MyItem") ? CacheHelper.GetCache("MyItem") : new MyType();. As you can see one line process to get my object, or a new object if the item isn't contained within the cache. You can easily expand on this to help with your specific application.
Conclusion
Application performance is something that as web developers we need to be working on every day. Using DotNetNuke's built in cache system gives us a helpful tool to improve performance, and also ensures that applications we develop can easily scale to Web Farm environments. Please feel free to share comments/questions below.
This post has been cross-posted to my personal blog.