I've already talked about the advantages of memcached here, but I never mentioned how to get it in to your ColdFusion applications. Fortunately most of the hard work has already been done for you by Shayne Sweeney, as he has created the ColdFusion memcached client for you.
Once you have memcached installed on your server (Linux or Windows) you then need to get it working with your code.
Firstly, go to the memcached ColdFusion client page and download the project. Unzip the project and you should see the following:
com
resources
tags
MemcachedFactory.cfc
The first thing you need to do is move the com, resources and MemcachedFactory.cfc files to a /memcached/ directory in the root of your web app leaving you with just the tags folder.
The tags folder contains the main custom tag that gets your site connected to the memcached server you have installed. So move the cached.cfm file within the tags folder to your servers CustomTags directory, I renamed it to memcached.cfm, but thats up to you. Now you have the use of a <cf_memcached> tag. If you don't have access to your servers CustomTags directory for some reason, then you can put the file in your site somewhere and use the <cfmodule> tag instead, its entirely up to you.
Thats the hard bit (if you like) done, now all you need to do is use it! The first thing to do is cache the MemcachedFactory.cfc in the application scope, so enter something like the below into your Application.cfm/cfc:
<cfif not structKeyExists(application,"memcachedFactory")> <cfset application.memcachedFactory=createObject
("component","memcached.MemcachedFactory").init()> </cfif>
Now to cache a simple query for example, consider the below:
<cf_memcached variable="myquery" key="myqueryKey" timeout="86400"> <cfquery datasource="datasource" name="myquery"> SELECT * FROM tableName </cfquery> </cf_memcached>
Firstly, the variable="myquery" tells memcached what element on the page to cache, this variable will usually be your query name, but you can cache pretty much anything with a variable name on the page, in this case we are caching our query, which you will see also has a name of myquery.
Then we have the key="myqueryKey", this is your unique key for memcached, this key can be any alpha-numeric value you want, but must be unique to your application otherwise you will start pulling out results for other queries. Every time your site calls this custom tag, memcached will check if it has a unexpired key called myqueryKey, if it does it will send that back to your code, if not, it will go to the database, store the resultset and then spit that back to your code.
Finally there is the timeout="86400" attribute, this is just your timeout for the cached record in seconds. In this case, we are caching the query for 86400 seconds (24 hours).
The </cf_memcached> is not required at all, but I like to put it there to contain the item I am caching, this makes things easier to read in the future.
Once this is done, have a look through the cfc and you will see all the other things that memcached can do. I also have a custom tag called memcacheddelete.cfm, which when called, will delete a key from memcached ready for it to be hashed again. There are many other methods in the cfc that are also very useful.