Printable Version Printable Version Recommend Recommend Email to a friend Email to a friend

Caching in DotNetNuke

Caching is a simple concept but is often left behind for "Getting the job done" and just "Making it work". With caching in mind from the beginning you can ensure that your application will perform great day one and continue to scale well as you or your customer starts pumping gobs of data into it.

One key point to know about the importance of caching is that one of the most expensive methods your application can do is to request data from the database. If you request data and get the same result back more than once then you probably should have cached it.

DotNetNuke makes caching of data really easy and there is only two API methods that you really need to know to get started using caching.



Note: Namespaces required in this example:

using DotNetNuke.Common.Utilities; 
using DotNetNuke.Entities.Host; 

public static object GetCache(string CacheKey);  
public static void SetCache(string CacheKey, object objObject, TimeSpan SlidingExpiration); 

GetCache() accepts a single string value and returns an untyped object. The important part here is how you craft the the string. The best methodology I have found is to create several constant variables and append them together with a unique identified.

Example:

 
string cacheKey = "MyModuleName" + "_TypeOfContent_" + contentId.ToString(); 
 

This will ensure that the content being cached is unique to your module and can not be overridden by another.

With the previously mentioned CacheKey in hand you can now add your content to the cache. But you may be asking yourself: When do I know when to set the cache? GetCache() will return a null object if it has expired. So all you need to do is check if the returned object is null.

var cacheObj = DataCache.GetCache(cacheKey); 
   
if (cacheObj == null) {..} 

The next question to ask is: How long do I cache my object for?

We can utilize the Performance setting within Host settings in order to manipulate the time a object is cached.

var timeOut = 20 * Convert.ToInt32(Host.PerformanceSetting);

Now that we have the length all we have to do is perform our database call to get our data.

cacheObj = DataProvider.Instance().ExecuteDataSet("ProcedureName");

Now check to make sure that the timeOut value is not set to no caching and to make sure we got a value back from our database.

if (timeOut > 0 & cache != null) {...} 

Assuming that all is well there. We can proceed to caching our object.

DataCache.SetCache(cacheKey, cacheObj, TimeSpan.FromMinutes(timeOut)); 

The process is you are simply marking this object with a string key, adding the object and then telling it when you want it to expire. DotnetNuke will handle the rest.

Now put it all together and you get:

public static int GetVideoLogTotal(int videoId) 
{ 
    var cache = DataCache.GetCache(Constants.ModuleCacheKey + Constants.VideoLogTotal + videoId.ToString()); 
  
    if (cache == null) 
    { 
        var timeOut = 20 * Convert.ToInt32(Host.PerformanceSetting); 
        cache = GetVideoLog(videoId).Count; 
        if (timeOut > 0 & cache != null) 
        { 
            DataCache.SetCache(Constants.ModuleCacheKey + Constants.VideoLogTotal + videoId.ToString(), cache, TimeSpan.FromMinutes(timeOut)); 
        } 
    } 
  
    return (int)cache; 
}

In the next article we'll go over how to invalidate items in the cache and how to do entity based caching instead of record set caching.


Do you have a question? Ask on our Forum!

Try FREE
30 days money back guaranteed