Are all options loaded to memory on each request?

Are all options variables fetched from the database and loaded to cache on each request?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Yes, sort of. When the get_option call is made, WordPress runs a function called wp_load_alloptions, which either grabs a cached copy of all autoloaded options or loads all those options into the cache. Then wp_load_alloptions returns an array of all the autoloaded options. If your option is autoloaded (specified when you use the add_option function), it will be part of this array and get returned.

If your option is not auto loaded, WordPress then looks in the cache specifically for that option and returns the value if it’s there. If that misses, the final fallback is to go to the database, fetch the value, set it the cache and then return it.

Finally, if no option is found at all (eg. the option name was invalid), WordPress stores that option’s name in a cached array called notoptions. If subsequent calls to the non-option are made, WP checks the notoptions array first so it doesn’t waste time checking for something that isn’t there.

If you’re worried about calling get_option multiple times per page load, don’t be. WordPress will cache the option value after the first get_option call regardless of whether or not its autoloaded.

Check out the source for get_option to see what’s going on.

Method 2

No,

only the options that are specifically loaded with autoload set to true

See http://codex.wordpress.org/Function_Reference/add_option

So if it is an option that is needed on every page, when you add it to the db, set autoload=true.

After that, just use get_option normally – wp will handle the cacheing etc.

Method 3

Yes,

There is no need to set autoload to yes/true. It is set to yes by default:
http://codex.wordpress.org/Function_Reference/add_option
last function parameter ($autoload):

Should this option be automatically loaded by the function wp_load_alloptions() (puts options into object cache on each page load)? Valid values: yes or no.
Default: yes

Though, if you set it to no, then the option won’t be put in cache.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x