How to start Memcached on ColdFusion startup

Coldfusion 9 offers a nice new feature: the server.cfc.

The server.cfc can be enabled in all CF editions to be called once the server starts. Much like a bootscript or the good’ol autoexec.bat from MS-DOS.

We use Memcached a lot for our projects and always had the problem that each CF server spawned multiple connections to the memcached server. This was because at startup there were already dozens of requests waiting and they all fired in the very same second.

Here’s our server.cfc inspired by Raymond Camdens idea to also handle bad startups:

<cfscript>
  component {
    public function onServerStart() {
      writeLog(file="myserver",text="Server starting up. Loading Memcached");
      server.memcachedFactory=createObject("component","memcached.MemcachedFactory").init('',30);
      myMail = new mail(to="some@email.xyz", from="server@email.xyz", subject="Server started", body="The server has started.");
      myMail.send();
      }
  }
</cfscript>
share