Aliaspooryorik
ColdFusion ORM Book

Clearing ColdBox cached views automatically

One of the many cool features in ColdBox is the ability to cache views in the template cache. This is great for improving performance of your site in production, but gets a bit tiresome when you're changing code in the view and have to manually clear the cache to see your changes. Someone asked over on the ColdBox mailing lists if you could turn off view caching for a dev environment, I posted a reply (along with some good comments by the smart folks on that list), but thought I'd blog it here as well.

The ColdBox cache has a clearAllViews() method which as the name suggests allows you to clear all the cached views (no surprise there!). So if you wanted to clear all the views automatically you could detect the environment and call it in your Main.onRequestStart method like so:


void function onRequestStart( event )
{
var rc = arguments.event.getCollection();

if ( getSetting( "Environment" ) != "production" )
{
getColdboxOCM( "template" ).clearAllViews();
}
}

That was easy, and yet it's a bit ugly having conditional code like this in a controller. What I like to do is use an interceptor, which is only defined for my development environment, to clear the cache. Here is the code:


/**
* I am only used when in development (this interceptor should not be used in production)
*/

component name="Development" extends="coldbox.system.interceptor"
{

// ------------------------------------------- CONSTRUCTOR -------------------------------------------

/**
* I configure the interceptor
*/

void function Configure()
{
}

// ------------------------------------------- INTERCEPTION POINTS -------------------------------------------
/**
* This occurs after rendering, usually the last step in an execution. This simulates an on request end interception point.
*/

void function postProcess( event, interceptData )
{
log.debug("postProcess fired, clearing all views");
getColdboxOCM( "template" ).clearAllViews();
}

}

Of course, now you need to tell ColdBox about it in the ColdBox.cfc config.


component
{

/**
* I detect the environment
*/

string function detectEnvironment()
{
if ( ReFindNoCase( "^(localhost|127.0.0.1)$", cgi.http_host ) != 0 )
{
return "development";
}
else
{
return ReReplace( cgi.http_host, "[^a-z0-9]", "", "all" );
}
}

/**
* settings for the development environment
*/

void function development()
{
// Override coldbox directives
coldbox.debugMode = true;

// add interceptor that clears the view cache
var devInterceptor =
{
class = 'interceptors.Development'
};

arrayPrepend( interceptors, devInterceptor );
}

// Configure ColdBox Application
void function configure(){

// coldbox directives
coldbox = {
... your settings here ...
}

// Register interceptors as an array
interceptors = [
//Autowire
{
class="coldbox.system.interceptors.Autowire",
properties={}
},
//SES
{
class="coldbox.system.interceptors.SES",
properties={}
}
];

... more settings here ...
}
}

As you can see the 'interceptors.Development' interceptor is only registered if the environment is identified as 'development'. On the production server it will be ignored.

 


3 comments

  1. After your suggestion on the mailing list I implemented this method above and it's working just fine!

    However, I am starting to think that the better method is the one that Curt Gratz suggested whic is to use a setting for the cache value like so:

    event.setView(name="home", cache= getSetting("cacheViews"));

    The way you and I are currently doing it, the view gets cached and then cleared, which seems like senseless work for CB as opposed to his suggestion of just setting a variable(s) in Coldbox.cfc to handle dev/stage/prod environments.

    THoughts on pros/cons of either?

    Andy

    Comment by Andy K – January 14, 2011
  2. I also suggest you to use the integrated per-environment settings to define the cache variable.

    You may also use this to set other cache settings like handler/eventCaching.

    Usually, I prefer using built-in functionality's instead of rewrite them except if the built-in one is not optimized enough.

    Donaldo
    @I_TwitIT

    Comment by Donaldo De Sousa – January 14, 2011
  3. @Andy, I think they are both good ways to do it.

    I like to clear the cache instead of stopping caching completely as I want to know that the caching mechanism is working. If I set caching off then it would never be used in development only in production which would worry me.

    The interceptor can also clear other things from the cache besides objects in the template cache if I want to.

    Also, when I cache views I tend to use the additional settings so I'd have something like this:

    event.setView( name='home', cache=true, cacheTimeout=30, cacheLastAccessTimeout=10 )

    It would seem odd to me to have the cache flag set false but still be passing the caching arguments.

    @Donaldo,

    Yes, I do use the in-built environment detection to turn on debugging, handler caching etc. I just wanted to keep the code samples simple.

    Comment by John Whish – January 14, 2011

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts