admin管理员组文章数量:1410712
As a plugin developer, I strive for coding plugins that take as less server resources as possible. That's why I thought of avoiding to repeat queries to the database that are run on the same request (non-persitent).
Let's say my plugin has a function/class that needs to retrieve a setting from the options table (using the get_option
WP core function). And that I know that same value will be needed by another function/class that will be run sometime after (within the same request/page load).
My question is, what would be more efficient and faster:
A) Store the value in a PHP constant
B) Store the value in a PHP global variable
C) Pass the value between calling functions
D) Use WP_Cache
(cache it the first time I retrieve it and then check if a cached value exists before querying the database)
E) Do nothing and stop thinking. There's no real improvement.
OK, and what if instead of a single value, they were many, like an array of data? Would the appropriate answer be still the same? What if I'd had my settings stored in several keys and I'd like to retrieve all of them in the first place to stored them and avoid having to query/execute the get_option function as the code runs down?
As a plugin developer, I strive for coding plugins that take as less server resources as possible. That's why I thought of avoiding to repeat queries to the database that are run on the same request (non-persitent).
Let's say my plugin has a function/class that needs to retrieve a setting from the options table (using the get_option
WP core function). And that I know that same value will be needed by another function/class that will be run sometime after (within the same request/page load).
My question is, what would be more efficient and faster:
A) Store the value in a PHP constant
B) Store the value in a PHP global variable
C) Pass the value between calling functions
D) Use WP_Cache
(cache it the first time I retrieve it and then check if a cached value exists before querying the database)
E) Do nothing and stop thinking. There's no real improvement.
OK, and what if instead of a single value, they were many, like an array of data? Would the appropriate answer be still the same? What if I'd had my settings stored in several keys and I'd like to retrieve all of them in the first place to stored them and avoid having to query/execute the get_option function as the code runs down?
Share Improve this question asked Feb 10, 2020 at 14:16 Víctor NoirVíctor Noir 233 bronze badges 4 |1 Answer
Reset to default 0If you check the source code for get_option
, you'll see that it already uses wp_cache_get()
and wp_cache_add()
under the hood. So without looking deeper into the implementation, I would presume that two calls of get_option('foo');
would only trigger a single db call (and cache the result).
Hence, in this case E) would apply: caching is already implemented for these calls.
However, on a more general note
- Always use the core caching mechanisms (e.g.
WP_Object_Cache
instead of some custom implementations).
You could just store it in some PHP constant/global, that is true (and probably how it will behave in a regular environment). But now I, a pro user, comes and wants to connect memcached/Redis. The existing implementations should now put the object cache in redis and take care of everything for me. Had I implemented my own caching system, I'd need to extend it myself to get it to work.
Always check how WP core does things and try to work in similar ways.
In my personal experience, one of the things that hurt performance most is
WP_Query
. So cache these results if it fits your case and try to make the query quicker. (Query for less fields, don't auto populate objects, turn some query caching/pagination off, etc.)
本文标签: phpShould I use wpcache in my plugin to make it faster
版权声明:本文标题:php - Should I use wp_cache in my plugin to make it faster? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744761339a2623772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_option
doesn't necessarily make a DB call. WP Object Cache caches nothing unless you have a caching plugin/driver available. – Ismail Commented Feb 11, 2020 at 21:28