Back to Home

Memcached How to find keys by pattern?

memcached · memcashd · patterns · pattern · memcached · memcache · memcachedb · pattern · patterns · find · keys · key

Memcached How to find keys by pattern?

    Good morning | day | evening | night,% username%!

    When using Memcached , sometimes questions may arise: “But how to see all Memcached keys?” or "How to find all keys by mask" * "or" sql_ * "?"
    Then the manuals open and the search for such a function begins , but, unfortunately, it doesn’t
    turn out :-( Then Google starts ... And there isn’t much there :-(
    And then the search for undocumented features starts :-) and then “Hurray! Found!”

    This code just adds such a simple function as viewing keys, and returns them as an array, with which you can then do whatever you like :-)

    1. /**
    2.  * getKeys()
    3.  *
    4.  *  get all keys from Memcache
    5.  *
    6.  * @author         Roman Kutsy
    7.  * @version        2.0.20090708
    8.  *
    9.  * @return    array     $keys
    10.  */    
    11. function getKeys()
    12.     ///////////////////////////////////////////////////////////////////////
    13.  
    14.     $s = @fsockopen('127.0.0.1',11211);
    15.          
    16.     // SLABS //////////////////////////////////////////////////////////////
    17.     
    18.     fwrite($s, 'stats slabs'."\r\n");
    19.          
    20.     $slabs = array();
    21.     
    22.     while( !feof($s) )
    23.     {
    24.         $temp = fgets($s, 256);
    25.         
    26.         preg_match('/^STAT\s([0-9]*)(.*)/', $temp, $slab_temp);
    27.         
    28.         if(isset($slab_temp['1']) && strlen($slab_temp['1'])>0)
    29.         {
    30.             $slabs[] = $slab_temp['1'];
    31.         }            
    32.         
    33.         unset($slab_temp);
    34.  
    35.         if(trim($temp)=='END')
    36.         {
    37.             break;
    38.         }
    39.     }
    40.  
    41.     unset($temp);
    42.  
    43.     // ITEMS //////////////////////////////////////////////////////////////
    44.     
    45.     fwrite($s, 'stats items'."\r\n");
    46.          
    47.     $items = array();
    48.     
    49.     while( !feof($s) )
    50.     {
    51.         $temp = fgets($s, 256);
    52.         
    53.         preg_match('/^STAT\sitems\:([0-9]*)(.*)/', $temp, $item_temp);
    54.         
    55.         if(isset($item_temp['1']) && strlen($item_temp['1'])>0)
    56.         {
    57.             $items[] = $item_temp['1'];
    58.         }            
    59.         
    60.         unset($item_temp);
    61.  
    62.         if(trim($temp)=='END')
    63.         {
    64.             break;
    65.         }
    66.     }
    67.  
    68.     unset($temp);
    69.     
    70.     $slabs = array_unique($slabs);
    71.     $items = array_unique($items);        
    72.  
    73.     // CACHEDUMP //////////////////////////////////////////////////////////
    74.  
    75.     $keys = array();            
    76.         
    77.     foreach($slabs as &$slab)
    78.     {
    79.         foreach($items as &$item)
    80.         {
    81.             fwrite($s, 'stats cachedump '.$slab.' '.$item."\r\n"); 
    82.             
    83.             while( !feof($s) )
    84.             {
    85.                 $temp = fgets($s, 256);
    86.  
    87.                 // ITEM cd3aec8b1dd7ef828267408e68b6d961:user_1_status [1 b; 1247043297 s]
    88.                 // or
    89.                 // ITEM sql_custom_photos_showphoto_11 [1379 b; 1247064083 s]
    90.  
    91.                 preg_match('/^ITEM\s([a-f0-9]{32}\:)?([A-Za-z0-9\_\-\.]*)\s\[[0-9]*\sb\;\s([0-9]*)\s.*/', $temp, $key_temp);
    92.  
    93.                 if(isset($key_temp['2']) && strlen($key_temp['2'])>0)
    94.                 {         
    95.                   $keys[] = $key_temp['2'];
    96.                 }            
    97.                 
    98.                 unset($key_temp);
    99.             
    100.                 if(trim($temp)=='END')
    101.                 {
    102.                     break;
    103.                 }                
    104.             }            
    105.         }
    106.     }
    107.     
    108.     unset($temp,$slabs,$items);
    109.     
    110.     fclose($s);
    111.     
    112.     ///////////////////////////////////////////////////////////////////////        
    113.          
    114.     $keys_temp = array_unique($keys);
    115.     unset($keys);        
    116.     asort($keys_temp);
    117.     
    118.     $keys = array();
    119.     foreach($keys_temp as &$k)
    120.     {
    121.         $keys[] = $k;
    122.     }
    123.     
    124.     return $keys;        
    125.     
    126.     ///////////////////////////////////////////////////////////////////////        
    127. }

    Good luck,% username%!

    UPD Moved to " Web-development "

    Read Next