OPCache のようなパフォーマンス向上の施策を行う際に重要なのは、その施策の効果を適切に計測できることです。 OPCache も例外ではなく、実際にキャッシュがどの程度ヒットしているのかを確認しなければ、チューニングの妥当性を判断することはできません。
OPCache の性能評価には cachetool が役に立ちます。
cachetool には統計情報の取得だけでなく、キャッシュのクリアなどの便利な機能も用意されています。
cachetool とは
cachetool は APCu および OPCache を管理するための CLI ツールです。
gordalina/cachetool: CLI App and library to manage apc & opcache.
https://github.com/gordalina/cachetool
インストール
# curl -sLO https://github.com/gordalina/cachetool/releases/latest/download/cachetool.phar
# chmod +x cachetool.phar
# cachetool.phar --version
CacheTool 9.2.1
使い方
CLI を使用する場合
以下は CLI(PHP CLI SAPI)に対して OPCache の状態を取得した例です。
この例では Cached scripts が 0 となっていることが確認できます。 これは、PHP の CLI 実行環境では通常 opcache.enable_cli=0 が設定されており、OPCache が有効になっていないためです。
# php cachetool.phar opcache:status --cli
+----------------------+---------------------------------+
| Name | Value |
+----------------------+---------------------------------+
| Enabled | Yes |
| Cache full | No |
| Restart pending | No |
| Restart in progress | No |
| Memory used | 8.74 MiB |
| Memory free | 119.26 MiB |
| Memory wasted (%) | 0 b (0%) |
| Strings buffer size | 8 MiB |
| Strings memory used | 2.37 MiB |
| Strings memory free | 5.63 MiB |
| Number of strings | 8160 |
+----------------------+---------------------------------+
| Cached scripts | 0 |
| Cached keys | 0 |
| Max cached keys | 16229 |
| Start time | Mon, 02 Feb 2026 18:26:46 +0000 |
| Last restart time | Never |
| Oom restarts | 0 |
| Hash restarts | 0 |
| Manual restarts | 0 |
| Hits | 0 |
| Misses | 1 |
| Blacklist misses (%) | 0 (0%) |
| Opcache hit rate | 0 |
+----------------------+---------------------------------+
機能・状態に関する項目
- Enabled: Opcache が有効になっているかどうか(Yes なら有効)
- Cache full: キャッシュが満杯かどうか(Yes の場合、これ以上スクリプトをキャッシュできない)
- Restart pending: キャッシュが再起動待ちの状態かどうか(再起動が必要な条件が発生しているが、まだ実行されていない)
- Restart in progress: 再起動中かどうか
メモリに関する項目
- Memory used: 現在 Opcache によって使用されているメモリ量
- Memory free: 残りの空きメモリ量(非常に少ないので要注意)
- Memory wasted (%): 「無駄になっているメモリ」の量と割合(削除されたスクリプトの断片など)
- Strings buffer size: 文字列専用バッファのサイズ(定数などで使われる)
- Strings memory used: 文字列バッファ内で使用中のメモリ量
- Strings memory free: 文字列バッファ内の未使用メモリ(0 b の場合、バッファがいっぱい)
- Number of strings: 文字列インターン(共有文字列)の総数(再利用されている定数名など)
キャッシュに関する項目
- Cached scripts: キャッシュに保存されている PHP スクリプトの数
- Cached keys: キャッシュに使われているハッシュキーの数
- Max cached keys: 許可されている最大ハッシュキー数(これに達すると hash restart が発生する可能性)
時間と再起動
- Start time: Opcache が起動した日時
- Last restart time: 最後にキャッシュがリセットされた日時
- Oom restarts: メモリ不足(Out Of Memory)で再起動した回数
- Hash restarts: ハッシュテーブルのサイズが足りなくなって再起動した回数
- Manual restarts: 明示的な命令(例:opcache_reset())で再起動した回数
HTTP インターフェース(PHP-FPM)を使用する場合
Web サーバー(PHP-FPM)経由で OPCache の状態を取得するには、HTTP インターフェースを利用します。
PHP-FPM 経由では、キャッシュされたスクリプト数やヒット率が確認できます。 CLI 実行時と結果が異なるのは、CLI と PHP-FPM が別の SAPI として動作しており、それぞれ独立した OPCache を持っているためです。
# php cachetool.phar opcache:status \
--web \
--web-path=/var/www/example/public_html \
--web-url=https://example.com
+----------------------+---------------------------------+
| Name | Value |
+----------------------+---------------------------------+
| Enabled | Yes |
| Cache full | No |
| Restart pending | No |
| Restart in progress | No |
| Memory used | 24.72 MiB |
| Memory free | 103.28 MiB |
| Memory wasted (%) | 0 b (0%) |
| Strings buffer size | 8 MiB |
| Strings memory used | 5.66 MiB |
| Strings memory free | 2.34 MiB |
| Number of strings | 50361 |
+----------------------+---------------------------------+
| Cached scripts | 647 |
| Cached keys | 1246 |
| Max cached keys | 16229 |
| Start time | Mon, 02 Feb 2026 18:04:26 +0000 |
| Last restart time | Never |
| Oom restarts | 0 |
| Hash restarts | 0 |
| Manual restarts | 0 |
| Hits | 9323 |
| Misses | 650 |
| Blacklist misses (%) | 0 (0%) |
| Opcache hit rate | 93.482402486714 |
+----------------------+---------------------------------+
Basic 認証を利用する場合
対象の Web アプリケーションが Basic 認証で保護されている場合は、以下のオプションを指定します。
--web-basic-auth=<username>:<password>
Web クライアントを指定する
HTTP クライアントを明示的に指定することも可能です。
--web=SymfonyHttpClient
キャッシュのクリア
OPCache のキャッシュは以下のコマンドでクリアすることができます。
# cachetool opcache:reset
デプロイ後に古いキャッシュが残っている場合などに有効です。
OPCache の効果測定を行う際は、CLI ではなく PHP-FPM を対象に確認する必要がある点に注意してください。