Caching Strategy

I am trying to figure out the best approach here and I don’t know my options I have this home handler that grabs a bunch of different things to show on the home page.

public void function index(event){
var rc = event.getCollection();

rc.blogcfc = variables.blogcfc;
rc.tweets = getLatestTweets(3);

rc.recentEntries = getRecentEntries();
rc.recentComments = getRecentComments();

event.setView(“home”);
}

My question is should I be caching these individual items, or do you cache the entire handler in production? Just curious what I do when I get into production. Caching is still one of those things that I have not fully grasped in ColdBox.

I don’t think there’s really a right or wrong way to do it. If you want to do some basic and easy caching and are ok with everything in your handler to be cached for the same amount of time, you can just do handler caching and let ColdBox deal with the cache engine for you.

My personal preference is to build caching into my services. I inject a CacheBox reference (via a provider) and add a cacheFor parameter to the service method I want to implement caching for.

rc.recentEntries = entryService.getRecentEntries(cacheFor=30);

Then inside that method I do something similar to:

if cacheFor

if not exists in cache
get data
put data in cache

return from cache

else

get data
return

Thanks!

~Brad

ok dumb question but in the coldbox struct in the coldbox configure method… what is eventCaching?

//Application Aspects
handlerCaching = false,
eventCaching = false,
proxyReturnCollection = false,
flashURLPersistScope = “session”

Does anyone have a good sample application that uses cachebox? I am having issues getting this going. This is my service layer and I saw an example of using the cache to get and put but this doesn’t seem to work. It seems that I am loading an instance of cachebox into the service layer but cachebox does not have a method called lookup.

What am I missing here ?

/**

  • @singleton
  • @accessors true
  • @serializable false
    */
    component {

property name=“twitterService” inject;
property name=“blogcfc” inject;
property name=“cache” inject=“cachebox”;

public HomeService function init(){
return this;
}

public query function getRecentEntries(){
var cacheKey = ‘q-recent-entries’;
var params = {
maxEntries = 10,
releasedOnly = true
};

if( cache.lookup(cacheKey) ){
return cache.get(cacheKey);
} else {
var data = blogcfc.getEntries(duplicate(params));
return data.entries;
}
}

}

You are injecting the cache box cache factory and not a specific cache.

Cachebox maps to coldbox. System. Cache. Cache factory
Cachebox:default maps to a named cache called default

As for event caching here is a little description. When you turn it on in the config then you can annotate your event action in your handlers with cache=true and cachetimeout and cachelastaccesstimeout.

Once an event action is marked for caching, and you execute the event via the browser cold box detects it and caches the output of the entire event. So the next time that event is called it is not executed but the HTML output is just sent back. If you execute the event with different URL or form parameters coldbox is smart enough to detect a new event caching pattern and caches another permutation for that event.

Event caching is extremely powerful and will optimize your applications considerably. However, you must understand it’s limitations and how it works. In 3.0 we also added a new interceptor point called onrequestcapture that gives you a chance before event caching is detected for you to mix in variables to the rc for cache evaluations. Great for adding your multiple domain or lingual sites.

Luis Majano
President
Ortus Solutions, Corp
Toll free phone/fax: 1-888-557-8057
Mobile: 909-248-3408
www.ortussolutions.com
www.coldbox.org