ColdFusion and Memcache

April 20th, 2010 | Categories: Coding, ColdFusion | Tags:

I’ve been playing around a lot with integrating ColdFusion and Memcache together over the past week. It’s a pretty straight forward integration using the Java Memcache Client. Since I couldn’t find much documentation, outside of the Java docs, I thought I’d give a few examples of how to get ColdFusion to use a Memcache pool.


First, you’ll need to install Memcache on your server, or on another server. Memcache by default listens on all interfaces on port 11211. You can change this, and bind it to localhost. If you need a cluster of Memcache servers, this client will also handle the pooling feature for you.


Next, place the jar file in the CFX/Java directory and restart ColdFusion. The location of the jar file differs from version to version of CF, and other CFML application engines. Basically, place the jar file where any other custom jar files go for your version of CF. Don’t forget to restart ColdFusion, so it picks up the new jar file.


Now for some example code of how to use the jar file:


You will need to initialize the pool. I suggest doing this in your request manager, or making a method that will check on each memcache call to verify that it is initialized. I have simple method that gets called in my request manager on each page load.




serverlist = "127.0.0.1:11211";
serverList = serverlist.split(",");
pool = createObject("java","com.danga.MemCached.SockIOPool");
pool = pool.getInstance();
if(not pool.isInitialized()){
pool.setServers(serverList);
pool.initialize();
poolInit = pool.isInitialized();
}




The next 3 examples are just examples of how to get/set/delete keys. This is obviously pseudo code, but show how simple it is to use the memcache client. If you load the memcache java object, and dump it, you will see all the available methods.



var memcached = createObject("java","com.danga.MemCached.MemCachedClient");
memData = memcached.get(keyName);


var memcached = createObject("java","com.danga.MemCached.MemCachedClient");
memcached.set(keyName, dataToCache);


memcached = createObject("java","com.danga.MemCached.MemCachedClient");
memcached.delete("keyName");//delete a specific key
memcached.flushAll();//delete all keys

  1. April 22nd, 2010 at 07:29
    Reply | Quote | #1

    Did you try the cfmemcached (or something like that) project? Or you had any particular reasons to write this code yourself?

  2. gabriel
    April 24th, 2010 at 13:10
    Reply | Quote | #2

    We rarely use 3rd party code at work, even for something like this, which does have a supported project. Also, I have been working with it inside of my framework, and this is the simplest implementation that works inside of it. Just like at the shop I work at, I tend to not like project code because most of the time, it does lots of things I don’t want it to do, in an attempt to do everything for everyone. Also, I’m so use to the coding standards we have, that whenever I look at most project code, I cringe in how at how inefficient it can be.