123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #!php.exe -q
- <?php
- /**
- * 解码CMEM的dump文件, 输出为key作为文件名的txt文件,value作为其文本内容.
- * @author gwang email:wanggangzero@qq.com
- * @copyright ? 2015-6-8, SJZ LoyalSoft Corporation & gwang. All rights reserved.
- * @version 2017.04.27 更新了解析规则, 对方发来的数据带有了4个字节的类型信息如果是0则没有压缩,2采用压缩.
- * 这比原来的数据友好多了, 全部数据完美解析.
- * 2015-6-8 created
- * @description
- * CMEM有两中模式二进制和字符串.一般都用字符串模式,概因效率相差不多.
- * 本脚本内解码算法亦以字符串模式为基础.
- * 原理: php写入Memcache的字符串对象(c格式的即字节数组)将会被转换为hex字符串
- * 再写入memcache, memcache的压缩标志 MEMCACHE_COMPRESSED 指定使用zlib压缩.
- * 如果自己的cmem库中使用了压缩,那么需要在将hex还原为字符串后再解压缩一遍还原为
- * 原来的数组.
- * addition: 即便使用了 MEMCACHE_COMPRESSED 标志位, memcache规定,
- * 压缩比大于0.8的不采用压缩value
- * 将不被压缩. 因此,下文代码中针对这一特殊规定做了处理.
- */
- namespace loyalsoft;
- # 导入主代码库
- include_once __DIR__ . '/../../main.php';
- # 设置时区为中国区(东8区)
- date_default_timezone_set("PRC");
- set_time_limit(0); # cli 不限定执行时间
- //
- // <editor-fold defaultstate="collapsed" desc=" hack for hex2bin">
- if (!function_exists('hex2bin')) { // first include in php 5.4
- function hex2bin($str) {
- return pack('H*', $str);
- // safer方法:2个字节一转换,确保最大限度完成转换.
- $sbin = "";
- $len = strlen($str);
- for ($i = 0; $i < $len; $i += 2) {
- $sbin .= pack("H*", substr($str, $i, 2));
- }
- return $sbin;
- }
- }
- // </editor-fold>
- //
- /**
- * 全局
- * @staticvar CRedisUtil $gRedis
- * @return \CRedisUtil
- */
- function gRedis() {
- static $gRedis;
- if ($gRedis == null) {
- $gRedis = new CRedisUtil();
- $host = '192.168.10.51';
- $port = '6666';
- $pwd = 'wanggang1985';
- $gRedis->conn($host, $port, $pwd);
- }
- return $gRedis;
- }
- function output($key, $value) {
- if (strpos($key, '-info') !== FALSE) { // userinfo
- $userinfo = JsonUtil::decode($value);
- if (property_exists($userinfo, 'user') //
- && property_exists($userinfo->user, 'ts') //
- && (day() - day($userinfo->user->ts) > 60)) { // 距上次登录60天以上了
- // uid, zoneid, tsday, info(bin2hex(gzcompress())
- $uid = $userinfo->user->oId;
- $zoneid = $userinfo->zoneid;
- $tsday = day($userinfo->user->ts);
- $info = base64_encode(gzcompress($value));
- $SQL = sprintf("call mgodpay.insertUserInfo('%s',%d,%d,'%s');", $uid, $zoneid, $tsday, $info);
- daoInst()->exec($SQL);
- echo $key . "\r\n";
- return;
- }
- }
- $redis = gRedis();
- $redis->set($key, $value);
- // $handle = fopen(__DIR__ . "/datas4/" . "$key.txt", "w");
- // fwrite($handle, $value);
- // fclose($handle);
- }
- if ($argc <= 1 # 未传参数的情况下
- || $argv[1] == "-h" || $argv[1] == "\\h") { # 或者是输入\h -h 查询帮助
- echo " usage: php.exe $argv[0] dumpfilename \n";
- echo " output: a txt file that use key as filename,value as contents.\n";
- echo "\t(attension: to avoid too many output in console, filter is suggested.)\n";
- } else {
- $n = 1;
- $filename = $argv[1];
- $zoneid = intval(str_replace('cmem_zone', '', $filename));
- if (!file_exists($filename)) {
- echo " file $filename not exist!";
- } else {
- $handle = fopen($filename, "rb"); # 打开文件
- if ($handle) {
- while (( $line = fgets($handle)) !== false) {
- $strarr = explode(" ", $line);
- $key = hex2bin($strarr[0]);
- // echo "key: $key \n";
- $sec = $strarr[1];
- $type = (int) substr($sec, 0, 8);
- $value = hex2bin(substr($sec, 8));
- if ($type == 2) {
- $value = gzuncompress($value);
- // echo "\n\ndecoded value: \n\n$value\n\n";
- }
- // echo "type: $type\n\ndecoded value: \n\n$value\n\n";
- // echo $key." : ".$value ."\n";
- output("$key-zone$zoneid", $value);
- if ($n++ % 50 == 0) {
- echo "dealed $n records!\n";
- // break;
- }
- }
- if (!feof($handle)) {
- // echo "Error: unexpected fgets() fail\n" ;
- }
- fclose($handle);
- }
- }
- }
|