2012-03-22 56 views

回答

11

內存緩存> = 2.0.0

function getMemcacheKeys() { 
    $memcache = new Memcache; 
    $memcache->connect('127.0.0.1', 11211) 
     or die ("Could not connect to memcache server"); 

    $list = array(); 
    $allSlabs = $memcache->getExtendedStats('slabs'); 
    $items = $memcache->getExtendedStats('items'); 
    foreach($allSlabs as $server => $slabs) { 
     foreach($slabs AS $slabId => $slabMeta) { 
      $cdump = $memcache->getExtendedStats('cachedump',(int)$slabId); 
      foreach($cdump AS $keys => $arrVal) { 
       if (!is_array($arrVal)) continue; 
       foreach($arrVal AS $k => $v) {     
        echo $k .'<br>'; 
       } 
      } 
     } 
    } 
} 
+2

我只是想在這裏指出,雖然這個工程cachedump是真的意味着用於memcached調試目的,並不打算像這樣使用。未來版本的memcached也可能不支持cachedump。 (請參閱評論8,來自核心memcached貢獻者之一http://code.google.com/p/memcached/issues/detail?id=187) – mikewied 2012-03-25 03:36:38

+1

Cachedump似乎沒有得到所有的密鑰(我一直試圖做到這一點,然後碰到這個鏈接):http://code.google.com/p/memcached/wiki/NewProgrammingFAQ – jonderry 2012-12-19 00:54:28

+0

一個很小但很大的差異,這對我來說是**限制**參數在' getExtendedStats'函數,默認值是_just_ 100. 更新: '$ cdump = $ memcache-> getExtendedStats('cachedump',(int)$ slabId,100000000);' – matija 2016-07-07 10:00:31

0

感謝您的示例代碼

下面是如何

我使用一個輔助類,以除去以除去特定的鍵或多個鍵緩存,所以你必須給函數一個參考memcache連接

public static function removePriceCache(&$memcache, &$cacheAvailable) { 

    if ($cacheAvailable == true) { 
     $list = array(); 
     $allSlabs = $memcache->getExtendedStats('slabs'); 
     $items = $memcache->getExtendedStats('items'); 
     foreach ($allSlabs as $server => $slabs) { 
      foreach ($slabs AS $slabId => $slabMeta) { 
       $cdump = $memcache->getExtendedStats('cachedump', (int) $slabId); 
       foreach ($cdump AS $keys => $arrVal) { 
        if (!is_array($arrVal)) 
         continue; 
        foreach ($arrVal as $k => $v) { 
         //echo $k . '<br>'; 

         if (stripos($k, "Price") !== false) { 
          $memcache->delete($k); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

這將刪除所有包含單詞「價格」在它的鍵..

6

所有這些對內存緩存解決方案,因此在這裏是爲Memcached的

function getMemcachedKeys($host = '127.0.0.1', $port = 11211) 
{ 

    $mem = @fsockopen($host, $port); 
    if ($mem === FALSE) return -1; 

    // retrieve distinct slab 
    $r = @fwrite($mem, 'stats items' . chr(10)); 
    if ($r === FALSE) return -2; 

    $slab = array(); 
    while (($l = @fgets($mem, 1024)) !== FALSE) { 
     // sortie ? 
     $l = trim($l); 
     if ($l == 'END') break; 

     $m = array(); 
     // <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] = array(); 
    } 

    // recuperer les items 
    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) { 
      // sortie ? 
      $l = trim($l); 
      if ($l == 'END') break; 

      $m = array(); 
      // 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; 
     } 
    } 

    // close 
    @fclose($mem); 
    unset($mem); 

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

    return $keys; 
} 
+1

這是目前最好的答案,儘管人們還沒有投票支持它;問題提問者用'd'詢問Memcached。我不知道爲什麼馬克貝克的答案有這麼多upvotes。 – Ryan 2017-04-04 20:47:22

+1

這是正確的答案。 – Apollo 2017-05-18 09:37:07

+1

同意,最佳答案。 – HakRo 2017-09-11 17:41:03

相關問題