OPcache: Bytecode Caching
Enable and tune OPcache to eliminate repeated script compilation overhead.
OPcache: Bytecode Caching is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
How PHP Normally Works
Without OPcache, every request causes PHP to: read the .php file from disk, parse it into an AST, compile it to opcodes (bytecode), then execute the opcodes.
What OPcache Does
OPcache stores compiled opcodes in shared memory. On subsequent requests, PHP skips the parse and compile steps — jumping straight to execution.
Performance Impact
OPcache typically provides 2-5x throughput improvement for PHP applications with no code changes required.
Enabling OPcache
OPcache is bundled with PHP 5.5+ and enabled by default in many distributions. Check with php -m | grep -i opcache.
# php.ini:
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=0Key OPcache Settings
Tune these settings for production:
# php.ini:
opcache.memory_consumption=256 ; MB of shared memory
opcache.interned_strings_buffer=16 ; MB for interned strings
opcache.max_accelerated_files=20000 ; max scripts to cache
opcache.validate_timestamps=0 ; disable for production!
opcache.revalidate_freq=0 ; how often to check timestampsvalidate_timestamps=0
In production, set validate_timestamps=0 to disable filesystem checks on every request — PHP will use the cached opcode always. After deployment, reset the cache.
Resetting OPcache
Clear the cache after deploying new code.
<?php
opcache_reset(); // from a PHP script
// Or restart PHP-FPM:
$ kill -USR2 $(cat /run/php-fpm.pid)opcache_get_status()
Query OPcache statistics at runtime to monitor hit rate and memory usage.
<?php
$status = opcache_get_status();
echo "Hit rate: ".$status["opcache_statistics"]["opcache_hit_rate"]."%";Preloading (PHP 7.4+)
OPcache preloading loads framework files into shared memory at server start — eliminating even the first-request compile overhead for core classes.
# php.ini:
opcache.preload=/srv/app/preload.php
opcache.preload_user=www-dataJIT Compilation (PHP 8.0+)
PHP 8 introduced JIT (Just-In-Time) compilation on top of OPcache, converting opcodes to native machine code. Greatest benefit for CPU-intensive tasks; marginal for typical I/O-bound web apps.
# php.ini:
opcache.jit_buffer_size=100M
opcache.jit=1255Immutable Classes
OPcache can share interned strings and immutable class definitions across worker processes — reducing per-process memory consumption.
Summary
Enable OPcache in production with validate_timestamps=0. Clear the cache after deployments with opcache_reset() or PHP-FPM restart. Monitor the hit rate with opcache_get_status().
Quick Check
What php.ini setting should be 0 in production for maximum OPcache performance?
Frequently asked questions
Is the “OPcache: Bytecode Caching” lesson free?
Yes — the full text of “OPcache: Bytecode Caching” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “OPcache: Bytecode Caching”?
Enable and tune OPcache to eliminate repeated script compilation overhead. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “OPcache: Bytecode Caching” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Profiling PHP with Xdebug
- OPcache: Bytecode Caching
- Optimizing Database Queries
- Memory Management and Performance Tips