storing common data - cache/scope/?

Moving along with my first ColdBox app - quick question…

I have several queries that return data that I’d normally stuff into the application scope for quick lookups. The data rarely changes and when it does I can reinit the app to rerun my queries.

What’s the best way to do this in ColdBox? I have the storage module loaded and am using that for some session storage and could dump things into application but that feels dirty. It seems like I could also possibly leverage CacheBox to simply cache the queries? Where would be the best place to do that? At the service layer? Handlers?


CFML’s built-in query caching is quick and dirty, but it works. It’s not very extensible though unless you’re on Railo/Lucee.

I would recommend using CacheBox. You can put the cache code wherever you want, but I prefer to do it in the service. Check out the getOrSet() method for a really clean one-liner.

var qRoles = cache.getOrSet(
return myDAO.getRoles();

That call will return it from the cache if found. If not found, it will execute the closure you provided to generate the data, store it, and return it.

You can pass in additional arguments to override the default timeout.

Inject CacheBox wherever you need it like so:
property name=“cache” inject=“cachebox:default”;

“default” can be changed to a specific cache name.

Also, don’t forget ColdBox has slick view and event caching as well to cache the HTML of an entire page request, including all service calls, etc.



ColdBox Platform Evangelist
Ortus Solutions, Corp

ColdBox Platform:

Thanks Brad - the views are very dynamic so I can’t cache those - it’s a travel app - so think flight details (dynamic), list of airports (cached).

I like the simplicity of the getOrSet method. I can set this up and then I’m assuming anytime I call roleService.getRoles() from my handler it would hit this and pull from where it needed to.