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:

  component {
    public function onServerStart() {
      writeLog(file="myserver",text="Server starting up. Loading Memcached");
      myMail = new mail(to="", from="", subject="Server started", body="The server has started.");