6.0.0-beta1
7/15/25
Last Modified 2/28/08 by Chuck Hagenbuch
  • Message
    This page is locked by 146.174.167.60 for 10 Minutes.

Memcache Lock Cleanup

With Horde versions before 3.2, the memcache support will generate lock files in your temp directory. This script will clean them out - you can run it from cron nightly. Contributed by Andrew Morgan.

<?php

<?php

# Clean up stale lock files by checking if the session
# still exists in memcache

# Connect to memcache
$memcache = new Memcache;
$memcache->connect('your.memcache.server', 11211);

# Walk through lock files
$dir = "/tmp";
$total = 0;
$removed = 0;
$dh = opendir($dir) or die("Unable to open '$dir'");
while(($filename = readdir($dh)) !== false) {
    if (preg_match('/^lock_([a-z0-9]+)$/', $filename, $matches)) {
        $key = $matches[1];
        $total++;
        #echo "checking key: $key\n";
        if ($memcache->get($key) === false) {
            #echo "$key not found in memcache\n";
            $removed++;
            unlink($dir . "/" . $filename);
        }
    }
}

closedir($dh);

$memcache->close();

echo "$total lock files\n";
echo "$removed lock files removed\n";
?>