wp_cache_get( int|string $key, string $group = , bool $force = false, bool $found = null ): mixed|false

Retrieves the cache contents from the cache by key and group.

Description

See also

Parameters

$keyint|stringrequired
The key under which the cache contents are stored.
$groupstringoptional
Where the cache contents are grouped.

Default:''

$forcebooloptional
Whether to force an update of the local cache from the persistent cache.

Default:false

$foundbooloptional
Whether the key was found in the cache (passed by reference).
Disambiguates a return of false, a storable value.

Default:null

Return

mixed|false The cache contents on success, false on failure to retrieve contents.

Source

function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
	global $wp_object_cache;

	return $wp_object_cache->get( $key, $group, $force, $found );
}

Changelog

VersionDescription
2.0.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Here is how you use the $found parameter in your code – it is passed by reference, so you do not need to define the variable upfront, its value will be set by the called function instead:

    $value = wp_cache_get( 'wpdocs_mykey', 'wpdocs_mygroup', false, $found );
    
    if ( ! $found) {
        $value = wpdocs_my_expensive_function();
        wp_cache_set( 'wpdocs_mykey', $value, 'wpdocs_mygroup' );
    }
    
    return $value;

    You need to use the $found parameter if there is a slightest chance that the value you are caching can be zero, false, null, or otherwise equivalent to false (falsy) like an empty array.

    If you are not sure, or if you are looking for a general rule of thumb – always use the $found parameter.

    If some object caches are not implementing the $found parameter correctly, then that is a good reason to fix those implementations (or use better maintained ones) and not a reason to not use the parameter.

  2. Skip to note 6 content
    function prefix_get_post_count( $post_status = 'publish' ) {
        $cache_key = 'prefix_post_count_'. $post_status;
        $_posts = wp_cache_get( $cache_key );
        if ( false === $_posts ) {
            $_posts = $wpdb->get_var(
                        $wpdb->prepare(
                            "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = %s",
                            $post_status
                        ));
     
            wp_cache_set( $cache_key, $_posts );
        }
     
        return $_posts;
    }
  3. Skip to note 7 content
    /**
     * Handles the users column output.
     *
     * @since 4.3.0
     *
     * @param array $blog Current site.
     */
    public function column_users( $blog ) {
        $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
        if ( ! $user_count ) {
            $blog_users = new WP_User_Query(
                array(
                    'blog_id'     => $blog['blog_id'],
                    'fields'      => 'ID',
                    'number'      => 1,
                    'count_total' => true,
                )
            );
            $user_count = $blog_users->get_total();
            wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
        }
    
        printf(
            '<a href="%s">%s</a>',
            esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ),
            number_format_i18n( $user_count )
        );
    }
  4. Skip to note 8 content

    It seems some cache dropins (like those used on WPEngine) may define `wp_cache_get` WITHOUT the 4th parameter, `$found`. This means `$found` might not be populated by `wp_cache_get() ` (it will always be `null`, regardless of whether the value was cached or not), and you can also get a ton of warnings as reported here and here.

    The workaround? Pretend `$found` isn’t an argument, and never use `wp_cache_set() ` to cache a value of `false` (because you won’t be able to differentiate a cached value of `false` from the lack of a cached value, where `wp_cache_get() ` also returns `false`.)

    Eg
    [PHP]
    $result = wp_get_cache(‘my_key’);
    if( $result === false){
    // calculate the result, and use `0` or `null` instead of `false`
    if( $result === false){
    $result = 0;
    }
    wp_cache_set(‘my_key’, $result);
    }
    [/php]

You must log in before being able to contribute a note or feedback.