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 ColdFusionMeetup.com 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 ...
   </cfsavecontent>

   <!--- store the content for later use --->
   <cfset myCache = application.CacheBox.store("myPage", temp) />
</cfif>

<cfoutput>#myCache.content#</cfoutput>

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.

Comments
Brad Wood's Gravatar Very nice. Clear and concise. :)
# Posted By Brad Wood | 10/25/09 10:45 PM
ike's Gravatar Thanks Brad! :)
# Posted By ike | 10/26/09 2:56 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.5.006. | Protected by Akismet | Blog with WordPress