Performance and monitoring

What the object cache is and how to enable it in WordPress with Redis

Reviewed on 6 min read WordPress cache Redis

The object cache is the mechanism WordPress uses to store the result of costly operations, such as a heavy SQL query, so it can return it directly the next time without repeating the work.

Imagine a page that needs several slow queries to build a product listing. With an object cache, WordPress stores the result the first time and, on the following requests, reads it from memory instead of querying the database again.

Why the built-in one isn't enough

WordPress comes with its own object cache, but it isn't persistent: it lives in the memory of each request and is lost when the request ends. It avoids repeating a query within the same page load, but the next visit starts from scratch.

Transients (set_transient), which many plugins use to store data for a while, are kept, but without a persistent cache they are stored in the wp_options table. That makes the table grow and can slow down the whole website.

The implementation is replaceable: if the file wp-content/object-cache.php exists, WordPress uses it instead. Object cache plugins install that file to store the data in an in-memory service such as Redis. From then on, both the object cache and the transients are kept between visits and move out of wp_options.

Only plugins and themes that use the WordPress cache functions (wp_cache_get, wp_cache_set) or transients benefit. You will notice more improvement on WooCommerce shops, client areas, courses and in the dashboard; an informational website with a page cache will notice little.

Step 1: enable Redis on the hosting

All plans include Redis for caching:

  1. In the client area, open your hosting and go to Package manager.
  2. Turn on the switch on the Redis Cache card and click Enable Redis Cache.
  3. When the panel confirms it is enabled, open Connection details.

The details are always these:

Field Value
Type Unix socket
Host or path /tmp/redis.sock
Port 0

The website needs the PHP redis extension. If the plugin says it can't find it, enable it in cPanel > Select PHP Version > Extensions, ticking redis for the version the website uses.

Step 2: connect WordPress

Use only one of these two methods: both compete for the same object-cache.php file.

With LiteSpeed Cache

If you already use LiteSpeed Cache for the page cache, use the same plugin:

  1. Go to LiteSpeed Cache > Cache > Object.
  2. Turn on Object Cache (ON).
  3. Under Method, choose Redis.
  4. In Host, type /tmp/redis.sock and in Port, 0.
  5. In Redis Database ID, enter 0 if it is the only website on the hosting, or a different number for each website (from 0 to 15).
  6. Save. At the top of the tab, the connection status should show as passed (Connection Test: Passed).

With Redis Object Cache

If you use another page cache plugin, or none:

  1. Edit wp-config.php and add these lines above /* That's all, stop editing! */:

    define('WP_REDIS_SCHEME', 'unix');
    define('WP_REDIS_PATH', '/tmp/redis.sock');
    define('WP_REDIS_PORT', 0);
    define('WP_REDIS_DATABASE', 0);
    define('WP_REDIS_PREFIX', 'tudominio_');
    

    If you have several websites on the same hosting, change WP_REDIS_DATABASE (from 0 to 15) and the prefix on each one.

  2. Install and activate the Redis Object Cache plugin.

  3. Go to Settings > Redis and click Enable Object Cache.

  4. The status should change to Connected.

Check that it works

  • In Tools > Site Health, the "You should use a persistent object cache" notice disappears.
  • Browse the website and the dashboard for a few minutes and go back to the plugin: the hits should grow and outnumber the misses.
  • Publish or edit a post and check that the change shows on the website.

If the memory keeps filling up, Redis evicts the least used keys and the cache becomes less effective. See the Redis guide for each plan's memory and how to share it between several websites.

Disable it

Always disable it from the plugin: in LiteSpeed Cache set Object Cache to OFF; in Redis Object Cache click Disable Object Cache before deactivating the plugin. That deletes wp-content/object-cache.php. Only switch Redis off in the Package manager afterwards, when no website in the account uses it: if you switch it off first, the website will show a connection error.

If the website is already showing an error because Redis is switched off, delete the file wp-content/object-cache.php from cPanel's File Manager and reload.

You may also find useful

Something wrong or out of date? Tell us and we will review it.