6.0.0-git
2024-12-14
Last Modified 2008-02-28 by Chuck Hagenbuch

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

# 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";