admin管理员组文章数量:1125072
I'm creating summary descriptions by year. To do this I do update_post_meta
on save to set values for word count, image count, external link count.
So for a year archive page I iterate over each post to create sums and then format the result.
With caching I can be sure to serve cached results to the users.
But during development with caching on I do notice the slowness.
I was considering using a cron like mechanism to create the HTML summaries in a subdirectory of the wp-content
folder or as wp_object_cache
items with a names like summary-YYYY.html
for the summary or summary-YYYY.json
.
My question:
What are my options for storing data like these? Are there any best practices for naming keys in the object cache? Are there other ways to associate arbitrary data with a year?
I'm creating summary descriptions by year. To do this I do update_post_meta
on save to set values for word count, image count, external link count.
So for a year archive page I iterate over each post to create sums and then format the result.
With caching I can be sure to serve cached results to the users.
But during development with caching on I do notice the slowness.
I was considering using a cron like mechanism to create the HTML summaries in a subdirectory of the wp-content
folder or as wp_object_cache
items with a names like summary-YYYY.html
for the summary or summary-YYYY.json
.
My question:
What are my options for storing data like these? Are there any best practices for naming keys in the object cache? Are there other ways to associate arbitrary data with a year?
Share Improve this question edited Mar 3, 2024 at 9:16 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Mar 2, 2024 at 19:21 artlungartlung 3,0976 gold badges29 silver badges37 bronze badges1 Answer
Reset to default 0I settled on using wp_cache_set
and wp_cache_get
which seems to do well. I create a key based on the class, function, and parameters:
$cacheKey = self::createCacheKey( __CLASS__, __FUNCTION__, $referenceYear, $referenceMonth );
createCacheKey()
looks like:
protected static function createCacheKey( ...$args ): string {
return implode('-', $args);
}
And I'm sure to use an appropriate expiration. For dates that are not this year or month I have longer expirations set.
Getting and returning the cache looks like this at the top of a function:
$cacheKey = self::createCacheKey( __CLASS__,
__FUNCTION__,
$referenceYear,
$referenceMonth );
$cache = wp_cache_get( $cacheKey, self::CACHE_GROUP );
if ( $cache ) {
return $cache;
}
And at the end of those functions I use:
wp_cache_set( $cacheKey, $output,
self::CACHE_GROUP, $cacheExpiration );
This seems like it's doing what I want and improves performance though it appears the default place things are stored and how reliable this is depends on server configuration.
本文标签: custom fieldBest Practice for storing aggregate data by date or other criteria
版权声明:本文标题:custom field - Best Practice for storing aggregate data by date or other criteria? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736630411a1945765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论