$alloptions
Whenever we are storing something on the WordPress options table via add_option() function, it stores the value in options table as well as cache. Yes, it stores it in 2 places. This is mainly for the quick retrieval or quick access.
Let’s see this with example.,
add_option( 'test_option_key', 'test value', '', 'yes' );
Above code will store test_option_key
to the options
table and in cache with the key $alloptions
under options
group. The main reason to store it in the $alloptions
key is the fourth parameter in the add_option
function which is autoload=yes
.
To autoload the options WordPress stores all the options in an array called $alloptions
under options
group.
$alloptions = wp_load_alloptions(); $alloptions[ $option ] = $serialized_value; // This will be our value in serialized form. wp_cache_set( 'alloptions', $alloptions, 'options' );
Let’s say, we are adding an option without autoloading. Now WordPress will store that key-value pair in the direct cache under options
group.
wp_cache_set( $option, $serialized_value, 'options' );
So whenever we try to get the value from options table via get_option() function, WordPress will first look into the cache if key exists then it will return that value otherwise it will get you the data from the database.
$notoptions
WordPress very clever to handle the unnecessary calls to the database. Yes whenever you are trying to get the option which is not present in the cache as well as in the database, WordPress will store that key in a special option called $notoptions
under options
group. This will be very useful to avoid multiple hits to the cache and database.
Basically, $notoptions
is an array which will have all non-existing keys which we tried to access it from options. It’s an associative array with the string key and boolean value.
Let’s see with an example;
get_option( 'my_non_exist_key' );
WordPress first will check in the $notoptions
array to confirm this key is not stored already as the non-exist option. Then it will check in the $alloptions
array as it is autoloaded already. If found it will return the data. If not then it will look into the direct cache under options
group, if found there then will be returned with that data. At last, it will look into the database for the data.
$notoptions = wp_cache_get( 'notoptions', 'options' );
If WordPress couldn’t find the data anywhere, then the requested key will be stored in the $notoptions
array for the future references.
$notoptions[$option] = true; wp_cache_set( 'notoptions', $notoptions, 'options' );
Don’t worry once the non-existing key stored in the database with the value, then WordPress will remove that key from the $notoptions
array. So that it won’t be ignored anymore.