kakakakakku blog

Weekly Tech Blog: Keep on Learning!

APC / OPcache を CLI で管理できる CacheTool を試してみた

CacheTool を使うと PHP のキャッシュ機能である APC / APCu / OPcache を CLI で管理できるようになるという話を今日会社で教えてもらった!さっそく導入してみようと思って調査した.

github.com

背景

例えば,キャッシュをクリアする場合,APC / APCu なら apc_clear_cache()apc_clear_cache('user') を実行すればクリアできるし,OPcache なら opcache_reset() を実行すればクリアできる.

ただし,php-fpm のソケットに接続して実行する必要があるため,現実的にはアプリケーション側に専用のエンドポイントを用意して,接続元をローカルホストに限定して…といった運用上の考慮が必要になったりする.その点が CLI で考慮されてるのが素晴らしい.

GitHub の README にも同じようなメリットが記載してあった.確かに cron 実行するのも良さそう.

Why is this useful?
Maybe you want to clear the bytecode cache without reloading php-fpm or using a web endpoint
Maybe you want to have a cron which deals with cache invalidation
Maybe you want to see some statistics right from the console
And many more…

インストール

インストールは簡単で,READEME の通り.

$ curl -sO http://gordalina.github.io/cachetool/downloads/cachetool.phar
$ chmod +x cachetool.phar

ただ,今回は Chef でウェブサーバにインストールしてみたかったので,レシピを書いた.

remote_file '/usr/local/bin/cachetool.phar' do
  source 'http://gordalina.github.io/cachetool/downloads/cachetool.phar'
  mode '0755'
  action :create_if_missing
end

使えるコマンド群

キャッシュのクリア以外にも幅広く運用に使えるコマンドが用意されている.

Available commands:
  help                        Displays help for a command
  list                        Lists commands
  self-update                 Updates cachetool.phar to the latest version
  selfupdate                  Updates cachetool.phar to the latest version
 apc
  apc:bin:dump                Get a binary dump of files and user variables
  apc:bin:load                Load a binary dump into the APC file and user variables
  apc:cache:clear             Clears APC cache (user, system or all)
  apc:cache:info              Shows APC user & system cache information
  apc:cache:info:file         Shows APC file cache information
  apc:key:delete              Deletes an APC key
  apc:key:exists              Checks if an APC key exists
  apc:key:fetch               Shows the content of an APC key
  apc:key:store               Store an APC key with given value
  apc:regexp:delete           Deletes all APC key matching a regexp
  apc:sma:info                Show APC shared memory allocation information
 apcu
  apcu:cache:clear            Clears APCu cache
  apcu:cache:info             Shows APCu user & system cache information
  apcu:cache:info:keys        Shows APCu keys cache information
  apcu:key:delete             Deletes an APCu key
  apcu:key:exists             Checks if an APCu key exists
  apcu:key:fetch              Shows the content of an APCu key
  apcu:key:store              Store an APCu key with given value
  apcu:regexp:delete          Deletes all APCu key matching a regexp
  apcu:sma:info               Show APCu shared memory allocation information
 opcache
  opcache:configuration       Get configuration information about the cache
  opcache:invalidate:scripts  Remove scripts from the opcode cache
  opcache:reset               Resets the contents of the opcode cache
  opcache:status              Show summary information about the opcode cache
  opcache:status:scripts      Show scripts in the opcode cache
 stat
  stat:clear                  Clears the file status cache, including the realpath cache
  stat:realpath_get           Show summary information of realpath cache entries
  stat:realpath_size          Display size of realpath cache

opcache:statusopcache:reset

opcache:status サブコマンドで,キャッシュの状態を確認することができる.opcache:reset サブコマンドで,キャッシュをクリアすることができる.そして --fcgi オプションで接続する php-fpm のソケットファイルを指定できるようになっている.

今回は Vagrant 上で試してみた.実際に Cached keys などのメトリクスから,正常にキャッシュがクリアされていることを確認できた.

$ sudo /usr/local/bin/cachetool.phar opcache:status --fcgi=/var/run/php-fpm/php-fpm.sock
+----------------------+---------------------------------+
| Name                 | Value                           |
+----------------------+---------------------------------+
| Enabled              | Yes                             |
| Cache full           | No                              |
| Restart pending      | No                              |
| Restart in progress  | No                              |
| Memory used          | 20.18 MiB                       |
| Memory free          | 107.82 MiB                      |
| Memory wasted (%)    | 0 b (0%)                        |
| Strings buffer size  | 8 MiB                           |
| Strings memory used  | 2.14 MiB                        |
| Strings memory free  | 5.86 MiB                        |
| Number of strings    | 21174                           |
+----------------------+---------------------------------+
| Cached scripts       | 389                             |
| Cached keys          | 512                             |
| Max cached keys      | 7963                            |
| Start time           | Tue, 15 Nov 2016 02:35:28 +0000 |
| Last restart time    | Tue, 15 Nov 2016 06:13:53 +0000 |
| Oom restarts         | 0                               |
| Hash restarts        | 0                               |
| Manual restarts      | 2                               |
| Hits                 | 2033                            |
| Misses               | 407                             |
| Blacklist misses (%) | 0 (0%)                          |
| Opcache hit rate     | 83.319672131148                 |
+----------------------+---------------------------------+

$ sudo /usr/local/bin/cachetool.phar opcache:reset --fcgi=/var/run/php-fpm/php-fpm.sock

$ sudo /usr/local/bin/cachetool.phar opcache:status --fcgi=/var/run/php-fpm/php-fpm.sock
+----------------------+---------------------------------+
| Name                 | Value                           |
+----------------------+---------------------------------+
| Enabled              | Yes                             |
| Cache full           | No                              |
| Restart pending      | No                              |
| Restart in progress  | No                              |
| Memory used          | 10.43 MiB                       |
| Memory free          | 117.57 MiB                      |
| Memory wasted (%)    | 0 b (0%)                        |
| Strings buffer size  | 8 MiB                           |
| Strings memory used  | 523.67 KiB                      |
| Strings memory free  | 7.49 MiB                        |
| Number of strings    | 21174                           |
+----------------------+---------------------------------+
| Cached scripts       | 0                               |
| Cached keys          | 0                               |
| Max cached keys      | 7963                            |
| Start time           | Tue, 15 Nov 2016 02:35:28 +0000 |
| Last restart time    | Tue, 15 Nov 2016 12:11:35 +0000 |
| Oom restarts         | 0                               |
| Hash restarts        | 0                               |
| Manual restarts      | 3                               |
| Hits                 | 0                               |
| Misses               | 2                               |
| Blacklist misses (%) | 0 (0%)                          |
| Opcache hit rate     | 0                               |
+----------------------+---------------------------------+

関連記事

5月頃に APC のメトリクスをレポートに出力してパフォーマンス分析などをしていたけど,そのときに CacheTool を知っていたらもう少し別のアプローチが選択できたかも!と思う.

kakakakakku.hatenablog.com

APC → OPcache に切り替えた PHP 5.6 アップデートの話も前に記事を書いた.参考に!

kakakakakku.hatenablog.com