How Docket Cache Uses OPcache
Redis and Memcached keep the object cache in memory, in a separate server. Docket Cache keeps it in memory too, but without a separate server. It does this with OPcache, a part of PHP that most hosts already turn on. This post explains how that works in simple terms, and what to check so it keeps working well.
What OPcache does#
PHP does not run your code straight from the file. Each time a PHP file is used, PHP reads it from disk, checks the code and turns it into small instructions called opcodes. Only then does it run them. Without OPcache, PHP does this work again for every file on every page load, and throws the result away at the end.
OPcache keeps the opcodes in shared memory, the first time a file is used. On the next page load, PHP takes them from memory and runs them at once. There is no disk read, and no work to turn the code into opcodes again. A WordPress page uses hundreds of PHP files, so this saves a lot of time.
Why the cache is saved as PHP files#
Many file caches save each value with serialize(). To read it back, they read the file from disk and call unserialize() to turn the text into a value again. That happens on every page load.
Docket Cache saves each value as a small PHP file that returns it. This is a real cache file from one of our test sites. It holds the user_count option.
<?php
return array (
'timestamp' => 1791993977,
'site_id' => 1,
'group' => 'options',
'key' => 'user_count',
'type' => 'string',
'sv' => 3,
'timeout' => 1793203577,
'data' => '1',
);
/*@DOCKET_CACHE_EOF*/
To PHP, this is an ordinary PHP file, so OPcache keeps it in memory like any other. When WordPress asks for this value again, Docket Cache includes the file, and the array comes straight from memory. Nothing is read from disk, and nothing is unserialized.
This is how Docket Cache gets the speed of an in-memory cache without a Redis or Memcached server. The Data Storage page explains each field in the file.
What Docket Cache does after writing a file#
Docket Cache does not wait for the next page load to fill OPcache. Right after it writes a cache file, it asks OPcache to compile it. So the value is in memory before anyone asks for it.
OPcache does not cache a file that was changed less than two seconds ago, because the file might still be being written. This is the opcache.file_update_protection setting. Docket Cache writes each file completely before it uses it, so it sets the file time a minute back to tell OPcache the file is ready.
When a value changes or is deleted, Docket Cache tells OPcache to drop the old copy, so the next read uses the new file.
When OPcache memory is full#
OPcache gets a fixed block of memory when PHP starts, 128 MB by default. It does not grow. Your cache files and all the PHP files of WordPress, your theme and your plugins share that block.
On one of our test sites, the Docket Cache Overview screen showed this.
- Object OPcache Stats were 179K of memory for 71 cache files.
- WP OPcache Stats were 31M of memory for 1007 WordPress files.
Each cache file is small, but a busy site can have tens of thousands of them. Three limits matter.
- Memory. When the block is full, new files are not kept in memory. PHP compiles them again on every page load, as if OPcache were off.
- Number of files. By default, OPcache has room for about 16,000 files, and that number covers WordPress and the cache files together. Docket Cache allows up to 50,000 cache files by default.
- Wasted memory. When a file changes, OPcache does not free the memory of the old copy. It marks it as wasted. When too much is wasted, OPcache empties itself and starts again, and the site is slower until it fills up. Cache files change much more often than plugin code, so this can happen more often on a site with an object cache.
What to check#
Open Docket Cache > Overview and look at Object OPcache Stats and WP OPcache Stats. If the numbers stay low while the cache files grow, OPcache may be full. For more detail, turn on the OPcache Viewer option in Docket Cache > Configuration. It shows the used, free and wasted memory, and the hit rate.
If you can change your PHP settings, this is a good place to start for a WordPress site with Docket Cache.
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=65407
opcache.max_wasted_percentage=10
Restart PHP after you change them. On shared hosting you often cannot change these settings. In that case, lower the number of cache files instead, so they fit in what your host gives you.
define('DOCKET_CACHE_MAXFILE', 10000);
What if there is no OPcache?#
Docket Cache still works. The cache files are then read from disk and compiled on each page load, which is slower than reading them from memory. If your host says OPcache is on but the Overview screen says it is not available, your host may have blocked the functions Docket Cache uses to check it. The FAQ explains what to ask your host.
Read more#
- OPcache Extension explains each OPcache setting in detail, and how to watch its memory.
- OPcache Optimisations explains what OPcache does to the code before it keeps it, and why the JIT does not help cache files.