The right way to dump slab keys seems to be by using lru_crawler metadump instead of stats cachedump, see https://github.com/memcached/memcached/issues/405
<?php
function getAllKeys(string $host, int $port): array
{
$sock = fsockopen($host, $port, $errno, $errstr);
if ($sock === false) {
throw new Exception("Error connection to server {$host} on port {$port}: ({$errno}) {$errstr}");
}
if (fwrite($sock, "stats items\n") === false) {
throw new Exception("Error writing to socket");
}
$slabCounts = [];
while (($line = fgets($sock)) !== false) {
$line = trim($line);
if ($line === 'END') {
break;
}
if (preg_match('!^STAT items:(\d+):number (\d+)$!', $line, $matches)) {
$slabCounts[$matches[1]] = (int)$matches[2];
}
}
foreach ($slabCounts as $slabNr => $slabCount) {
if (fwrite($sock, "lru_crawler metadump {$slabNr}\n") === false) {
throw new Exception('Error writing to socket');
}
$count = 0;
while (($line = fgets($sock)) !== false) {
$line = trim($line);
if ($line === 'END') {
break;
}
if (preg_match('!^key=(\S+)!', $line, $matches)) {
$allKeys[] = $matches[1];
$count++;
}
}
}
if (fclose($sock) === false) {
throw new Exception('Error closing socket');
}
return $allKeys;
}