- <?php
- function memcachedump_usage() {
- echo "Usage: memcachedump\\n";
- echo " -h host server host, default localhost\\n";
- echo " -p port server port, default 11211\\n";
- echo " -f filename dump to file, default STDOUT\\n";
- echo " -i import from file\\n";
- echo " --prefix key prefix\\n";
- }
- function memcachedump_description() {
- return "导出/导入 memcached 的数据";
- }
- function memcachedump_entrance($argv) {
- $args = args_parse($argv);
- $is_import = isset($args['i']) ? true : false;
- $mc = memcachedump_connect(isset($args['h']) ? $args['h'] : 'localhost', isset($args['p']) ? $args['p'] : 11211);
- if (!$mc) {
- fwrite(STDERR, "connect memcache error\\n");
- exit();
- }
- if (isset($args['f'])) {
- $file = fopen($args['f'], $is_import ? 'rb' : 'ab');
- if (false === $file) {
- fwrite(STDERR, "can not open file {$file}\\n");
- exit();
- }
- } else {
- $file = STDOUT;
- }
- $prefix = isset($args['prefix']) ? $args['prefix'] : '';
- if ($is_import) {
- memcachedump_import($mc, $file, $prefix);
- } else {
- memcachedump_dump($mc, $file, $prefix);
- }
- fclose($file);
- echo "\\ndone.\\n";
- }
- function memcachedump_connect($host, $port, $timeout = 1) {
- static $pool = array();
- if (!isset($pool["{$host}:{$port}"])) {
- $mc = new Memcache();
- if ($mc->connect($host, $port, $timeout)) {
- $pool["{$host}:{$port}"] = $mc;
- } else {
- return null;
- }
- }
- return $pool["{$host}:{$port}"];
- }
- function pack2DWord($n) {
- $a = (0xFF000000 & $n) >> 24;
- $b = (0x00FF0000 & $n) >> 16;
- $c = (0x0000FF00 & $n) >> 8;
- $d = 0x000000FF & $n;
- return pack('CCCC', $a, $b, $c, $d);
- }
- function unpackDWord($s) {
- $a = (0xFFFFFFFF & ord($s{0})) << 24;
- $b = (0xFFFFFFFF & ord($s{1})) << 16;
- $c = (0xFFFFFFFF & ord($s{2})) << 8;
- $d = ord($s{3});
- return $a + $b + $c + $d;
- }
- /**
- *
- * @param \\Memcache $mc
- * @param type $file
- * @param type $prefix
- */
- function memcachedump_dump($mc, $file, $prefix) {
- $server_slabs = $mc->getExtendedStats('slabs');
- foreach ($server_slabs as $server => $slabs) {
- foreach ($slabs as $slab_id => $slab_meta) {
- if (!is_int($slab_id)) {
- continue;
- }
- $dump = $mc->getExtendedStats('cachedump', (int)$slab_id, (int)$slab_meta['used_chunks']);
- foreach ($dump as $server => $entries) {
- if (!$entries) {
- continue;
- }
- foreach ($entries as $key => $meta) {
- if ($prefix && ($prefix !== substr($key, 0, strlen($prefix)))) {
- continue;
- }
- $val = $mc->get($key);
- if (empty($val)) {
- continue;
- }
- $kl = pack2DWord(strlen($key));
- $vl = pack2DWord((int)$meta[0]);
- fwrite($file, "{$kl}{$vl}{$key}{$val}");
- }
- }
- }
- }
- }
- /**
- *
- * @param \\MemCache $mc
- * @param type $file
- */
- function memcachedump_import($mc, $file) {
- while (!feof($file)) {
- $ks = fread($file, 4);
- if (feof($file) || !isset($ks{3})) {
- continue;
- }
- $vs = fread($file, 4);
- if (feof($file) || !isset($vs{3})) {
- continue;
- }
- $kl = unpackDWord($ks);
- $vl = unpackDWord($vs);
- if (($kl <= 0) || ($vl <= 0)) {
- continue;
- }
- $key = fread($file, $kl);
- if (feof($file)) {
- continue;
- }
- $val = fread($file, $vl);
- if (empty($val)) {
- continue;
- }
- $mc->set($key, $val);
- }
- }
- /**
- * 解释参数,可以解释以下类型:
- * -p
- * -pVALUE
- * -p value
- * --param value
- * -p=value
- * --param=value
- * param=value
- * @param array $argv
- * @return array
- */
- function args_parse($argv) {
- if (!is_array($argv) || empty($argv)) {
- return array();
- }
- $argc = count($argv);
- $ret = array();
- for ($i = 0; $i < $argc; ++$i) {
- $arg = $argv[$i];
- if (strpos($arg, '=') > 0) { // -p=value --param=value param=value
- list($arg_name, $arg_value) = explode('=', ltrim($arg, '-'), 2);
- $ret[$arg_name] = $arg_value;
- continue;
- }
- if ($arg{0} !== '-') {
- continue;
- }
- if (($arg{1} !== '-') && isset($arg{2})) {// -pVALUE
- $ret[$arg{1}] = substr($arg, 2);
- continue;
- } else if (isset($argv[$i + 1]) && ($argv[$i + 1]{0} !== '-') && (false === strpos($arg, '='))) {
- $ret[ltrim($arg, '-')] = $argv[$i + 1];
- ++$i;
- } else {
- $ret[ltrim($arg, '-')] = true;
- }
- }
- return $ret;
- }
- //该片段来自于http://www.codesnippet.cn/detail/230620149856.html
来源: http://www.codesnippet.cn/detail/230620149856.html