Content · Web Development · PHP

PHP Opcache Preloading Explained

OPcache preloading loads selected classes/functions during PHP-FPM startup so they persist for its lifetime.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

OPcache preloading loads selected classes/functions during PHP-FPM startup so they persist for its lifetime.

What It Does and When to Use It


Count preloaded script paths from the active OPcache status; status may be unavailable in the CLI SAPI. OPcache caches compiled opcodes, JIT may optimize CPU-intensive PHP, while APCu caches application-owned values.

Requirements, Inputs and Expected Behavior


Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Preloaded code changes require process restart; preloading is not the same as ordinary opcode caching.

Example 1: Basic Operation


Count preloaded script paths from the active OPcache status; status may be unavailable in the CLI SAPI.

<?php
$status=opcache_get_status(false);
$preloaded=$status['preload_statistics']['scripts'] ?? [];
echo 'Preloaded scripts: ',is_array($preloaded)?count($preloaded):0;

Example 2: Behavior and Validation


The generated preload.php must refer to actual files and be configured via opcache.preload; writing it alone does not preload anything.

<?php
$preload = <<<'PHP'
<?php
require __DIR__ . '/src/Domain/Order.php';
opcache_compile_file(__DIR__ . '/src/Domain/Invoice.php');
PHP;
file_put_contents(__DIR__ . '/preload.php', $preload);

Practical Example: Application Use


Configure FPM preloading with an executable script; changes require restarting FPM workers.

opcache.enable=1
opcache.preload=/srv/app/preload.php
opcache.preload_user=www-data

Important Behavior and Edge Cases


The second example highlights: The generated preload.php must refer to actual files and be configured via opcache.preload; writing it alone does not preload anything. The practical example adds: Configure FPM preloading with an executable script; changes require restarting FPM workers.

Comparison with Related Approaches


ApproachWhat changes
OPcache, JIT and APCuOPcache caches compiled opcodes, JIT may optimize CPU-intensive PHP, while APCu caches application-owned values.
Choosing safelyProfile application response times and memory under representative traffic before tuning.

Common Beginner Mistakes


MistakeCorrect approach
Assuming a cache is shared across multiple serversConfirm process- and host-level cache scope; use a distributed store when needed.
Never invalidating cached dataSet a TTL or versioned cache key and test release-time invalidation.
Skipping operational constraintsPreloaded code changes require process restart; preloading is not the same as ordinary opcode caching.

Key Takeaways


  • OPcache preloading loads selected classes/functions during PHP-FPM startup so they persist for its lifetime.
  • Configure FPM preloading with an executable script; changes require restarting FPM workers.
  • Profile application response times and memory under representative traffic before tuning.
Share this resourceSend this page to someone who may find it useful.