admin管理员组

文章数量:1122826

Any reason why wp_cache_set not to work? I have spun my wheels trying to figure out why these are not working. Any suggestions? These functions are supposed to help me cache the results to a key/object and then leverage the key/object to display the info. However, they are not storing key/object

$related_post_ids = wp_cache_get( 'related_post_ids' );
if ( false === $related_post_ids ) {
//seting args and run query
$the_query = new WP_Query( $args );
wp_cache_set('related_post_ids', $the_query, '', 300 );
}

Any reason why wp_cache_set not to work? I have spun my wheels trying to figure out why these are not working. Any suggestions? These functions are supposed to help me cache the results to a key/object and then leverage the key/object to display the info. However, they are not storing key/object

$related_post_ids = wp_cache_get( 'related_post_ids' );
if ( false === $related_post_ids ) {
//seting args and run query
$the_query = new WP_Query( $args );
wp_cache_set('related_post_ids', $the_query, '', 300 );
}
Share Improve this question edited Oct 27, 2017 at 14:49 techtransferportal asked Oct 27, 2017 at 14:11 techtransferportaltechtransferportal 411 gold badge1 silver badge6 bronze badges 7
  • 1 Please elaborate "Not working". What is the problem here? What are you trying to do that you can't get it to work? – Johansson Commented Oct 27, 2017 at 14:22
  • @JackJohansson These functions are supposed to help me cache the results to a key/object and then leverage the key/object to display the info. However, they are not storing key/object , hence not caching – techtransferportal Commented Oct 27, 2017 at 14:50
  • How do you try to retrieve these values? You might be using a hook that is fired before saving the cache. – Johansson Commented Oct 27, 2017 at 14:52
  • 1 @techtransferportal wp_cache_*() is non-persistent. It will be cleared whenever you refresh the page or browser to a new page. For more information read WP Object Cache. – Howdy_McGee Commented Oct 27, 2017 at 14:53
  • Hi @JackJohansson No hooks used, just the above – techtransferportal Commented Oct 27, 2017 at 15:09
 |  Show 2 more comments

2 Answers 2

Reset to default 22

As of WordPress 6.3, both WP_Query and WP_User_Query automatically cache requests. The original question example is outdated since get_posts() uses WP_Query.


The wp_cache_*() functions are non-persistent caches which will not carry over to the next page request. These functions take advantage of the WP_Object_Cache class ($wp_object_cache global). Non-persistent caches are be beneficial if you request the same information multiple times during the current page request. For example, if you're retrieving information from an API, you may want to cache the results.

If you're looking to preserve the cache through multiple page loads but optionally expire them after a particular time, you could use transients. Transient cache uses the WP_Object_Cache class but saves the data to the options table for a set time.

I guess you are thinking that this function should set a json object in browser local storage or cache storage. But this is not the case. This function still cache but in a non-persistent way. Following example may help you understand it better.

I have 5 posts displaying on my homepage.

When page is refreshed 5 posts are loaded which loads template part 'template-parts/content/content.php'.

Now I have created my own function to load the template for the first time and cache it for rest of 4 times.

$cache_key = 'home-template-parts';
$template = wp_cache_get($cache_key, 'template_cache_group');
if (!$template) {
    $template = locate_template(
        array(
            template_path() . "{$slug}-{$name}.php",
            template_path() . "{$slug}.php",
        ),
        false,
        false
    );
    wp_cache_set($cache_key, $template, 'template_cache_group');
}
load_template($template, false);

Similarly WordPress loop, database queries or anything that is loading multiple times can be cached.

wp_transient API

On other hand if you do not use wp_transient api carefully your database wp_options table will grow slowly over the time. And if table size is large your wp_transient api will be slow.

本文标签: performanceAny reason why wpcacheset not to work