CF on Wheels Plugin

Mike Henke has taken the original CF on Wheels sample code that I created and turned it into a plugin for Wheels. Thanks, Mike! You can read more about it on the CF on Wheels discussion group here.

Quick Start

I'm posting this to satisfy curiosity for those of you who might like to see a clear code sample for CacheBox before you download the distribution. This is something that I could have done better when I gave the recent presentation, so it's also a bit of a follow-up for that. ;)

The only thing you actually need in order to implement CacheBox in your application is the CacheBoxAgent.cfc (it's actually all lower-case in the distribution, which is an important note for linux compatibility).

So you'll have to create an Agent object and you'll want to store it in your application for future use. If you're using a dependency injection (DI) framework like ColdSpring or Lightwire, of course you'll declare it there, otherwise something like this should work nicely. Remember to choose a descriptive name for your agent!

<cfset application.cachebox = CreateObject("component","cacheboxagent").init(
   AgentName = "my_cache"
   [, Context = "application"]
   [, Evict = "auto"]
   ) />

The context and eviction policy arguments are optional. Supported contexts include application, server and cluster (and they probably do precisely what you're thinking they do right now). There are a variety of available eviction policies including Age, Idle, Least Frequently Used (LFU), Least Recently Used (LRU), and First In First Out (FIFO), however, in most cases the default "auto" eviction policy should work nicely and is recommended. The auto policy allows the CacheBox service to decide what the most efficient eviction policy is for your cache, based on usage patterns and available resources.

If you're using this agent to store singleton objects that should never expire, you'll want to use "none" as your eviction policy.

Now that you have your agent, you can use it to store whatever cache you need. Cache is retrieved using the Fetch() method and set using the Store() method. Both methods return a structure with two keys: status and content. A status of 0 indicates everything is okay. Any non-zero status indicates some kind of cache failure.

<cfset myCache = application.CacheBox.fetch("myPage") />

<cfif myCache.status>
   <!--- non-zero status means the cache wasn't found --->

   <cfsavecontent variable="temp">
      ... generate the content here ...

   <!--- store the content for later use --->
   <cfset myCache ="myPage", temp) />


If you're storing singleton objects, you'll want to make sure that you return the content value from the store() operation, because that will guarantee that you're using the first (and only) object placed in cache. This only applies to objects - strings, structures and other kinds of content are simply overwritten when you store them.

Now if you need to selectively reset that item in cache, that's also easy to do:

<cfset Application.CacheBox.expire("myPage") />

That's all there is to it.

BlogCFC was created by Raymond Camden. This blog is running version 5.5.006. | Protected by Akismet | Blog with WordPress