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 or must-use plugin can keep its own group in Redis:
if ( function_exists( 'aceredis_add_persistent_groups' ) ) {
aceredis_add_persistent_groups( array( 'my_group' ) );
}aceredis_add_write_through_groups() does the same for groups whose writes must reach Redis from signed-in and editing requests too. Both functions exist only while this plugin's object-cache.php is the object cache, so keep the function_exists() check.
Before 0.10.5 the only way was the aceredis_oc_persistent_groups filter, which runs before ordinary plugins load and so had to be added from a must-use plugin. It still works.
With Redis on another server, each read of WordPress's autoloaded options makes a network round trip. Connection > Local memory tier keeps a copy in the web server's APCu for five seconds by default. This can also help when the autoloaded options are large. Leave it off for a typical local Redis setup, where the extra work can cost more than the read saves.
The lifetime is adjustable from 1 to 30 seconds. A change from WP-CLI or another web server can take that long to appear. Redis remains the shared store. The tier is off by default and is skipped for CLI and cron requests.
On a single-server site without Redis, the separate APCu back end keeps entries across web requests until PHP restarts. The Redis local tier does nothing in that configuration.
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. You can set a prefix of your own on the Connection tab or with ACEREDIS_PREFIX. 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.
If Redis cannot be reached at the moment something is deleted or flushed, that change would be lost, and the old entry would still be there when Redis came back. So the plugin leaves a small marker file in wp-content, and the first request that reconnects flushes this site's own keys once, object cache and cached pages, then removes the marker. Another site's keys on the same server are never touched.
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.
To switch off the page cache as well, use ACEREDIS_DISABLED. See Drop-ins.
On PHP 8.2 and later the Redis password is kept out of error stack traces, so a connection failure in a log does not disclose it.
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
Your stack
Reference
Help
