关于PHP的压缩
考虑对PHP进行压缩
在汉语中用中文简洁地表达“背景”这个词,只需要一个翻译选项:情境。
-
- 自社サービスでcacheとしてredisを使ってるもののredisのサイズがバカにならなくなってきた
-
- redisのDBサイズが大きくなってくるとBGSAVE時にi/oが出てサイトが遅延するケース(?)
- redisの使い方が雑なのでお手当してサイズ圧縮したい
将当前状态保存到Redis中
protected function _getUserList() {
$userList = array();
$key = __METHOD__;
$cacheRes = Cache::read($key);
if ( $cacheRes ) {
$userList = $cacheRes;
return $userList;
}
$userList = UserDb::selectUserList()
$ret = Cache::save($key, $userList , 60);
return $userList;
}
# 保存内容は利用者に任せているのでバカでかいarrayとかが保存されがち
进行压缩怎么样?
protected function _getUserList() {
$userList = array();
$key = __METHOD__;
$cacheRes = Cache::read($key);
if ( $cacheRes ) {
$userList = json_decode(gzuncompress($cacheRes), true);
return $userList;
}
$userList = UserDb::selectUserList()
$comp = gzcompress(json_encode($userList))
$ret = Cache::save($key, $comp, 60);
return $userList;
}
尺寸的变化
圧縮有無サイズなし573byteあり187byte
大约能够削减70%左右。
速度的影响如何?
试着读100万遍
$before = microtime(true);
for ( $i = 1; $i<=100000; $i++ ) {
AppGameMaster::getInstance()->isGameMaster($this->userId);
}
$after = microtime(true);
$diff = $after - $before;
AppDebug::dBug($diff);
圧縮有無処理秒数なし2.870146secあり2.798397sec
几乎都是误差。
不同的压缩方式 de
圧縮形式内容計測サイズgzdeflate/gzinflateデルタ圧縮0.2774169sec181bytegzcompress/gzuncompressZLIBフォーマット圧縮0.2798397sec187bytegzencode/gzdecodeGIZPコマンドと同phpVer都合で利用できず—
请参考
你在PHP中使用哪种压缩方法?
杂感 (zá
-
- cacheなのでgzdeflate()あたりがパフォーマンス的には高い
- ただメソッド名が馴染みがないので大規模開発の場合、gzcompressやgzencodeの方が可読性は高いか?