Wordpress 是否加载缓存函数,例如 wp_get_nav_menu_object?
我已经问自己这个问题有一段时间了。也许有人已经做了一些挖掘(或参与了 WP)来知道答案。
我正在谈论在页面加载期间将 WP 函数中的对象存储在 PHP 变量中,例如,以避免为同一结果集查询数据库两次。
我并不是指的是预渲染动态页面并将其保存为 HTML 格式以便更快检索的意义上的缓存。
在一页加载期间,一个主题中可能会多次使用相当多的“模板标签”(Wordpress 功能)。当主题或插件调用这样的函数时,WP是否每次都会运行数据库查询来检索必要的数据,并且每次都会解析这些数据以返回所需的对象吗?
或者,该函数是否在第一次运行时将其结果存储在 PHP 变量中,并在查询数据库或解析之前检查它是否已经存在?
示例包括:
wp_get_nav_menu_object()
wp_get_nav_menu_items()
wp_list_categories()
wp_tag_cloud()
wp_list_authors()< /code>
...还有诸如 bloginfo()
或这样的重要函数wp_nav_menu()
。
当然,缓存任何和所有查询(例如与帖子相关的查询)没有多大意义。但对于上面的例子(还有更多),我相信会的。
到目前为止,当主题需要在页面上多次调用相同的函数时,我自己通过编写自己的函数或类并缓存在全局或静态变量中来缓存这些通用函数。我不明白为什么我应该通过多次运行完全相同的通用查询来增加服务器负载。
WordPress 中是否已经存在这种缓存?
I've been asking myself this question for quite a while. Maybe someone has already done some digging (or is involved in WP) to know the answer.
I'm talking about storing objects from WP-functions in PHP variables, for the duration of a page load, e.g. to avoid having to query the database twice for the same result set.
I don't mean caching in the sense of pre-rendering dynamic pages and saving them in HTML format for faster retrieval.
Quite a few "template tags" (Wordpress functions) may be used multiple times in a theme during one page load. When a theme or plugin calls such a function, does WP run a database query every time to retrieve the necessary data, and does it parse this data every time to return the desired object?
Or, does the function store the its result in a PHP variable the first time it runs, and checks if it already exists before it queries the database or parses?
Examples include:
wp_get_nav_menu_object()
wp_get_nav_menu_items()
wp_list_categories()
wp_tag_cloud()
wp_list_authors()
...but also such important functions as bloginfo()
or wp_nav_menu()
.
Of course, it wouldn't make much sense to cache any and all queries like post-related ones. But for the above examples (there are more), I believe it would.
So far, I've been caching these generic functions myself when a theme required the same function to be called more than once on a page, by writing my own functions or classes and caching in global or static variables. I don't see why I should add to the server load by running the exact same generic query more than a single time.
Does this sort of caching already exist in Wordpress?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,对于某些查询和功能。请参阅WP 对象缓存。相关函数为
wp_cache_get
、wp_cache_set
、wp_cache_add
和wp_cache_delete
。您可以通过 WordPress 代码在许多地方使用这些函数来准确执行您所描述的操作。Yes, for some queries and functions. See WP Object Cache. The relevant functions are
wp_cache_get
,wp_cache_set
,wp_cache_add
, andwp_cache_delete
. You can find these functions being used in many places through the WordPress code to do exactly what you are describing.