Checks whether a script path is currently present in the OPcache of the active SAPI.
What It Does and When to Use It
Ask whether a specific resolved path is cached in the active 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. CLI and FPM caches are separate; path and cache-warming timing affect results.
Example 1: Basic Operation
Ask whether a specific resolved path is cached in the active SAPI.
<?php
$file = realpath(__FILE__);
if ($file !== false) var_dump(opcache_is_script_cached($file));
Example 2: Behavior and Validation
Check the effective configuration when a file is not cached.
<?php
$settings = opcache_get_configuration()['directives'] ?? [];
echo 'OPcache: ', (int)($settings['opcache.enable'] ?? 0);
Practical Example: Application Use
Compare cached status for the current file and another application script.
<?php
foreach ([__FILE__, __DIR__ . '/bootstrap.php'] as $candidate) {
$resolved = realpath($candidate);
echo basename($candidate), ': ', ($resolved && opcache_is_script_cached($resolved) ? 'cached' : 'not cached'), "
";
}
Important Behavior and Edge Cases
The second example highlights: Check the effective configuration when a file is not cached. The practical example adds: Compare cached status for the current file and another application script.
Comparison with Related Approaches
| Approach | What changes |
|---|---|
| OPcache, JIT and APCu | OPcache caches compiled opcodes, JIT may optimize CPU-intensive PHP, while APCu caches application-owned values. |
| Choosing safely | Profile application response times and memory under representative traffic before tuning. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming a cache is shared across multiple servers | Confirm process- and host-level cache scope; use a distributed store when needed. |
| Never invalidating cached data | Set a TTL or versioned cache key and test release-time invalidation. |
| Skipping operational constraints | CLI and FPM caches are separate; path and cache-warming timing affect results. |
Key Takeaways
- Checks whether a script path is currently present in the OPcache of the active SAPI.
- Compare cached status for the current file and another application script.
- Profile application response times and memory under representative traffic before tuning.