Repository navigation
Object cache
WordPress keeps an object cache: posts, terms, options and query results it has already loaded. Out of the box that cache lives for one request. The object-cache.php drop-in keeps it in Redis, so the next request starts with the work already done.
With an object cache in place WordPress also stores transients there, not in the database.
On the Caching tab, turn on Object Cache and Transient Cache. Transient Cache is the switch that installs the drop-in. See Drop-ins.
Not everything. The drop-in persists the groups that are the same for every visitor and worth keeping:
- posts and post meta
- terms, term meta, term queries and taxonomy relationships
- options (the autoloaded set)
- transients and site transients
- WooCommerce's shared product caches
Other groups stay in memory for the request, as they would without the drop-in. Sessions, notices and counts are never stored.
A plugin can add its own group to the persistent list with the aceredis_oc_persistent_groups filter. The filter runs as WordPress starts its object cache, before ordinary plugins load, so the add_filter() call has to be in a must-use plugin.
This is the part that differs most from what you might expect of an object cache drop-in, and it is deliberate.
| Request | Reads Redis | Writes Redis |
|---|---|---|
| Guest front-end page view | yes | yes |
Anonymous admin-ajax.php request |
yes | yes |
| WP-Cron | yes | yes |
| Signed-in front-end page view | yes, by default | no |
| wp-admin screen | only if you turn it on | no |
| Basket, checkout and Store API | only if you turn it on | no |
| REST API, AJAX from signed-in users, plugin and theme updates | no | no |
Signed-in and admin requests never write to the shared cache, so nothing that belongs to one user can reach another. When such a request changes something, it deletes the shared copy, and the next guest request rebuilds it.
Transients are the exception. They are stored in Redis from every kind of request, because with an object cache WordPress has nowhere else to put them. Without this, a transient set in wp-admin would vanish at the end of the request and every admin page would repeat the remote call it was meant to save.
The three read switches are on the Caching and WooCommerce tabs, and each has a constant that overrides it: ACEREDIS_OC_MEMBER_READ, ACEREDIS_OC_ADMIN_READ and ACEREDIS_OC_WC_READ.
| Limit | Value | Why |
|---|---|---|
| Largest value | 64 KB (32 KB on WooCommerce requests), 1 MB for options | Stops one runaway value filling Redis. A larger value is not stored and is rebuilt when needed. |
| Default lifetime | 1 day | A key written with no expiry would otherwise live for ever |
| Maximum lifetime | 1 day | Caps every write, including transients that ask for longer |
The maximum lifetime is the Object Cache Max TTL setting (60 to 2592000 seconds, or 0 for no cap), or ACEREDIS_OC_MAX_TTL in wp-config.php, which wins. On a Redis shared between sites, a short cap keeps cold keys from crowding out the ones in use.
If you cache something large yourself and it never seems to be stored, the size limit is the reason. Split it, or keep it in a transient under the limit.
Every key carries a generation number. A flush adds one to that number: a single Redis command. Old keys become unreachable at once and are removed in the background a couple of minutes later.
No flush scans the whole database, and nothing in the plugin uses the blocking KEYS command.
The cache is flushed when you switch theme, save the Customizer, or update a plugin, theme or core. Saving a post does not flush it: only that post's own keys and the stamp that post queries depend on are invalidated. Sites that need the old behaviour can define ACEREDIS_OC_FLUSH_ON_POST_CHANGE.
Keys are prefixed with a short hash made from WP_CACHE_KEY_SALT, the database name and the table prefix, so several sites sharing one Redis database keep separate object caches. On multisite each site's keys also carry its blog id, with global groups shared across the network.
The object cache fails open. If the connection cannot be made, the request carries on with an in-memory cache, exactly as WordPress behaves with no drop-in. Connect and read timeouts are short (half a second and under) so a slow Redis cannot hold PHP workers.
To take the object cache out of the picture without touching files:
define( 'ACEREDIS_OC_BYPASS', true );For a single request, add ?aceredis_oc_bypass=1 to the URL.
The Diagnostics tab shows how the drop-in is connected (socket, TCP or TLS), whether the bypass is active, and hit and miss counts. Define ACEREDIS_OC_PROF for timings.
Slow requests can be logged, one line each, with the time taken, whether the visitor was signed in, the cache mode, and the query and Redis counts:
define( 'ACEREDIS_OC_SLOW_LOG', '/var/log/php/ace-requests.log' );
define( 'ACEREDIS_OC_SLOW_LOG_MS', 1500 );A separate, optional cache for wp-admin (off by default). It keeps the results of a few slow queries WordPress runs on every admin list screen (the month filters on posts and media, the custom fields key list, the media type checks) and drops them the moment the data under them changes.
Plugins can use it too:
$rows = aceredis_admin_cache_remember(
'myplugin_report',
function () {
return myplugin_build_report();
},
array( 'posts' )
);
aceredis_admin_cache_bump( 'posts' );When the setting is off, aceredis_admin_cache_remember() simply runs the callback.
Ace Redis Cache: a page cache and object cache for WordPress, backed by Redis or Valkey. Written and maintained by Shane Rounce at AceMedia. Website · Repository · Issues · GPLv2 or later
Setting up
The caches
- Page cache
- Measured image priority
- Object cache
- Logged-in page cache
- Block cache
- Purging and refresh
Your stack
Reference
Help
