RenderView with suffix argument not working as I'd expect

Hi,

I've been playing around with the view caching in ColdBox 3 RC 1, and
have found when I pass a value to the cacheSuffix argument of render
view it doesn't seem to cache the view. Is this a bug or am I not
understanding how it works?

Here's a quick example:

component
{
  void function cachedviewtest( required event )
  {
    var rc = arguments.event.getCollection();

    // get the template cache
    var TemplateCache = getColdboxOCM( "template" );
    // set the view we want to render
    var theview = "general/cachedviewtest";
    // create a suffix for this event to identify in the cache
    var cachesuffix = "foobar";

    writeDump( var=TemplateCache.GETKEYS(), output="console" );

    writeDump( var="exists? " & TemplateCache.lookup( "cbox_view-general/
cachedviewtestfoobar" ), output="console" );
    writeDump( var="exists? " &
TemplateCache.lookup( "#TemplateCache.getViewCacheKeyPrefix()##theview##cachesuffix#" ),
output="console" );

    arguments.event.setView( name=theview, cache="true",
cacheTimeout="5", cacheLastAccessTimeout="5",
cacheSuffix=cachesuffix );
  }
}

My cachedviewtest.cfm view is a simple output of #now()#

When I run it this first time I get this in my console:

[localhost CF9.01]:array [empty]
[localhost CF9.01]:exists? false
[localhost CF9.01]:exists? false

The browsers shows the current timestamp, which is what I'd expect.
However if I run it again I get the new current timestamp in the
browser, but the console shows:

[localhost CF9.01]:array - Top 1 of 1 rows
[localhost CF9.01]:1) cbox_view-general/cachedviewtestfoobar
[localhost CF9.01]:exists? true
[localhost CF9.01]:exists? true

If I set the cacheSuffix argument passed to renderView to an empty
string, then the the timestamp does not update in my browser and my
console shows:

[localhost CF9.01]:array - Top 1 of 1 rows
[localhost CF9.01]:1) cbox_view-general/cachedviewtest
[localhost CF9.01]:exists? false
[localhost CF9.01]:exists? true

So it seems to me that if you set the cacheSuffix, then the view is
correctly store in the cache, but is not retrieved from it again. This
is also confirmed by the debugging info which says:

rendering Cached View [general/cachedviewtest.cfm]

When I set the suffix to an empty string, but shows:

rendering View [general/cachedviewtest.cfm]

When I do set the suffix to any string.

As a side note, which may help. If I run the event with a blank suffix
and then change the suffix to #Rand()#, it always renders a cached
view. Am I completely misunderstanding what the suffix argument is
for?

Thanks,

- John

John

This is right on the money, I am adding a fix as I type.

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Alrighty, fixed and man did it cleanup some code, it actually became faster logic, thanks John for discovering this. Tricky little one.

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Awesome, thanks Luis, you’re a legend!

– sent by a little green robot

Hi Luis, Thanks for looking at this so quickly - it really is a nice feature of the framework.

Whilst playing around with it I noticed that the key names in the cache are all lowercase and in the format:

cbox_view-:general/bleedingedge6af76d5935bd2f57483bde7d6f6d6638

However, the "cbox_incomingcontexthash" key in the private request collection is uppercase, for example:

6AF76D5935BD2F57483BDE7D6F6D6638

As the lookup to the cache is case-sensitive, I wondered if there was a reason for this as at the moment I’m forcing to lowercase when building the key to get the hit.

The other thing I wondered about is, should the getViewCacheKeyPrefix() method return:

cbox_view-:

at the moment is returns

cbox_view-

So I need to add the colon myself when constructing the key.

Thanks again,

  • John

The colon is because it can be from a module. Basically since there is
no module it is an empty colon marker.

Ad for the cage keys, the cache is case insensitive so case should not matter.

Cool, thanks Luis. I was sure it was case-sensitive, but I was wrong! :slight_smile:

No problem john. By the way great post maybe you can do a follow up using the onrequestcapture interception point to showcase adding rc variables to event caching

done! :slight_smile:

Quick follow up question about event caching for an app with users. In the past, when I built apps where users could login, then I’d have something like this in the main.onrequeststart method:

component
{
property name=“securityService” inject=“model”;

function onRequestStart(event)
{
var rc = event.getCollection();
rc.CurrentUser = securityService.getCurrentUser();
}
}

However, if I want the events to be cached on a per-user basis I would need to do this in the onRequestCapture interception point instead if I want the “CurrentUser” key to affect the cache key name. Is that how you would do it?

Thanks,

  • John

That is how I would do it now yes.

Curt Gratz

Computer Know How

Thanks Curt.

– sent by a little green robot

well, there is an alternative, remember that users are tracked by their cfid+cftoken or jsession id (j2ee session enabled).

So instead, why no at onRequestCapture mix in the jsession id into the rc so the event caching is unique per jsession id.

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com