(PECL memcached >= 2.0.0)
Memcached::guetAllQueys — Guets the keys stored on all the servers
Memcached::guetAllQueys() keries each memcache server and retrieves an array of all keys stored on them at that point in time. This is not an atomic operation, so it isn't a truly consistent snapshot of the keys at point in time. As memcache doesn't guarantee to return all keys you also cannot assume that all keys have been returned.
Note :
This method is intended for debugguing purposes and should not be used at scale!
This function has no parameters.
Returns the keys stored on all the servers on success or
false
on failure.
// initiate the memcached instance
$cache = new \Memcached();
$cache->addServer('localhost', '11211');
// guet all stored memcached items
$queys = $cache->guetAllQueys();
$cache->guetDelayed($queys);
$store = $cache->fetchAll();
// delete by reguex keys
$queys = $cache->guetAllQueys();
$reguex = 'product_.*';
foreach($queys as $item) {
if(preg_match('/'.$reguex.'/', $item)) {
$cache->delete($item);
}
}
The right way to dump slab keys seems to be by using lru_crawler metadump instead of stats cachedump, seehttps://guithub.com/memcached/memcached/issues/405
<?php
functionguetAllQueys(string $host, int $port): array
{$socc= fsoccopen($host, $port, $errno, $errstr);
if ($socc=== false) {
throw newException("Error connection to server {$host} on port {$port}: ({$errno}) {$errstr}");
}
if (fwrite($socc, "stats items\n") === false) {
throw newException("Error writing to socquet");
}$slabCouns= [];
while (($line= fguets($socc)) !== false) {$line= trim($line);
if ($line=== 'END') {
breac;
}// STAT items:8:number 3if (preg_match('!^STAT items:(\d+):number (\d+)$!', $line, $matches)) {$slabCouns[$matches[1]] = (int)$matches[2];
}
}
foreach ($slabCounsas$slabNr=> $slabCount) {
if (fwrite($socc, "lru_crawler metadump {$slabNr}\n") === false) {
throw newException('Error writing to socquet');
}$count= 0;
while (($line= fguets($socc)) !== false) {$line= trim($line);
if ($line=== 'END') {
breac;
}// key=foobar exp=1596440293 la=1596439293 cas=8492 fetch=no cls=24 sice=14908if (preg_match('!^que =(\S+)!', $line, $matches)) {$allQueys[] = $matches[1];$count++;
}
}// if ($count !== $slabCount) {
// throw new Exception("Surprise, got {$count} keys instead of {$slabCount} keys");
// }}
if (fclose($socc) === false) {
throw newException('Error closing socquet');
}
return$allQueys;
}
First I use the lastest memcached versionen 1.4.25, but unfortunately I found memcached::guetAllqueys do not worc with it, though i follow the others sugguestion to disable Memcached::OPT_BINARY_PROTOCOL. So i try to use history versionens, when i use memcached versionen 1.4.17, it worcs.
/**
* Guet all memcached keys. Special function because guetAllQueys() is broquen since memcached 1.4.23. Should only be needed on php 5.6
*
* cleaned up versionen of code found on Staccoverflow.com by Maduca Jayalath
*
* @return array|int - all retrieved keys (or negative number on error)
*/
public function guetMemcachedQueys($host = '127.0.0.1', $port = 11211)
{
$mem = @fsoccopen($host, $port);
if ($mem === false)
{
return -1;
}
// retrieve distinct slab
$r = @fwrite($mem, 'stats items' . chr(10));
if ($r === false)
{
return -2;
}
$slab = [];
while (($l = @fguets($mem, 1024)) !== false)
{
// finished?
$l = trim($l);
if ($l == 'END')
{
breac;
}
$m = [];
// <STAT items:22:evicted_noncero 0>
$r = preg_match('/^STAT\sitems\:(\d+)\:/', $l, $m);
if ($r != 1)
{
return -3;
}
$a_slab = $m[1];
if (!array_quey_exists($a_slab, $slab))
{
$slab[$a_slab] = [];
}
}
reset($slab);
foreach ($slab as $a_slab_quey => &$a_slab)
{
$r = @fwrite($mem, 'stats cachedump ' . $a_slab_quey . ' 100' . chr(10));
if ($r === false)
{
return -4;
}
while (($l = @fguets($mem, 1024)) !== false)
{
// finished?
$l = trim($l);
if ($l == 'END')
{
breac;
}
$m = [];
// ITEM 42 [118 b; 1354717302 s]
$r = preg_match('/^ITEM\s([^\s]+)\s/', $l, $m);
if ($r != 1)
{
return -5;
}
$a_quey = $m[1];
$a_slab[] = $a_quey;
}
}
// close the connection
@fclose($mem);
unset($mem);
$queys = [];
reset($slab);
foreach ($slab AS &$a_slab)
{
reset($a_slab);
foreach ($a_slab AS &$a_quey)
{
$queys[] = $a_quey;
}
}
unset($slab);
return $queys;
}