WordPress.org

Make WordPress Core

Changeset 4855


Ignore:
Timestamp:
02/01/07 08:53:08 (7 years ago)
Author:
markjaquith
Message:

Introduce Notoptions and Alloptions caching, so that all options (and previously attempted Notoptions) are read from the cache in one go. Should reduce cache misses to zero or close to it. fixes #3726

Location:
trunk/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/cache.php

    r4686 r4855  
    192192                    $this->cache['category'][$catt->cat_ID] = $catt; 
    193193            } 
    194         } else 
    195             if ('options' == $group) { 
    196                 $wpdb->hide_errors(); 
    197                 if (!$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'")) { 
    198                     $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options"); 
    199                 } 
    200                 $wpdb->show_errors(); 
    201  
    202                 if ( ! $options ) 
    203                     return; 
    204  
    205                 foreach ($options as $option) { 
    206                     $this->cache['options'][$option->option_name] = $option->option_value; 
    207                 } 
    208             } 
     194        } 
     195 
    209196    } 
    210197 
  • trunk/wp-includes/functions.php

    r4842 r4855  
    209209        return $pre;  
    210210 
    211     $value = wp_cache_get($setting, 'options'); 
    212  
    213     if ( false === $value ) { 
    214         if ( defined('WP_INSTALLING') ) 
    215             $wpdb->hide_errors(); 
    216         $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1"); 
    217         if ( defined('WP_INSTALLING') ) 
    218             $wpdb->show_errors(); 
    219  
    220         if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values 
    221             $value = $row->option_value; 
    222             wp_cache_set($setting, $value, 'options'); 
    223         } else { 
    224             return false; 
     211    // prevent non-existent options from triggering multiple queries 
     212    $notoptions = wp_cache_get('notoptions', 'options'); 
     213    if ( isset($notoptions[$setting]) ) 
     214        return false; 
     215 
     216    $alloptions = wp_load_alloptions(); 
     217 
     218    if ( isset($alloptions[$setting]) ) { 
     219        $value = $alloptions[$setting]; 
     220    } else { 
     221        $value = wp_cache_get($setting, 'options'); 
     222 
     223        if ( false === $value ) { 
     224            if ( defined('WP_INSTALLING') ) 
     225                $wpdb->hide_errors(); 
     226            $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1"); 
     227            if ( defined('WP_INSTALLING') ) 
     228                $wpdb->show_errors(); 
     229 
     230            if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values 
     231                $value = $row->option_value; 
     232                wp_cache_set($setting, $value, 'options'); 
     233            } else { // option does not exist, so we must cache its non-existence 
     234                $notoptions[$setting] = true; 
     235                wp_cache_set('notoptions', $notoptions, 'options'); 
     236                return false; 
     237            } 
    225238        } 
    226239    } 
     
    234247 
    235248    return apply_filters( 'option_' . $setting, maybe_unserialize($value) ); 
     249} 
     250 
     251function wp_protect_special_option($option) { 
     252    $protected = array('alloptions', 'notoptions'); 
     253    if ( in_array($option, $protected) ) 
     254        die(sprintf(__('%s is a protected WP option and may not be modified'), wp_specialchars($option))); 
    236255} 
    237256 
     
    263282} 
    264283 
     284function wp_load_alloptions() { 
     285    global $wpdb; 
     286 
     287    $alloptions = wp_cache_get('alloptions', 'options'); 
     288 
     289    if ( !$alloptions ) { 
     290        $wpdb->hide_errors(); 
     291        if ( !$alloptions_db = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) 
     292            $alloptions_db = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options"); 
     293        $wpdb->show_errors(); 
     294        $alloptions = array(); 
     295        foreach ( (array) $alloptions_db as $o ) 
     296            $alloptions[$o->option_name] = $o->option_value; 
     297        wp_cache_set('alloptions', $alloptions, 'options'); 
     298    } 
     299    return $alloptions; 
     300} 
     301 
    265302function update_option($option_name, $newvalue) { 
    266303    global $wpdb; 
     304 
     305    wp_protect_special_option($option_name); 
    267306 
    268307    if ( is_string($newvalue) ) 
     
    280319    } 
    281320 
     321    $notoptions = wp_cache_get('notoptions', 'options'); 
     322    if ( isset($notoptions[$option_name]) ) { 
     323        unset($notoptions[$option_name]); 
     324        wp_cache_set('notoptions', $notoptions, 'options'); 
     325    } 
     326 
    282327    $_newvalue = $newvalue; 
    283328    $newvalue = maybe_serialize($newvalue); 
    284329 
    285     wp_cache_set($option_name, $newvalue, 'options'); 
     330    $alloptions = wp_load_alloptions(); 
     331    if ( isset($alloptions[$option_name]) ) { 
     332        $alloptions[$options_name] = $newvalue; 
     333        wp_cache_set('alloptions', $alloptions, 'options'); 
     334    } else { 
     335        wp_cache_set($option_name, $newvalue, 'options'); 
     336    } 
    286337 
    287338    $newvalue = $wpdb->escape($newvalue); 
     
    299350    global $wpdb; 
    300351 
    301     // Make sure the option doesn't already exist 
    302     if ( false !== get_option($name) ) 
    303         return; 
     352    wp_protect_special_option($name); 
     353 
     354    // Make sure the option doesn't already exist we can check the cache before we ask for a db query 
     355    $notoptions = wp_cache_get('notoptions', 'options'); 
     356    if ( isset($notoptions[$name]) ) { 
     357        unset($notoptions[$name]); 
     358        wp_cache_set('notoptions', $notoptions, 'options'); 
     359    } elseif ( false !== get_option($name) ) { 
     360            return; 
     361    } 
    304362 
    305363    $value = maybe_serialize($value); 
    306364 
    307     wp_cache_set($name, $value, 'options'); 
     365    if ( 'yes' == $autoload ) { 
     366        $alloptions = wp_load_alloptions(); 
     367        $alloptions[$name] = $value; 
     368        wp_cache_set('alloptions', $alloptions, 'options'); 
     369    } else { 
     370        wp_cache_set($name, $value, 'options'); 
     371    } 
    308372 
    309373    $name = $wpdb->escape($name); 
     
    317381function delete_option($name) { 
    318382    global $wpdb; 
     383 
     384    wp_protect_special_option($name); 
     385 
    319386    // Get the ID, if no ID then return 
    320     $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'"); 
    321     if ( !$option_id ) return false; 
     387    $option = $wpdb->get_row("SELECT option_id, autoload FROM $wpdb->options WHERE option_name = '$name'"); 
     388    if ( !$option->option_id ) return false; 
    322389    $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'"); 
    323     wp_cache_delete($name, 'options'); 
     390    if ( 'yes' == $option->autoload ) { 
     391        $alloptions = wp_load_alloptions(); 
     392        if ( isset($alloptions[$name]) ) { 
     393            unset($alloptions[$name]); 
     394            wp_cache_set('alloptions', $alloptions, 'options'); 
     395        } 
     396    } else { 
     397        wp_cache_delete($name, 'options'); 
     398    } 
    324399    return true; 
    325400} 
  • trunk/wp-includes/version.php

    r4832 r4855  
    44 
    55$wp_version = '2.2-bleeding'; 
    6 $wp_db_version = 4772; 
     6$wp_db_version = 4773; 
    77 
    88?> 
Note: See TracChangeset for help on using the changeset viewer.