PHP Conference Japan 2024

Memcached::getAllKeys

(PECL memcached >= 2.0.0)

Memcached::getAllKeys取得所有伺服器上儲存的金鑰

說明

public Memcached::getAllKeys(): array|false

Memcached::getAllKeys() 查詢每個 memcache 伺服器,並檢索當時儲存在它們上面的所有金鑰陣列。這不是一個原子操作,所以它不是當時金鑰的真正一致快照。由於 memcache 不保證會傳回所有金鑰,您也不能假設已傳回所有金鑰。

注意:

此方法旨在用於偵錯目的,不應大規模使用!

參數

此函式沒有參數。

傳回值

成功時傳回儲存在所有伺服器上的金鑰,失敗時傳回 false

新增註解

使用者貢獻的註解 4 個註解

6
flaviu dot chelaru at gmail dot com
7 年前
// 初始化 memcached 實例
$cache = new \Memcached();
$cache->addServer('localhost', '11211');

// 取得所有儲存的 memcached 項目

$keys = $cache->getAllKeys();
$cache->getDelayed($keys);

$store = $cache->fetchAll();

// 依據 regex 金鑰刪除

$keys = $cache->getAllKeys();
$regex = 'product_.*';
foreach($keys as $item) {
if(preg_match('/'.$regex.'/', $item)) {
$cache->delete($item);
}
}
3
xiangku7890 at gmail dot com
8 年前
我首先使用最新的 memcached 版本 1.4.25,但不幸的是,我發現 memcached::getAllkeys 無法使用它,儘管我遵循其他建議停用 Memcached::OPT_BINARY_PROTOCOL。所以我嘗試使用歷史版本,當我使用 memcached 版本 1.4.17 時,它可以運作。
1
danb1974 at gmail dot com
4 年前
傾印 slab 金鑰的正確方法似乎是使用 lru_crawler metadump 而不是 stats cachedump,請參閱 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("連線伺服器 {$host},在 port {$port} 上的錯誤:({$errno}) {$errstr}");
}

if (
fwrite($sock, "stats items\n") === false) {
throw new
Exception("寫入 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('寫入 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('關閉 socket 時發生錯誤');
}

return
$allKeys;
}
1
harold at snel dot me
5 年前
/**
* 取得所有 memcached 金鑰。特殊函式,因為自 memcached 1.4.23 以來 getAllKeys() 已損壞。應該只需要在 php 5.6 上使用
*
* 清理過的程式碼版本,在 Stackoverflow.com 上由 Maduka Jayalath 找到
*
* @return array|int - 所有檢索的金鑰 (或錯誤時為負數)
*/
public function getMemcachedKeys($host = '127.0.0.1', $port = 11211)
{
$mem = @fsockopen($host, $port);
if ($mem === false)
{
return -1;
}

// 檢索不同的 slab
$r = @fwrite($mem, 'stats items' . chr(10));
if ($r === false)
{
return -2;
}

$slab = [];
while (($l = @fgets($mem, 1024)) !== false)
{
// 完成了嗎?
$l = trim($l);
if ($l == 'END')
{
break;
}

$m = [];
// <STAT items:22:evicted_nonzero 0>
$r = preg_match('/^STAT\sitems\:(\d+)\:/', $l, $m);
if ($r != 1)
{
return -3;
}
$a_slab = $m[1];

if (!array_key_exists($a_slab, $slab))
{
$slab[$a_slab] = [];
}
}

reset($slab);
foreach ($slab as $a_slab_key => &$a_slab)
{
$r = @fwrite($mem, 'stats cachedump ' . $a_slab_key . ' 100' . chr(10));
if ($r === false)
{
return -4;
}

while (($l = @fgets($mem, 1024)) !== false)
{
// 完成了嗎?
$l = trim($l);
if ($l == 'END')
{
break;
}

$m = [];
// ITEM 42 [118 b; 1354717302 s]
$r = preg_match('/^ITEM\s([^\s]+)\s/', $l, $m);
if ($r != 1)
{
return -5;
}
$a_key = $m[1];

$a_slab[] = $a_key;
}
}

// 關閉連線
@fclose($mem);
unset($mem);

$keys = [];
reset($slab);
foreach ($slab AS &$a_slab)
{
reset($a_slab);
foreach ($a_slab AS &$a_key)
{
$keys[] = $a_key;
}
}
unset($slab);

return $keys;
}
To Top