Just a quick tip for folks using ColdFusion 9's new caching system. Whenever you add something to the cache you must use a unique key. For a simple method, that's relatively easy:
return cachedData;
}
public any function doItSlow() {
var cachedData = cacheGet("slowProcess");
if(isNull(cachedData)) {
var cachedData = doSomethingSlow();
cachePut("slowProcess", cachedData);
}
That's a pretty trivial example, but you get the idea. A problem may arise, though, when your method takes arguments, especially multiple arguments. Imagine a typical "getX" type function that loads data. It may take arguments for the max number of items to return, the starting index, the order, and perhaps a search string. Imagine the following method signature:
function getStuff(numeric maxItems=10, numeric start=1, string sort="name asc", string search) {
Given that you have multiple arguments and therefore a large number of unique results, how could you cache the results? You could generate a key by examining the arguments scope:
var key = "";
key &= "#arguments.maxItems#_";
key &= "#arguments.start#_";
key &= "#arguments.sort#_";
if(structKeyExists(arguments.search)) key &="#arguments.search#";
This would create a key looking something like: 10_1_name_asc_foo. While this method works, it can get pretty complex. (Although if you start adding more arguments, you may have an overly complex API in the first place. That's a discussion for another day.) Here is a little trick I've taken to using that makes it even easier:
var key = "mymethod_#serializeJSON(arguments)#";
That's it. I use a prefix of "mymethod" so that when I examine the entire cache it is clear as to where the data belongs. The rest of the key is simply a serialized form of the arguments. It may be ugly, but it works perfectly well. Obviously you want to use with caution - especially since that search argument could be anything.