An object cache stores the results of database queries in memory so pages need fewer queries to load.
An object cache is a store in memory where your application keeps pieces of data it has already worked out, so it can reuse them instead of asking the database again. The "objects" are things like a list of menu items, a user's profile, a product's price or the result of a complicated query.
For example, WordPress might load your site's settings, menus and widget configuration on every single page view. Without an object cache, that's dozens of MySQL queries per page. With one, the first page view runs those queries and saves the results in memory, and the next thousand page views read them back almost instantly.
How an object cache works
- Your application needs some data and checks the object cache first.
- If the data is there (a hit), it's used straight away.
- If it isn't (a miss), the application queries the database, uses the result and saves a copy in the cache.
- When the data changes, the application deletes or updates the cached copy so visitors don't see old information.
The cache lives in a fast in-memory service, usually Redis or Memcached. WordPress has a built-in object cache, but by default it only lasts for a single request. A persistent object cache keeps data between requests, which is where the real gains come from.
Object cache vs page cache
These two work at different levels:
- A page cache saves the whole finished HTML page and serves it to the next visitor without running PHP at all. It's very fast but only works for pages that look the same for everyone.
- An object cache saves smaller pieces of data. It still runs PHP, but with far fewer database queries.
That's why an object cache matters most on pages a page cache can't help with: logged-in dashboards, carts, checkouts, account pages, searches and the WordPress admin. Most fast sites use both. See cache for the other types.
Why an object cache matters for your website
Database queries are often the slowest part of building a dynamic page. Cutting them reduces server response time, lowers load on the database and lets the same server handle more visitors before you need more PHP workers or a bigger plan.
Common object cache problems
- Stale data when a plugin changes the database without clearing the related cache entries. Flushing the object cache usually fixes it.
- Too little memory, so useful entries are pushed out before they're reused.
- Caching too much, such as huge options that slow every request down.
- Two plugins fighting over the same object cache drop-in file.