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;
}
// STAT items:8:number 3
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;
}
// key=foobar exp=1596440293 la=1596439293 cas=8492 fetch=no cls=24 size=14908
if (preg_match('!^key=(\S+)!', $line, $matches)) {
$allKeys[] = $matches[1];
$count++;
}
}
// if ($count !== $slabCount) {
// throw new Exception("Surprise, got {$count} keys instead of {$slabCount} keys");
// }
}
if (fclose($sock) === false) {
throw new Exception('Error closing socket');
}
return $allKeys;
}