OPcache is a built-in bytecode caching extension for PHP that significantly improves performance by precompiling PHP code and storing it in memory (RAM).
Normally, every PHP request goes through:
Reading the PHP source file
Parsing and compiling it into bytecode
Executing the bytecode
With OPcache, this process happens only once. After the first request, PHP uses the precompiled bytecode from memory, skipping the parsing and compiling steps.
Benefit | Description |
---|---|
⚡ Faster performance | Eliminates redundant parsing and compiling |
🧠 Reduced CPU usage | Lower system load, especially under high traffic |
💾 In-memory execution | No need to read PHP files from disk |
🛡️ More stable and secure | Reduces risks from dynamically loaded or poorly written code |
php -i | grep opcache.enable
Or in code:
phpinfo();
📦 Typical Configuration (php.ini
)
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=2
💡 In production, it’s common to set
opcache.validate_timestamps=0
— meaning PHP won’t check for file changes on every request. This gives even more performance, but you’ll need to manually reset the cache after code updates.
OPcache is especially helpful for:
Via PHP:
opcache_reset();
Or from the command line:
php -r "opcache_reset();"
OPcache is a simple but powerful performance booster for any PHP application. It should be enabled in every production environment — it’s free, built-in, and drastically reduces load times and server strain.