2012/07/02

[PHP] Memcache 簡易スクリプト

◎簡単な 確認やら、セットやらできるやつ

→ コマンドラインから、php -f chk_memcached.php get_list みたいに実行

・get_list 表示例

# php -f chk_memcached.php get_list
==============================
command:get_list
==============================
* = old cache
---------------
expire = 0  | size =       10 | cache_key = foo
expire = 0  | size =       20 | cache_key = hoge
---------------
key count:2
     total cache size:        30byte
*old total cache size:         0byte


#define('MEM_HOST', 'localhost');
define('MEM_HOST', '127.0.0.1');
define('MEM_PORT', 11211);

if ($argc < 2) {
  showUsage();
  exit;
}

$output = array();
$keys   = array();
$value  = '';

//Memcache
$memcache_obj = new Memcache;
$memcache_obj->connect(MEM_HOST, MEM_PORT);

array_shift($argv);
$command = array_shift($argv);

switch ($command) {
  case 'get':
    $keys = $argv;
    
    $re = $memcache_obj->get($keys);
    break;

  case 'get_list':
    $re = getKeyList($memcache_obj);
    break;

  case 'set':
    $keys = array_shift($argv);
    $value = array_shift($argv);
    
    $re = $memcache_obj->set($keys, $value);
    break;

  case 'delete':
    $keys = array_shift($argv);
    
    $re = $memcache_obj->delete($keys);
    break;

  default:
    showUsage();
    break;
}

$output[] = '==============================';
$output[] = 'command:' . $command;
if (is_array($keys)) $keys = join(',', $keys);
if ($keys)  $output[] = 'key(s):' . $keys;
if ($value) $output[] = 'value:' . $value;
$output[] = '==============================';
$output[] = '';

echo join("\n", $output);

if (is_array($re)) var_dump($re);
else echo $re;
echo "\n";


function showUsage() {

  $usage = basename(__FILE__) . " -- Usage >>>>\n";
  $usage.= "    :get_list\n";
  $usage.= "    :get [key1] [key2] [key3] ...  [keyN]\n";
  $usage.= "    :set [key] [value]\n";
  $usage.= "    :delete [key]\n";

  echo $usage;

}


/**
 * 統計情報からすべてのキーを抜き出す
 */
function getKeyList($memcache_obj) {
  $result = array();
  $tmp_keys = array();
  $all_keys = array();
  $total = 0;
  $old_total = 0;

  $slabs = $memcache_obj->getExtendedStats('slabs');

  $stats = $memcache_obj->getStats();
  $memcached_started_time = $stats['time'] - $stats['uptime'];

  $stats_item = $memcache_obj->getStats('items');
  $items = $stats_item['items'];
  foreach ($items as $slabclass => $item) {
      $stats_cachedump = $memcache_obj->getStats('cachedump', $slabclass, $item['number']);
      $tmp_keys = array_merge($tmp_keys, $stats_cachedump);
  }

  ksort($tmp_keys);
  foreach ($tmp_keys as $cache_key => $v) {
    $size = (int)$v[0];
    $time = ($v[1] == $memcached_started_time)? 0: $v[1];
    $expire = (!$time)? $time: date('Y/m/d H:i:s', $time);
    $old_flg = ($time && $time < time())? '*': ' ';
    $total+=$size;
    if ($old_flg == '*') $old_total+=$size;
    $all_keys[] = 'expire = ' . $expire . $old_flg . ' | size = ' . sprintf("%8s", $size) . ' | cache_key = ' . $cache_key;
  }



  $result[] = '* = old cache';
  $result[] = '--------------- ';
  $result[] = join("\n", $all_keys);
  $result[] = '--------------- ';
  $result[] = 'key count:' . count($all_keys);
  $result[] = '     total cache size:' . sprintf("%10s", $total) . 'byte';
  $result[] = '*old total cache size:' . sprintf("%10s", $old_total) . 'byte';

  return join("\n", $result);
}