Memcached with Unix Sockets in W3 Total Cache

Wrote a quick patch to allow using unix socket connections to Memcached for W3 Total Cache (for a new product line at work):

diff -ur w3-total-cache.orig/lib/W3/Cache/Memcached.php w3-total-cache/lib/W3/Cache/Memcached.php
--- w3-total-cache.orig/lib/W3/Cache/Memcached.php	2011-08-26 01:52:28.000000000 -0400
+++ w3-total-cache/lib/W3/Cache/Memcached.php	2011-11-04 16:47:06.356716709 -0400
@@ -33,7 +33,13 @@
             foreach ((array) $config['servers'] as $server) {
                 list($ip, $port) = explode(':', $server);
-                $this->_memcache->addServer(trim($ip), (integer) trim($port), $persistant);
+                $ip = trim($ip);
+                $port = (integer) trim($port);
+                if( @filetype($ip) === 'socket' ) {
+                    $port = 0;
+                    $ip = 'unix://' . $ip;
+                }
+                $this->_memcache->addServer($ip, $port, $persistant);
         } else {
             return false;

Use by changing to the W3TC plugin directory and running the patch command with -p1:

$ cd wp-content/plugins/w3-total-cache/
$ curl -s | patch -p1

Then you can run by setting the memcached server to “/path/to/socket.sock:0” in W3TC’s settings.

I’ve only tested this against the latest version ( but it should probably work against older versions, though I have no idea how far back. In retrospect, it would probably be better to just check if the first character of the IP is a ‘/’ (who would use a relative path to a socket?) to avoid the stat call every time. Oh well.