ColdFusion CacheLocker
I ran into a problem the other day where I needed a way to temporarily store data between page requests. Typically I'm able to stash this sort of thing in the session scope, but these requests originated from different sources and I prefer to avoid (de)serializing when I can.
Instead I set up something akin to one of those pay-per-use lockers. You stick your items in the bin, drop in a few quarters, and take the newly unlocked key. Later you come back and use that key to retrieve your items. Your key is now 'locked' back into the starting position and the cycle begins anew.
Like one of those pay-per-use lockers you just stash your data, save they key, use the key, trash the data.
Simple as pie.
This isn't the sort of thing that comes up often, but should it arise I've got just the tool for the job!
Example Usage:
// initialize the locker
application.cacheLocker = CreateObject("component","cacheLocker").init();
// store some arbitrary data
key = application.cacheLocker.store([1,2,3,4]);
// then retrieve the data using the saved key
// throws a CacheException if the key doesn't 'fit'
arbitraryNumbers = application.cacheLocker.retrieve(key);
The data is destroyed after being retrieved; it's a one time only locker.
There are two important things to keep in mind when using this utility:
- There's currently no mechanism for cleaning out lockers, so if you're not regularly retrieving your data then this thing is just going to grow, and grow, and grow.
- The locker is not stored in any sort of persistent memory. If ColdFusion goes down, then the lockers are destroyed.
Generating Mock Images in ColdFusion
Dummy Image!
It's a cool site that lets you pass in a width and a height to generate an image which could come in really handy for quickly mocking up web pages.
I can't believe I never thought of doing something like this!
ColdFusion is a great language for doing this sort of thing, so it was trivial to whip something up quickly.
I was originally writing a file to disk because I didn't realize you could stream an image variable to the browser, but my other co-worker Jim came to the rescue with the cfimage "writeToBrowser" action!
<cfset params = listToArray(cgi.query_string,"x") />
<cfif arrayLen(params) lt 2>
<cfthrow message="Input should be like ?[w]x[h]"/>
</cfif>
<cfset width = params[1] />
<cfset height = params[2] />
<cfset color = "gray" />
<cfif not (isNumeric(width) and isNumeric(height))/>
<cfthrow message="Width/Height should be numeric ?100x100">
</cfif>
<cfset image = ImageNew("", width, height,"rgb", color) />
<cfset ImageDrawText(image, "#width# x #height#", 0, 10)>
<cfimage source="#image#" action="writeToBrowser"/>
I'd love to link an example to you, but my hosting plan doesn't support _cf_image_
ColdFusion FileSweeper
I'm tired writing scripts to clean up temporary files.
It's a nigh trivial task. The chance for error is small, but the consequences can be dire!
I wrote a little utility that makes this a little easier. It simply deletes all the files in a folder (recursive or no) that are older than a specified number of seconds.
Example Usage:
// no init needed, but the function's there if you like
fileSweeper = CreateObject("component", "fileSweeper");
// all arguments are actually defaulted to the values shown
// so you needn't pass any, fancy eh?
fileSweeper.deleteOldFiles(
folder = ExpandPath("/temp"),
seconds = 600,
filter = "*.*",
recurse = "no",
lockname = "deleteFiles",
timeout = 60,
logging = true
);
ColdFusion serializeJSON Problem
I ran into a little problem with the CF8 serializeJSON function. The function doesn't properly escape quotes in struct keys, which results in invalid JSON being generated.
For Example: (all code is in cfscript)
// Struct Key with quotes in it
heightCounts["6'0"""] = 5;
// Serialize function runs alright...
serialized = serializeJSON(heightCounts);
// But the JSON is invalid. The quotes are unescaped!
writeOutput(serialized);
// => {"6'0"":5.0}
// As you might expect, deserializing throws an error
deserializeJSON(serialized);
I certainly don't like the idea of having quotes in struct keys, but that's beside the point. I filed a bug report, but I also wrote a little function to jsStringFormat my struct keys. Problem solved!
function cleanKeys(dirtyData) {
var cleanData = structNew();
var cleanKey = "";
var i = "";
if(!isStruct(dirtyData)) {
return dirtyData;
}
for(i in dirtyData) {
cleanKey = jsStringFormat(i);
cleanData[cleanKey] = cleanKeys(dirtyData[i]);
}
return cleanData;
}
You just pass in your quote-fully keyed struct, and get a clean one back:
// Same example as above
heightCounts["6'0"""] = 5;
// This time we sanitize the struct keys
jsSafeHeightCounts = cleanKeys(heightCounts);
// The serializeJSON call still runs without error
serialized = serializeJSON(jsSafeHeightCounts);
// But this time the output is correct!
writeOutput(serialized);
// => {"6\'0\"":5.0}
// And the deserialize works as expected
deserializeJSON(serialized);
Ta Da!
Merging Netflix Accounts with ColdFusion and JavaScript
I wrote a little script using ColdFusion and JavaScript to merge two NetFlix accounts. It's ugly and cheesy, but it works so I thought I'd share.
Here's how you do it:
- Register for a developer key.
- Grab the RSS feed url of the queue you want to merge FROM. This can be obtained by logging into the FROM account and clicking on the "RSS" link in the footer.
- Log in to the account that you'd like to merge TO.
- Hit this script in the same browser you logged into the TO account with, and make sure you allow pop-ups!
<!--- enter your feed here! --->
<cfset feed = "http://rss.netflix.com/QueueRSS?id=P4806480653914107620156446228102116" />
<!--- enter your consumer key here! --->
<cfset key = "your-consumer-key-here" />
<cfset href = "http://widgets.netflix.com/addToQueue.jsp?output=json&devKey=#key#&queue_type=disc&movie_id=http://api.netflix.com/catalog/movie/" />
<cffeed action="read" name="queue" source="#feed#" />
<cfset movies = queue.item />
<cfset idList = "" />
<cfloop array="#movies#" index="i">
<cfset idList = ListAppend(idList,ListLast(i.guid.value,"/")) />
</cfloop>
<cfoutput>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"/>
<--- Pop up a new window, swap the href every second. Told ya it was cheesy! --->
<script>
var popUp = window.open('http://google.com');
var idList = [#idList#];
new PeriodicalExecuter(
function(pe) {
if (idList.length == 0) {
pe.stop();
} else {
popUp.location.href = "#href#" + idList.pop();
}
},
1
);
</script>
</cfoutput>
Note: I don't know if this allowed by the EULA, or if there even is a EULA...so, use at your own risk!





























