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
|
Show 2 more comments
2 Answers
Reset to default 22As 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
版权声明:本文标题:performance - Any reason why wp_cache_set not to work? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301672a1931259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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