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!
- Posted in:
- ColdFusion
- Coldbox


Comment by Adrian J. Moreno – January 11, 2012
Good to know that the change fixed it for you. It was driving me nuts!
Comment by John Whish – January 11, 2012
Always always get first and then test.
Comment by Luis Majano – January 23, 2012
Comment by John Whish – January 24, 2012