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

    Good luck,% username%!

    UPD Moved to " Web-development "

    Read Next