Aliaspooryorik
ColdFusion ORM Book

ColdBox Object Cache Manager occasional error

I've been getting occasional errors on a ColdBox 3.0.0 website which is using the ColdBox Object Cache Manager where the value is undefined. I've tried to replicate it but couldn't. However, I think I've figured it out.

Here is the code:


var cacheKey = "toplevelcategories";
if( !getColdboxOCM().lookup( cacheKey ) )
{
// not cached so get it
prc.toplevelcategories = CatalogService.findTopLevelCategories();
// cache for 15 mins with an idle timeout of 5 mins
getColdboxOCM().set( cacheKey, prc.toplevelcategories, 15, 5 );
}
else
{
prc.toplevelcategories = getColdboxOCM().get( cacheKey );
}

The issue seems to be caused by the cache evicting the item between the lookup to see if it exists and the call to get it from the cache. This would explain why it hardly ever happens.

I've now changed the code to:


var cacheKey = "toplevelcategories";
prc.toplevelcategories = getColdboxOCM().get( cacheKey );
if( IsNull( prc.toplevelcategories ) )
{
// not cached so get it
prc.toplevelcategories = CatalogService.findTopLevelCategories();
// cache for 15 mins with an idle timeout of 5 mins
getColdboxOCM().set( cacheKey, prc.toplevelcategories, 15, 5 );
}

This time I try to get it from the cache first, if it's not in cache, then I can check for a null. So far this seems to have done the trick!


4 comments

  1. We ran into the same problem and patched it similarly. I think this was fixed in 3.0.1.

    Comment by Adrian J. Moreno – January 11, 2012
  2. Hi Adrian,
    Good to know that the change fixed it for you. It was driving me nuts!

    Comment by John Whish – January 11, 2012
  3. This applies to any cache you use. If you do a looku and then retrieve them it might not exist.

    Always always get first and then test.

    Comment by Luis Majano – January 23, 2012
  4. Hi Luis, yes that makes sense. I guess it's because there is a lookup() method we're used as CF developers to checking before getting a key. :)

    Comment by John Whish – January 24, 2012

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