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.