Storing complex objects in memcached on Railo

Lately I have been doing a lot of research into making sites run efficiently and to a larger extent, load  more quickly.  Yesterday I installed memcached and had it up and running on a test server.  Once the memcached daemon is installed and ready to go you will just need to install a java client that will work for Railo.  My friend Gabe wrote this post that covers what is required to get this up and running on an ACF server.  Getting it running on Railo is little different, just extract the jar files for the Java memcached client into the directory that contains your railo.jar file.

I had a simple proof-of-concept application up quickly, I was able to set a string value in the cache and pull it back from the cache easily.  Things got a little bit bumpy when I tried to store a complex object in cache.  The result coming back from the cache set was coming back as true which would indicate that the object was successfully set in cache.  When I tried to get the object back from cache and dump it, it always returned a null value.  Logging into memcached through telnet I could see there actually was a value stored for the key but it was not returning.

Since I knew I could store simple values with no problem I decided to take the path of least resistance.  Using the Railo serialize() function I could convert complex objects to a string representation and insert it into the cache.  When I pull the value out of cache I evaluate() it and return the object value.  Since I would end up creating a facade for setting/getting objects in cache this is not really an issue.  I know ACF handles this without the need for a work around so it just left me scratching my head.  I would imagine the difference is how Railo and ACF implements the data types, but I have not had time to verify this although if I have some free time this afternoon I intend to look in to this.

Here is a code sample I wrote to get this going.  Basically it is the same as Gabe’s code with some changes to make it work with Railo.

 serverlist = "";
 serverList = serverlist.split(",");
 pool = createObject("java","com.danga.MemCached.SockIOPool");
 pool = pool.getInstance();
 if(not pool.isInitialized()){
 poolInit = pool.isInitialized();

 memcached = createObject("java", "com.danga.MemCached.MemCachedClient");
 someData = queryNew('a, b');
 queryAddRow(someData, 1);
 querySetCell(someData, 'a', 'apple');
 querySetCell(someData, 'b', 'orange');
 dump(memcached.set("someData", serialize(someData)));

About Robert Zehnder

Web application developer specializing in ColdFusion/Railo and Open Source development.

Posted on April 21, 2010, in memcached, Railo and tagged , . Bookmark the permalink. 6 Comments.

  1. Did you try taking one of the railo .rc files (change the .rc to .jar) and drop it in the lib directory of your j2ee container?

    If I am not mistaken I believe it has something to do with the container not being aware of how to automatically serialize the Railo data types.

  2. I have not I will give that a shot and see if it clears things up. Thanks Gary!

  3. @Gary: The rename/copy of the .rc files into {tomcat}/lib solved the issue. You rock. Thanks!

  4. Question,

    why aren’t you using Railo’s Cache extensions? They allow you to store complex objects with CachePut() and CacheGet()… etc. You can use distributed EHCache or Memcached if you like. It’s part of the extension.


    • Sorry for the delays in the responses, work has been, ah, interesting today. I was doing a PoC for caching in a mixed environment. Although I personally rarely use anything other than Railo (why use anything else? 😉 ) it was pretty much just an exercise.

      I will look into the the cache plug ins. It looks like EhCacheLite is the only thing that is available. I assume setting a RamCache will use a memcached implementation?

  5. Glad it worked out for you! Now i wonder what I did wrong cause ehcache has a similar problem and doing the .rc trick didn’t help…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: