Make WordPress Core

Changeset 3011


Ignore:
Timestamp:
11/07/2005 09:56:03 PM (21 years ago)
Author:
ryan
Message:

Object caching, round one.

Location:
trunk
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-db.php

    r2959 r3011  
    119119        }
    120120
     121        wp_cache_set($cat_ID, get_category($cat_ID), 'category');
     122
    121123        if ($update) {
    122124                do_action('edit_category', $cat_ID);
    123125        } else {
     126                wp_cache_delete('all_category_ids', 'category');
    124127                do_action('create_category', $cat_ID);
    125128                do_action('add_category', $cat_ID);
    … …  
    167170        // TODO: Only set categories to general if they're not in another category already
    168171        $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
     172
     173        wp_cache_delete($cat_ID, 'category');
     174        wp_cache_delete('all_category_ids', 'category');
    169175
    170176        do_action('delete_category', $cat_ID);
    … …  
    234240        $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
    235241
     242        wp_cache_delete($id, 'users');
     243        // TODO: Need to delete username keyed cache object.
     244
    236245        do_action('delete_user', $id);
    237246
  • trunk/wp-includes/classes.php

    r3001 r3011  
    444444                                $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
    445445
    446                         $q['cat'] = array_reduce(
    447                                 $cache_categories,
    448                                 create_function('$a, $b', 'return ($b->fullpath == "'.$cat_path.'") ? $b->cat_ID : $a;'),
    449                                 0
    450                         );
    451 
    452                         // If full path not found, look for last dir as category ignoring parent
    453                         if($q['cat'] == 0) {
    454                                 $q['cat'] = array_reduce(
    455                                         $cache_categories,
    456                                         create_function('$a, $b', 'return ($b->category_nicename == "'.$q['category_name'].'") ? $b->cat_ID : $a;'),
    457                                         0
    458                                 );
    459                         }
    460                        
     446                        $all_cat_ids = get_all_category_ids();
     447                        $q['cat'] = 0;                 
     448                        foreach ( $all_cat_ids as $cat_id ) {
     449                                $cat = get_category($cat_id);
     450                                if ( $cat->fullpath == $cat_path ) {
     451                                        $q['cat'] = $cat_id;
     452                                        break;
     453                                }
     454                        }
     455
    461456                        $tables = ", $wpdb->post2cat, $wpdb->categories";
    462457                        $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
  • trunk/wp-includes/functions-post.php

    r2996 r3011  
    138138        wp_set_post_cats('', $post_ID, $post_category);
    139139
    140         if ( 'static' == $post_status )
     140        if ( 'static' == $post_status ) {
    141141                clean_page_cache($post_ID);
    142         else
     142                wp_cache_delete($post_ID, 'pages');
     143        } else {
    143144                clean_post_cache($post_ID);
     145        }
    144146
    145147        // Set GUID
  • trunk/wp-includes/functions.php

    r3005 r3011  
    261261
    262262function get_settings($setting) {
    263         global $wpdb, $cache_settings, $cache_nonexistantoptions;
     263        global $wpdb;
    264264        if ( strstr($_SERVER['REQUEST_URI'], 'wp-admin/install.php') || defined('WP_INSTALLING') )
    265265                return false;
    266266
    267         if ( empty($cache_settings) )
    268                 $cache_settings = get_alloptions();
    269 
    270         if ( empty($cache_nonexistantoptions) )
    271                 $cache_nonexistantoptions = array();
    272 
    273         if ( 'home' == $setting && '' == $cache_settings->home )
    274                 return apply_filters('option_' . $setting, $cache_settings->siteurl);
    275 
    276         if ( isset($cache_settings->$setting) ) :
    277                 return apply_filters('option_' . $setting, $cache_settings->$setting);
    278         else :
    279                 // for these cases when we're asking for an unknown option
    280                 if ( isset($cache_nonexistantoptions[$setting]) )
    281                         return false;
    282 
    283                 $option = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'");
    284 
    285                 if (!$option) :
    286                         $cache_nonexistantoptions[$setting] = true;
    287                         return false;
    288                 endif;
    289 
    290                 @ $kellogs = unserialize($option);
    291                 if ( $kellogs !== FALSE )
    292                         return apply_filters('option_' . $setting, $kellogs);
    293                 else return apply_filters('option_' . $setting, $option);
    294         endif;
     267        $value = wp_cache_get($setting, 'options');
     268
     269        if ( false === $value ) {
     270                $value = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'");
     271                wp_cache_add($setting, $value, 'options');
     272        }
     273
     274        // If home is not set use siteurl.
     275        if ( 'home' == $setting && '' == $value )
     276                return get_settings('siteurl');
     277
     278        if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
     279                $value = preg_replace('|/+$|', '', $value);
     280
     281        @ $kellogs = unserialize($value);
     282        if ( $kellogs !== FALSE )
     283                return apply_filters('option_' . $setting, $kellogs);
     284        else
     285                return apply_filters('option_' . $setting, $value);
    295286}
    296287
    … …  
    339330
    340331function update_option($option_name, $newvalue) {
    341         global $wpdb, $cache_settings;
     332        global $wpdb;
    342333
    343334        if ( is_string($newvalue) )
    … …  
    355346                add_option($option_name);
    356347
     348        wp_cache_set($option_name, $newvalue, 'options');
     349
    357350        $newvalue = $wpdb->escape($newvalue);
    358         $option_name = $wpdb->escape( $option_name );
     351        $option_name = $wpdb->escape($option_name);
    359352        $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
    360         $cache_settings = get_alloptions(); // Re cache settings
    361353        return true;
    362354}
    … …  
    371363// thx Alex Stapleton, http://alex.vort-x.net/blog/
    372364function add_option($name, $value = '', $description = '', $autoload = 'yes') {
    373         global $wpdb, $cache_settings;
     365        global $wpdb;
    374366
    375367        // Make sure the option doesn't already exist
    376         if ( isset($cache_settings->$name) )
     368        if ( false !== get_option($name, 'options') )
    377369                return;
    378370
    … …  
    381373                $value = serialize($value);
    382374
    383         if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$name'") ) {
    384                 $name = $wpdb->escape($name);
    385                 $value = $wpdb->escape($value);
    386                 $description = $wpdb->escape($description);
    387                 $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
    388 
    389                 if ( $wpdb->insert_id ) {
    390                         global $cache_settings;
    391                         $cache_settings->{$name} = $original;
    392                 }
    393         }
     375        wp_cache_add($name, $value, 'options');
     376
     377        $name = $wpdb->escape($name);
     378        $value = $wpdb->escape($value);
     379        $description = $wpdb->escape($description);
     380        $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
     381
    394382        return;
    395383}
    … …  
    401389        if ( !$option_id ) return false;
    402390        $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
     391        wp_cache_delete($name, 'options');
    403392        return true;
    404393}
    … …  
    589578// Handles page caching.
    590579function &get_page(&$page, $output = OBJECT) {
    591         global $page_cache, $wpdb;
     580        global $wpdb;
    592581
    593582        if ( empty($page) ) {
    594                 if ( isset($GLOBALS['page']) )
     583                if ( isset($GLOBALS['page']) ) {
    595584                        $_page = & $GLOBALS['page'];
    596                 else
     585                        wp_cache_add($_page->ID, $_page, 'pages');
     586                } else {
    597587                        $_page = null;
     588                }
    598589        } elseif ( is_object($page) ) {
    599                 if ( !isset($page_cache[$page->ID]) )
    600                         $page_cache[$page->ID] = &$page;
    601                 $_page = & $page_cache[$page->ID];
    602         } else {
    603                 if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) )
     590                wp_cache_add($page->ID, $page, 'pages');
     591                $_page = $page;
     592        } else {
     593                if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) ) {
    604594                        $_page = & $GLOBALS['page'];
    605                 elseif ( isset($page_cache[$page]) )
    606                         $_page = & $page_cache[$page];
    607                 else {
     595                        wp_cache_add($_page->ID, $_page, 'pages');
     596                } elseif ( $_page = wp_cache_get($page, 'pages') ) {
     597                        // Got it.
     598                } else {
    608599                        $query = "SELECT * FROM $wpdb->posts WHERE ID= '$page'";
    609                         $page_cache[$page] = & $wpdb->get_row($query);
    610                         $_page = & $page_cache[$page];
     600                        $_page = & $wpdb->get_row($query);
     601                        wp_cache_add($_page->ID, $_page, 'pages');
    611602                }
    612603        }
    … …  
    623614}
    624615
     616function set_category_path($cat) {
     617        $cat->fullpath = '/' . $cat->category_nicename;
     618        $path = $cat->fullpath;
     619        $curcat = $cat;
     620        while ($curcat->category_parent != 0) {
     621                $curcat = get_category($curcat->category_parent);
     622                $path = '/' . $curcat->category_nicename . $path;
     623        }
     624       
     625        $cat->fullpath = $path;
     626
     627        return $cat;
     628}
     629
    625630// Retrieves category data given a category ID or category object.
    626631// Handles category caching.
    627632function &get_category(&$category, $output = OBJECT) {
    628         global $cache_categories, $wpdb;
     633        global $wpdb;
    629634
    630635        if ( empty($category) )
    631636                return null;
    632637
    633         if ( !isset($cache_categories) )
    634                 update_category_cache();
    635 
    636638        if ( is_object($category) ) {
    637                 if ( !isset($cache_categories[$category->cat_ID]) )
    638                         $cache_categories[$category->cat_ID] = &$category;
    639                 $_category = & $cache_categories[$category->cat_ID];
    640         } else {
    641                 if ( !isset($cache_categories[$category]) ) {
     639                wp_cache_add($category->cat_ID, $category, 'category');
     640                $_category = $category;
     641        } else {
     642                if ( ! $_category = wp_cache_get($category, 'category') ) {
    642643                        $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category'");
    643                         $cache_categories[$category->cat_ID] = & $_category;
    644                 } else {
    645                         $_category = & $cache_categories[$category];
    646                 }
     644                        wp_cache_add($category, $_category, 'category');
     645                }
     646        }
     647
     648        if ( !isset($_category->fullpath) ) {
     649                $_category = set_category_path($_category);
     650                wp_cache_replace($_category->cat_ID, $_category, 'category');   
    647651        }
    648652
    … …  
    693697        $category = &get_category($cat_ID);
    694698        return $category->cat_name;
     699}
     700
     701function get_all_category_ids() {
     702        global $wpdb;
     703       
     704        if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
     705                $cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
     706                wp_cache_add('all_category_ids', $cat_ids, 'category');
     707        }
     708       
     709        return $cat_ids;
    695710}
    696711
    … …  
    12321247        for ($i = 0; $i < count($pages); $i++) {
    12331248                $page_cache[$pages[$i]->ID] = &$pages[$i];
     1249                wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
    12341250        }
    12351251}
    … …  
    12441260
    12451261function update_post_category_cache($post_ids) {
    1246         global $wpdb, $category_cache, $cache_categories;
     1262        global $wpdb, $category_cache;
    12471263
    12481264        if ( empty($post_ids) )
    … …  
    12561272        WHERE category_id = cat_ID AND post_id IN ($post_ids)");
    12571273
    1258         if ( !isset($cache_categories) )
    1259                 update_category_cache();
    1260 
    1261         if ( !empty($dogs) ) {
    1262                 foreach ($dogs as $catt) {
    1263                         $category_cache[$catt->post_id][$catt->cat_ID] = &$cache_categories[$catt->cat_ID];
    1264                 }
    1265         }
     1274        if ( empty($dogs) )
     1275                return;
     1276               
     1277        foreach ($dogs as $catt)
     1278                $category_cache[$catt->post_id][$catt->cat_ID] = &get_category($catt->cat_ID);
    12661279}
    12671280
    … …  
    13231336
    13241337function update_category_cache() {
    1325         global $cache_categories, $wpdb;
    1326         if ( $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories") ):
    1327                 foreach ($dogs as $catt)
    1328                         $cache_categories[$catt->cat_ID] = $catt;
    1329 
    1330                 foreach ($cache_categories as $catt) {
    1331                         $curcat = $catt->cat_ID;
    1332                         $cache_categories[$catt->cat_ID]->fullpath = '/' . $cache_categories[$catt->cat_ID]->category_nicename;
    1333                         while ($cache_categories[$curcat]->category_parent != 0) {
    1334                                 $curcat = $cache_categories[$curcat]->category_parent;
    1335                                 $cache_categories[$catt->cat_ID]->fullpath = '/' . $cache_categories[$curcat]->category_nicename . $cache_categories[$catt->cat_ID]->fullpath;
    1336                         }
    1337                 }
    1338                 return true;
    1339         else :
    1340                 return false;
    1341         endif;
    1342 }
    1343 
    1344 function clean_user_cache($id) {
    1345         global $cache_userdata;
    1346 
    1347         if ( isset( $cache_userdata[$id] ) )
    1348                 unset( $cache_userdata[$id] );
     1338        return true;
    13491339}
    13501340
    … …  
    21242114        if ( $cur->meta_value != $meta_value )
    21252115                $wpdb->query("UPDATE $wpdb->usermeta SET meta_value = '$meta_value' WHERE user_id = '$user_id' AND meta_key = '$meta_key'");
     2116
     2117        wp_cache_delete($user_id, 'users');
     2118        // FIXME: Need to delete username keyed cache object.
    21262119}
    21272120
  • trunk/wp-includes/pluggable-functions.php

    r3004 r3011  
    2828if ( !function_exists('get_userdata') ) :
    2929function get_userdata( $user_id ) {
    30         global $wpdb, $cache_userdata;
     30        global $wpdb;
    3131        $user_id = (int) $user_id;
    3232        if ( $user_id == 0 )
    3333                return false;
    3434
    35         if ( isset( $cache_userdata[$user_id] ) )
    36                 return $cache_userdata[$user_id];
     35        $user = wp_cache_get($user_id, 'users');
     36       
     37        if ( $user )
     38                return $user;
    3739
    3840        if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$user_id'") )
    39                 return $cache_userdata[$user_id] = false;
     41                return false;
    4042
    4143        $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
    … …  
    5254        }
    5355
    54         $cache_userdata[$user_id] = $user;
    55         $cache_userdata[$cache_userdata[$user_id]->user_login] =& $cache_userdata[$user_id];
    56 
    57         return $cache_userdata[$user_id];
     56        wp_cache_add($user_id, $user, 'users');
     57        wp_cache_add($user->user_login, $user, 'users');
     58       
     59        return $user;
    5860}
    5961endif;
    … …  
    9698        if ( empty( $user_login ) )
    9799                return false;
    98 
    99         if ( isset( $cache_userdata[$user_login] ) )
    100                 return $cache_userdata[$user_login];
     100               
     101        $userdata = wp_cache_get($user_login, 'users');
     102        if ( $userdata )
     103                return $userdata;
    101104
    102105        if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'") )
    … …  
    148151        }
    149152
    150         $login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
     153        $login = get_userdatabylogin($username);
     154        //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
    151155
    152156        if (!$login) {
  • trunk/wp-includes/registration-functions.php

    r2995 r3011  
    44        global $wpdb;
    55        $username = sanitize_user( $username );
    6         $query = "SELECT user_login FROM $wpdb->users WHERE user_login = '$username'";
    7         $query = apply_filters('username_exists', $query);
    8         return $wpdb->get_var( $query );
     6        $user = get_userdatabylogin($username);
     7        if ( $user )
     8                return $user->user_login;
     9
     10        return null;
    911}
    1012
    … …  
    5052        }
    5153       
    52         clean_user_cache($user_id);
    53         clean_user_cache($user_login);
    54 
    5554        update_usermeta( $user_id, 'first_name', $first_name);
    5655        update_usermeta( $user_id, 'last_name', $last_name);
    … …  
    7069                $user->set_role(get_settings('default_role'));
    7170        }
     71
     72        wp_cache_delete($user_id, 'users');
     73        wp_cache_delete($user_login, 'users');
    7274       
    7375        if ( $update )
  • trunk/wp-includes/template-functions-category.php

    r2978 r3011  
    127127
    128128function get_category_children($id, $before = '/', $after = '') {
    129         global $cache_categories;
    130 
    131         if ( !isset($cache_categories) )
    132                 update_category_cache();
    133 
    134         $c_cache = $cache_categories; // Can't do recursive foreach on a global, have to make a copy
    135         $chain = '';
    136         foreach ( $c_cache as $category ) {
     129        $cat_ids = get_all_category_ids();
     130        foreach ( $cat_ids as $cat_id ) {
     131                if ( $cat_id == $id)
     132                        continue;
     133
     134                $category = get_category($cat_id);
    137135                if ( $category->category_parent == $id ) {
    138136                        $chain .= $before.$category->cat_ID.$after;
  • trunk/wp-settings.php

    r2818 r3011  
    2121$HTTP_USER_AGENT = getenv('HTTP_USER_AGENT');
    2222unset( $wp_filter, $cache_userdata, $cache_lastcommentmodified, $cache_lastpostdate, $cache_settings, $category_cache, $cache_categories );
     23
     24if ( ! isset($blog_id) )
     25        $blog_id = 1;
    2326
    2427// Fix for IIS, which doesn't set REQUEST_URI
    … …  
    7578if ( defined('CUSTOM_USER_META_TABLE') )
    7679        $wpdb->usermeta = CUSTOM_USER_META_TABLE;
    77        
     80
    7881// We're going to need to keep this around for a few months even though we're not using it internally
    7982
    … …  
    8891$tablepostmeta = $wpdb->postmeta;
    8992
     93if ( file_exists(ABSPATH . 'wp-content/object-cache.php') )
     94        require (ABSPATH . 'wp-content/object-cache.php');
     95else
     96        require (ABSPATH . WPINC . '/cache.php');
     97
     98// For now, disable persistent caching by default.  To enable, comment out
     99// the following line.
     100define('DISABLE_CACHE', true);
     101
     102wp_cache_init();
     103
    90104$wp_filters = array();
    91105
    … …  
    95109
    96110$wpdb->hide_errors();
    97 if ( !update_category_cache() && (!strstr($_SERVER['PHP_SELF'], 'install.php') && !defined('WP_INSTALLING')) ) {
     111$db_check = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'");
     112if ( !$db_check && (!strstr($_SERVER['PHP_SELF'], 'install.php') && !defined('WP_INSTALLING')) ) {
    98113        if ( strstr($_SERVER['PHP_SELF'], 'wp-admin') )
    99114                $link = 'install.php';
    … …  
    195210
    196211function shutdown_action_hook() {
     212        wp_cache_close();
    197213        do_action('shutdown');
    198214}
Note: See TracChangeset for help on using the changeset viewer.