Nodea — logo

OPcache

OPcache is an accelerator built into the PHP engine that keeps the compiled bytecode (opcode) of scripts in shared memory. As a result PHP no longer has to read, parse and compile the same source files on every request — from the second call onward the server pulls ready-made opcode straight from RAM.

How OPcache works

By default PHP is an interpreted language: on each request it reads the .php files, translates them into intermediate opcode and only then executes them. That compilation step repeats over and over even though application code rarely changes. OPcache breaks the cycle — after the first compilation it stores the resulting opcode in memory and skips the entire parsing stage on subsequent calls.

The cache is periodically checked for file changes (based on timestamps), so PHP automatically recompiles modified scripts after a code update. In production the timestamp check is often disabled (validate_timestamps=0) for maximum performance, with the cache cleared manually during deployment. It is worth distinguishing OPcache from data caches: Redis and Memcached cache query results and sessions, whereas OPcache concerns compiled code only.

Practical application

OPcache is one of the most effective and simplest ways to speed up a PHP application — it can cut page generation time by two or three times with virtually no code changes. The gain is especially visible in large CMS platforms and frameworks that load hundreds of files on every request.

On shared hosting and in prebuilt PHP images OPcache is usually enabled by default, often paired with PHP-FPM. On your own VPS an administrator can tune the buffer size and file count to the scale of the application, translating directly into shorter response times and lower CPU usage.

Powiązane pojęcia

Najczęstsze pytania

Do I have to enable OPcache myself?

On most hosting and standard PHP images OPcache is built in and enabled by default — just check its status in phpinfo. On your own server it is worth tuning, especially memory_consumption and max_accelerated_files, so the cache can hold all your application's files. A buffer that is too small forces constant eviction and recompilation, which cancels the benefit.