DNN Community Blog

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact community@dnnsoftware.com.

 


Using DotNetNuke Caching In Custom Modules

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.

Comments

Comment Form

Only registered users may post comments.

NewsArchives


Aderson Oliveira (3)
Alec Whittington (11)
Alex Shirley (10)
Andrew Nurse (30)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (13)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (3)
Bill Walker (36)
Bob Kruger (5)
Brice Snow (1)
Bruce Chapman (9)
Bryan Andrews (1)
cathal connolly (52)
Charles Nurse (157)
Chris Hammond (197)
Chris Paterra (55)
Clinton Patterson (25)
Cuong Dang (21)
Daniel Bartholomew (2)
Daniel Mettler (19)
Dave Buckner (2)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (66)
Geoff Barlow (2)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (1)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (234)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (3)
Jordan Coopersmith (1)
Keivan Beigi (3)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Matthias Schlomann (15)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (1)
Michael Washington (202)
Mike Horton (19)
Mitchel Sellers (28)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Patrick Santry (3)
Peter Donker (50)
Philip Beadle (135)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott S (11)
Scott Willhite (97)
Sebastian Leupold (76)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Timo Breumelhof (24)
Torsten Weggen (1)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (36)
Will Strohl (140)
William Severance (5)

Copyright 2014 by DNN Corp | Terms of Use | Privacy | Design by Parker Moore Design