Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Clearing ColdBox cached views automatically

DZone's Guide to

Clearing ColdBox cached views automatically

· Java Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

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.

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.

Topics:

Published at DZone with permission of John Whish, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}