[3.8.0] Intercept call to expired object from cache

Hi there,

There is an issue that Im having with a saved object into cache with expiration time: 120 and idle time out 20.

If I stop using a section in a page for longer than 20 minutes, and then try to do something that will need to get data from cache I got an error telling me that the requested object is not found. If I refresh the page the error vanish, since the object will be put again into cache in not exists. Is there a global setting in CacheBox if “cacheKey” pass the 20 minutes idle time, and gets a call after that will auto put in cache again? "cache.set(cacheKey, data, 120, 20)"

A simple scenario: If a person leaves the building and returns after a 20 minutes coffee break and tries to get back from where he left off… he will get an error.

Thanks

Xerrano

Do you have an example of how you are doing this? The reason I ask, as I am just getting into the workings of caching at the moment myself, is that I did read on the documentation site that there is a preferred method to see if it exists before getting the object.

What caching mechanism are you using and what are the settings for it.

The reason I ask is that, even though its time might be not expired, it is possible for it to have been reaped from the cache. If that is the case then, you may need to tweak your cache settings a bit more.

Please share the relevant code. It is up to you to check for the existence of an item in cache and recreate/set it if necessary. Usually this looks something like so:

var key = 'keyName';
local.result = cache.get(key);
if( structKeyExists(local,"result") ) {
  var data = local.result;
} else {
  var data = logicToProduceData();
  cache.set(key,data);
}
// Use data

Now, that being said the upcoming version of CacheBox (1.6) will include a new getOrSet() method for Railo and CF10 that accepts a closure to allow you to do the above as a one-liner:
https://ortussolutions.atlassian.net/browse/CACHEBOX-7

var data = cache.getOrSet('keyName',function() { return logicToProduceData(); } );
// Use data

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thank you guys,

//cacheIOCService is an ecache set up in CacheBox

In one plugin I have this: (the plugin function will be called everytime the page gets a hit and do a query if object is not in cache

`
<cfif NOT cacheIOCService.lookup(‘qryCategoryService’)>
//do a query and save
<cfset cacheIOCService.set(‘qryCategoryService’, qryCategory, 60, 20) />

//get data from cache
<cfset qryCategories = cacheIOCService.get(‘qryCategoryService’) />

`

This is very simple, what I was trying to avoid I think… is to use the lookup logic in every place I need to use the “qryCategoryService” I was about to write an interceptor to simplify and avoid code duplication (the lookup logic) so, I was wondering if it’s possible to intercept this type of error, and do something only when occur.

I think the problem is that the item can actually expire from the cache between the time your lookup runs and the time your get runs. Lookup simply returns true if the item exists right then, but does not guarantee the continued existence of it. Therefore it is always best to simply issue a get and then check to see if null came back. If you received a value, then it exists, if you receive null, then it does not exist. See the code sample in my last E-mails.

As far as reducing the code you type, I like to implement caching in the service layer so you only put the logic in once and then use your service to get the data. There is also an AOP aspect called CacheBack that will memoize the output of a function. http://www.coldbox.org/forgebox/view/CacheBack

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com