Skip to main content
PhpFileCache writes every cache item as a PHP file that returns an array. OPcache compiles that file once and keeps it in shared memory, so a warm read costs neither file I/O nor JSON decoding. Under PHP-FPM, where every request builds the router anew, that is the difference between reading and decoding the route index on every request and getting it for free.

When to use it

Use it for data that is written rarely and read on every request, above all the router cache:
Keep frequently changing data, such as rate limit counters, in FilesystemCache or a memory store. Every write invalidates the OPcache entry of that file, so a value that changes on every request gains nothing and keeps OPcache busy. In a FrankenPHP worker the router already keeps the decoded index in memory, so PhpFileCache mainly pays off for the first request of every worker and under FPM.

What it stores

Only scalars, arrays and null, because the file is generated with var_export(). Setting an item whose value contains an object throws an InvalidArgumentException. The route index, the serialized routes and the JWKS key set all fit.

Requirements and safety

  • OPcache is part of every PHP 8.5 build. Make sure it is enabled for your SAPI, otherwise every read includes and compiles the file.
  • Files are named <sha256 of the key>.cache.php; clear() only removes files with that suffix, so a shared directory is safe.
  • Whoever can write to the cache directory can run code. Keep it outside the web root and writable only by the PHP user. A directory PhpFileCache creates itself gets mode 0750.
  • Writes, misses and increment() behave like in FilesystemCache: atomic renames under a random temporary name, a vanished or corrupt file is a miss (a corrupt one is removed), an existing file that cannot be read throws a RuntimeException, and increments are serialised by .increment.lock in the cache directory.