12/05/2008

How to enable - disable caching programmatically and on-demand

Sometimes, when you're doing something programmatically, like moving tabs around, you really don't want caching in your way in any form, since there's a great danger it'll affect the outcome. For some reason, programmatically clearing the cache may not be enough - you just don't need any caching around when you do certain stuff, and you don't want to oblige any host user to manually clear the cache. Here's a really simple way to disable caching programmatically.

 

 

'Get the initial caching level
'from the HostSettings arraylist 
'(this is the easiest way)
Dim initialCachingLevel As String
initialCachingLevel = _
   
HostSettings.Item("PerformanceSetting").ToString

'Disable caching
Dim hc As New Entities.Host.HostSettingsController
hc.UpdateHostSetting("PerformanceSetting", "0")

'Do your stuff here

'Enable caching with
'the initial caching level
hc.UpdateHostSetting("PerformanceSetting", initialCachingLevel)

 

What I'm doing in the bit of code above is disable caching temporarily to do what I have to do, then return it to its previous state. That's equivalent to going to Host Settings-Performance, from the Host menu, change the caching level, click "update", do any job I have to do and then get back to the Host menu and return caching to its previous value.

 

The caching level is defined by the "PerformanceSetting" Host property which can take string values ranged from "0" to "3" representing different levels of caching:

 

0 - No caching

1 - Light caching

2 - Moderate caching

3 - Heavy caching

 

Some notes:

 

You can get the current caching level from the HostSettingsController too, but it's a bit more fussy since the GetHostSetting method returns an IDataReader. Since the Host's settings get copied to the publicly-available HostSettings hashtable, I thought it's easier to obtain the current setting from there.

 

Changing the value in the HostSettings hashtable will NOT affect your caching. You've got to use HostSettingsController.UpdateHostSetting.

 

You may also want to clear any cache left before you start doing your stuff, it helps in some cases. In this case, the DataCache class may prove very useful. Here's what you can do (you can choose not to execute some of these lines, depending on your needs):

 

DataCache.ClearTabsCache(0)
DataCache.ClearPortalCache(0, True)
DataCache.ClearHostCache(True)
DataCache.ClearModuleCache()
DataCache.ClearTabPermissionsCache(0)
DataCache.ClearUserCache( _
       
0, _
       
UserController.GetCurrentUserInfo().Username)

 

 

Some of the above statements may overlap, in the sense that they're actually subsets of other statements. For example, ClearHostCache() is considered equivalent to the Clear Cache option that exists in the Host Settings page. I personally prefer to issue ALL those statements, even if they overlap.

 

The DataCache class has some more methods but I think those mentioned here are enough to give you a good start. You'll probably find out the rest yourself, in an as-needed basis.

 

The above code applies only to portal 0. Whenever you see a boolean parameter there, it's indicating "cascade" cache clearing, which I always want set to true. Also, the ClearUserCache() method needs the currently logged on user's name, which I get by using UserController.GetCurrentUserInfo() which returns a UserInfo object containing, among others, the user's name.

3 comments:

Anonymous,  May 7, 2011 at 11:06 PM  

Mind explaining how we're to implement this code? Execute it?

Gyromyristis May 8, 2011 at 1:01 AM  

The reason for this post was not to give a complete guide on how to use such code, but just the code itself. I undestand that I may not have made this clear, I'm sorry about it.

This can be used, according to your needs, either when developing a DNN module and need to take care of caching somehow (e.g. when your module creates or alters existing content on your site that may be cached), or even inside your .ascx skins (a quick thought would be a skin object available only to specific roles, such as a content administrator, which allows them to clear the cache with a click - something Oliver Hine's ribbon replacement for DNN has already implemented, possibly in a similar way).

Intraday Commodity Calls March 15, 2012 at 6:53 PM  

Suppose I have some CacheResult attributes set on members of a class and I want to disable caching at runtime sometimes. Is this possible? I'd like to have a flag to turn caching on/off in a particular case in case it has unexpected consequences in production. Right now my fallback plan is to use a separate cache object for just this feature and set it to scope="request" to sort of turn caching off but this is not ideal.

Sites i like. You should visit here to take commodity calls for trade in share market also take mcx calls and intraday calls with 85 to 90% accuracy.

Related Posts with Thumbnails

Recent Comments

Free DotNetNuke Stuff

Free DotNet Videos

  © Blogger template The Professional Template by Ourblogtemplates.com 2008

Back to TOP