# Object Cache Hit Rate in the Admin Bar

> Shows administrators the object cache hit rate, hits and misses for the current page in the admin bar, to check whether Redis or Memcached is actually helping.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: object-cache, performance, admin-bar
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/debug-object-cache-stats/

## Code

```php
// Added just before the bar renders, so most of the page's cache calls are counted.
add_action( 'wp_before_admin_bar_render', function () {
    global $wp_object_cache, $wp_admin_bar;

    if ( ! current_user_can( 'manage_options' ) || ! is_object( $wp_object_cache ) || ! $wp_admin_bar ) {
        return;
    }

    $hits   = isset( $wp_object_cache->cache_hits ) ? (int) $wp_object_cache->cache_hits : 0;
    $misses = isset( $wp_object_cache->cache_misses ) ? (int) $wp_object_cache->cache_misses : 0;
    $total  = $hits + $misses;
    $rate   = $total ? round( $hits / $total * 100, 1 ) : 0;

    $wp_admin_bar->add_node( array(
        'id'     => 'wpide-object-cache',
        'parent' => 'top-secondary',
        'title'  => esc_html( sprintf( 'Cache %s%% %s', $rate, wp_using_ext_object_cache() ? '(persistent)' : '(runtime)' ) ),
        'meta'   => array( 'title' => sprintf( '%d hits, %d misses', $hits, $misses ) ),
    ) );
} );
```

## Notes

Without a persistent object cache the numbers only cover the current request.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Object Cache Hit Rate in the Admin Bar" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
