WordPress.org

Make WordPress Core

Changeset 6229


Ignore:
Timestamp:
10/12/2007 09:12:34 PM (13 years ago)
Author:
ryan
Message:

Eliminate , , and . see #5182

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/post.php

    r6213 r6229  
    349349            return false;
    350350
     351        wp_cache_delete($post_ID, 'post_meta');
     352
    351353        $result = $wpdb->query( "
    352354                        INSERT INTO $wpdb->postmeta
     
    363365    $mid = (int) $mid;
    364366
     367    $post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = '$mid'");
     368    wp_cache_delete($post_id, 'post_meta');
     369
    365370    return $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id = '$mid'" );
    366371}
     
    408413    if ( in_array($mkey, $protected) )
    409414        return false;
     415
     416    $post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = '$mid'");
     417    wp_cache_delete($post_id, 'post_meta');
    410418
    411419    $mvalue = maybe_serialize( stripslashes( $mvalue ));
  • trunk/wp-app.php

    r6195 r6229  
    697697
    698698    function get_feed($page = 1, $post_type = 'post') {
    699         global $post, $wp, $wp_query, $posts, $wpdb, $blog_id, $post_cache;
     699        global $post, $wp, $wp_query, $posts, $wpdb, $blog_id;
    700700        log_app('function',"get_feed($page, '$post_type')");
    701701        ob_start();
     
    716716        $wpdb = $GLOBALS['wpdb'];
    717717        $blog_id = (int) $GLOBALS['blog_id'];
    718         $post_cache = $GLOBALS['post_cache'];
    719718        log_app('function',"query_posts(# " . print_r($wp_query, true) . "#)");
    720719
     
    757756        log_app('function',"get_entry($postID, '$post_type')");
    758757        ob_start();
    759         global $posts, $post, $wp_query, $wp, $wpdb, $blog_id, $post_cache;
     758        global $posts, $post, $wp_query, $wp, $wpdb, $blog_id;
    760759        switch($post_type) {
    761760            case 'post':
  • trunk/wp-includes/post.php

    r6221 r6229  
    2727
    2828function &get_children($args = '', $output = OBJECT) {
    29     global $post_cache, $wpdb, $blog_id;
     29    global $wpdb;
    3030
    3131    if ( empty( $args ) ) {
     
    5050    $children = get_posts( $r );
    5151
    52     if ( $children ) {
    53         foreach ( $children as $key => $child ) {
    54             $post_cache[$blog_id][$child->ID] =& $children[$key];
    55             $kids[$child->ID] =& $children[$key];
    56         }
    57     } else {
    58         return false;
    59     }
     52    if ( !$children )
     53        return false;
     54
     55    update_post_cache($children);
     56
     57    foreach ( $children as $key => $child )
     58        $kids[$child->ID] =& $children[$key];
    6059
    6160    if ( $output == OBJECT ) {
     
    9493// Handles post caching.
    9594function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
    96     global $post_cache, $wpdb, $blog_id;
     95    global $wpdb;
    9796
    9897    if ( empty($post) ) {
     
    10099            $_post = & $GLOBALS['post'];
    101100        else
    102             $_post = null;
     101            return null;
    103102    } elseif ( is_object($post) ) {
    104         if ( 'page' == $post->post_type )
    105             return get_page($post, $output, $filter);
    106         if ( !isset($post_cache[$blog_id][$post->ID]) )
    107             $post_cache[$blog_id][$post->ID] = &$post;
    108         $_post = & $post_cache[$blog_id][$post->ID];
     103        wp_cache_add($post->ID, $post, 'posts');
     104        $_post = &$post;
    109105    } else {
    110106        $post = (int) $post;
    111         if ( isset($post_cache[$blog_id][$post]) )
    112             $_post = & $post_cache[$blog_id][$post];
    113         elseif ( $_post = wp_cache_get($post, 'pages') )
    114             return get_page($_post, $output, $filter);
    115         else {
     107        if ( ! $_post = wp_cache_get($post, 'posts') ) {
    116108            $_post = & $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post));
    117             if ( 'page' == $_post->post_type )
    118                 return get_page($_post, $output, $filter);
    119             $post_cache[$blog_id][$post] = & $_post;
    120         }
    121     }
    122 
    123     if ( defined('WP_IMPORTING') )
    124         unset($post_cache[$blog_id]);
     109            wp_cache_add($_post->ID, $_post, 'posts');
     110        }
     111    }
    125112
    126113    $_post = sanitize_post($_post, $filter);
     
    274261
    275262function add_post_meta($post_id, $key, $value, $unique = false) {
    276     global $wpdb, $post_meta_cache, $blog_id;
     263    global $wpdb;
    277264
    278265    if ( $unique ) {
     
    283270    }
    284271
    285     $post_meta_cache[$blog_id][$post_id][$key][] = $value;
     272    $cache = wp_cache_get($post_id, 'post_meta');
     273    if ( ! is_array($cache) )
     274        $cache = array();
     275    $cache[$key][] = $value;
     276
     277    wp_cache_set($post_id, $cache, 'post_meta');
    286278
    287279    $value = maybe_serialize($value);
     
    294286
    295287function delete_post_meta($post_id, $key, $value = '') {
    296     global $wpdb, $post_meta_cache, $blog_id;
     288    global $wpdb;
    297289
    298290    if ( empty($value) ) {
     
    310302        // expected_slashed ($key)
    311303        $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '$key'", $post_id));
    312         unset($post_meta_cache[$blog_id][$post_id][$key]);
    313304    } else {
    314305        // expected_slashed ($key, $value)
    315306        $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '$key' AND meta_value = '$value'", $post_id));
    316         $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
    317         if ($cache_key) foreach ( $cache_key as $index => $data )
    318             if ( $data == $value )
    319                 unset($post_meta_cache[$blog_id][$post_id][$key][$index]);
    320     }
    321 
    322     unset($post_meta_cache[$blog_id][$post_id][$key]);
     307    }
     308
     309    wp_cache_delete($post_id, 'post_meta');
    323310
    324311    return true;
     
    326313
    327314function get_post_meta($post_id, $key, $single = false) {
    328     global $wpdb, $post_meta_cache, $blog_id;
     315    global $wpdb;
    329316
    330317    $post_id = (int) $post_id;
    331318
    332     if ( isset($post_meta_cache[$blog_id][$post_id][$key]) ) {
     319    $meta_cache = wp_cache_get($post_id, 'post_meta');
     320
     321    if ( isset($meta_cache[$key]) ) {
    333322        if ( $single ) {
    334             return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key][0] );
     323            return maybe_unserialize( $meta_cache[$key][0] );
    335324        } else {
    336             return maybe_unserialize( $post_meta_cache[$blog_id][$post_id][$key] );
    337         }
    338     }
    339 
    340     if ( !isset($post_meta_cache[$blog_id][$post_id]) )
     325            return maybe_unserialize( $meta_cache[$key] );
     326        }
     327    }
     328
     329    if ( !$meta_cache ) {
    341330        update_postmeta_cache($post_id);
     331        $meta_cache = wp_cache_get($post_id, 'post_meta');
     332    }
    342333
    343334    if ( $single ) {
    344         if ( isset($post_meta_cache[$blog_id][$post_id][$key][0]) )
    345             return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key][0]);
     335        if ( isset($meta_cache[$key][0]) )
     336            return maybe_unserialize($meta_cache[$key][0]);
    346337        else
    347338            return '';
    348     }   else {
    349         return maybe_unserialize($post_meta_cache[$blog_id][$post_id][$key]);
     339    } else {
     340        return maybe_unserialize($meta_cache[$key]);
    350341    }
    351342}
    352343
    353344function update_post_meta($post_id, $key, $value, $prev_value = '') {
    354     global $wpdb, $post_meta_cache, $blog_id;
     345    global $wpdb;
    355346
    356347    $original_value = $value;
     
    368359        // expected_slashed ($key)
    369360        $wpdb->query($wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_key = '$key' AND post_id = %d", $value, $post_id));
    370         $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
    371         if ( !empty($cache_key) )
    372             foreach ($cache_key as $index => $data)
    373                 $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
    374361    } else {
    375362        // expected_slashed ($key)
    376363        $wpdb->query($wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_key = '$key' AND post_id = %d AND meta_value = %s", $value, $post_id, $prev_value));
    377         $cache_key = $post_meta_cache[$blog_id][$post_id][$key];
    378         if ( !empty($cache_key) )
    379             foreach ($cache_key as $index => $data)
    380                 if ( $data == $original_prev )
    381                     $post_meta_cache[$blog_id][$post_id][$key][$index] = $original_value;
    382     }
     364    }
     365
     366    wp_cache_delete($post_id, 'post_meta');
    383367
    384368    return true;
     
    387371
    388372function delete_post_meta_by_key($post_meta_key) {
    389     global $wpdb, $post_meta_cache, $blog_id;
     373    global $wpdb;
    390374    if ( $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key)) ) {
    391         unset($post_meta_cache[$blog_id]); // not worth doing the work to iterate through the cache
     375        // TODO Get post_ids and delete cache
     376        // wp_cache_delete($post_id, 'post_meta');
    392377        return true;
    393378    }
     
    397382
    398383function get_post_custom($post_id = 0) {
    399     global $id, $post_meta_cache, $wpdb, $blog_id;
     384    global $id, $wpdb;
    400385
    401386    if ( !$post_id )
     
    404389    $post_id = (int) $post_id;
    405390
    406     if ( !isset($post_meta_cache[$blog_id][$post_id]) )
     391    if ( ! wp_cache_get($post_id, 'post_meta') )
    407392        update_postmeta_cache($post_id);
    408393
    409     return $post_meta_cache[$blog_id][$post_id];
     394    return wp_cache_get($post_id, 'post_meta');
    410395}
    411396
     
    978963// Handles page caching.
    979964function &get_page(&$page, $output = OBJECT, $filter = 'raw') {
    980     global $wpdb, $blog_id;
    981 
    982965    if ( empty($page) ) {
    983         if ( isset( $GLOBALS['page'] ) && isset( $GLOBALS['page']->ID ) ) {
    984             $_page = & $GLOBALS['page'];
    985             wp_cache_add($_page->ID, $_page, 'pages');
    986         } else {
    987             // shouldn't we just return NULL at this point? ~ Mark
    988             $_page = null;
    989         }
    990     } elseif ( is_object($page) ) {
    991         if ( 'post' == $page->post_type )
    992             return get_post($page, $output, $filter);
    993         wp_cache_add($page->ID, $page, 'pages');
    994         $_page = $page;
    995     } else {
    996         $page = (int) $page;
    997         // first, check the cache
    998         if ( ! ( $_page = wp_cache_get($page, 'pages') ) ) {
    999             // not in the page cache?
    1000             if ( isset($GLOBALS['page']->ID) && ($page == $GLOBALS['page']->ID) ) { // for is_page() views
    1001                 // I don't think this code ever gets executed ~ Mark
    1002                 $_page = & $GLOBALS['page'];
    1003                 wp_cache_add($_page->ID, $_page, 'pages');
    1004             } elseif ( isset($GLOBALS['post_cache'][$blog_id][$page]) ) { // it's actually a page, and is cached
    1005                 return get_post($page, $output, $filter);
    1006             } else { // it's not in any caches, so off to the DB we go
    1007                 // Why are we using assignment for this query?
    1008                 $_page = & $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID= %d LIMIT 1", $page ));
    1009                 if ( 'post' == $_page->post_type )
    1010                     return get_post($_page, $output, $filter);
    1011                 // Potential issue: we're not checking to see if the post_type = 'page'
    1012                 // So all non-'post' posts will get cached as pages.
    1013                 wp_cache_add($_page->ID, $_page, 'pages');
    1014             }
    1015         }
    1016     }
    1017 
    1018     $_page = sanitize_post($_page, $filter);
    1019 
    1020     // at this point, one way or another, $_post contains the page object
    1021 
    1022     if ( $output == OBJECT ) {
    1023         return $_page;
    1024     } elseif ( $output == ARRAY_A ) {
    1025         return get_object_vars($_page);
    1026     } elseif ( $output == ARRAY_N ) {
    1027         return array_values(get_object_vars($_page));
    1028     } else {
    1029         return $_page;
    1030     }
     966        if ( isset( $GLOBALS['page'] ) && isset( $GLOBALS['page']->ID ) )
     967            return get_post($GLOBALS['page'], $output, $filter);
     968        else
     969            return null;
     970    }
     971
     972    return get_post($page, $output, $filter);
    1031973}
    1032974
     
    10721014
    10731015function &get_page_children($page_id, $pages) {
    1074     global $page_cache, $blog_id;
    1075 
    1076     if ( empty($pages) )
    1077         $pages = &$page_cache[$blog_id];
    1078 
    10791016    $page_list = array();
    10801017    foreach ( $pages as $page ) {
     
    16951632
    16961633function update_post_cache(&$posts) {
    1697     global $post_cache, $blog_id;
    1698 
    16991634    if ( !$posts )
    17001635        return;
    17011636
    1702     for ($i = 0; $i < count($posts); $i++) {
    1703         $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
    1704     }
     1637    foreach ( $posts as $post )
     1638        wp_cache_add($post->ID, $post, 'posts');
    17051639}
    17061640
    17071641function clean_post_cache($id) {
    1708     global $post_cache, $post_meta_cache, $post_term_cache, $blog_id;
    1709 
    1710     if ( isset( $post_cache[$blog_id][$id] ) )
    1711         unset( $post_cache[$blog_id][$id] );
    1712 
    1713     if ( isset ($post_meta_cache[$blog_id][$id] ) )
    1714         unset( $post_meta_cache[$blog_id][$id] );
     1642    wp_cache_delete($id, 'posts');
     1643    wp_cache_delete($id, 'post_meta');
    17151644
    17161645    clean_object_term_cache($id, 'post');
     
    17181647
    17191648function update_page_cache(&$pages) {
    1720     global $page_cache, $blog_id;
    1721 
    1722     if ( !$pages )
    1723         return;
    1724 
    1725     for ($i = 0; $i < count($pages); $i++) {
    1726         $page_cache[$blog_id][$pages[$i]->ID] = &$pages[$i];
    1727         wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
    1728     }
     1649    update_post_cache($pages);
    17291650}
    17301651
    17311652function clean_page_cache($id) {
    1732     global $page_cache, $blog_id;
    1733 
    1734     if ( isset( $page_cache[$blog_id][$id] ) )
    1735         unset( $page_cache[$blog_id][$id] );
    1736 
    1737     wp_cache_delete($id, 'pages');
     1653    clean_post_cache($id);
     1654
    17381655    wp_cache_delete( 'all_page_ids', 'pages' );
    17391656    wp_cache_delete( 'get_pages', 'page' );
     
    17411658
    17421659function update_post_caches(&$posts) {
    1743     global $post_cache;
    1744     global $wpdb, $blog_id;
     1660    global $wpdb;
    17451661
    17461662    // No point in doing all this work if we didn't match any posts.
     
    17481664        return;
    17491665
    1750     // Get the categories for all the posts
    1751     for ($i = 0; $i < count($posts); $i++) {
    1752         $post_id_array[] = $posts[$i]->ID;
    1753         $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
    1754     }
    1755 
    1756     $post_id_list = implode(',', $post_id_array);
    1757 
    1758     update_object_term_cache($post_id_list, 'post');
    1759 
    1760     update_postmeta_cache($post_id_list);
    1761 }
    1762 
    1763 function update_postmeta_cache($post_id_list = '') {
    1764     global $wpdb, $post_meta_cache, $blog_id;
    1765 
    1766     // We should validate this comma-separated list for the upcoming SQL query
    1767     $post_id_list = preg_replace('|[^0-9,]|', '', $post_id_list);
    1768 
    1769     if ( empty( $post_id_list ) )
    1770         return false;
    1771 
    1772     // we're marking each post as having its meta cached (with no keys... empty array), to prevent posts with no meta keys from being queried again
    1773     // any posts that DO have keys will have this empty array overwritten with a proper array, down below
    1774     $post_id_array = (array) explode(',', $post_id_list);
    1775     $count = count( $post_id_array);
    1776     for ( $i = 0; $i < $count; $i++ ) {
    1777         $post_id = (int) $post_id_array[ $i ];
    1778         if ( isset( $post_meta_cache[$blog_id][$post_id] ) ) { // If the meta is already cached
    1779             unset( $post_id_array[ $i ] );
    1780             continue;
    1781         }
    1782         $post_meta_cache[$blog_id][$post_id] = array();
    1783     }
    1784     if ( count( $post_id_array ) == 0 )
    1785         return;
    1786     $post_id_list = join( ',', $post_id_array ); // with already cached stuff removeds
     1666    update_post_cache($posts);
     1667
     1668    $post_ids = array();
     1669
     1670    for ($i = 0; $i < count($posts); $i++)
     1671        $post_ids[] = $posts[$i]->ID;
     1672
     1673    update_object_term_cache($post_ids, 'post');
     1674
     1675    update_postmeta_cache($post_ids);
     1676}
     1677
     1678function update_postmeta_cache($post_ids) {
     1679    global $wpdb;
     1680
     1681    if ( empty( $post_ids ) )
     1682        return false;
     1683
     1684    $ids = array();
     1685    foreach ( $post_ids as $id ) {
     1686        if ( false === wp_cache_get($id, 'post_meta') )
     1687            $ids[] = $id;
     1688    }
     1689
     1690    if ( empty( $ids ) )
     1691        return false;
    17871692
    17881693    // Get post-meta info
    1789     if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN($post_id_list) ORDER BY post_id, meta_key", ARRAY_A) ) {
    1790         // Change from flat structure to hierarchical:
    1791         if ( !isset($post_meta_cache) )
    1792             $post_meta_cache[$blog_id] = array();
    1793 
     1694    $id_list = join(',', $ids);
     1695    $cache = array();
     1696    if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN ($id_list) ORDER BY post_id, meta_key", ARRAY_A) ) {
    17941697        foreach ($meta_list as $metarow) {
    17951698            $mpid = (int) $metarow['post_id'];
     
    17981701
    17991702            // Force subkeys to be array type:
    1800             if ( !isset($post_meta_cache[$blog_id][$mpid]) || !is_array($post_meta_cache[$blog_id][$mpid]) )
    1801                 $post_meta_cache[$blog_id][$mpid] = array();
    1802             if ( !isset($post_meta_cache[$blog_id][$mpid]["$mkey"]) || !is_array($post_meta_cache[$blog_id][$mpid]["$mkey"]) )
    1803                 $post_meta_cache[$blog_id][$mpid]["$mkey"] = array();
     1703            if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
     1704                $cache[$mpid] = array();
     1705            if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
     1706                $cache[$mpid][$mkey] = array();
    18041707
    18051708            // Add a value to the current pid/key:
    1806             $post_meta_cache[$blog_id][$mpid][$mkey][] = $mval;
    1807         }
    1808     }
     1709            $cache[$mpid][$mkey][] = $mval;
     1710        }
     1711    }
     1712
     1713    foreach ( $ids as $id ) {
     1714        if ( ! isset($cache[$id]) )
     1715            $cache[$id] = array();
     1716    }
     1717
     1718    foreach ( array_keys($cache) as $post)
     1719        wp_cache_set($post, $cache[$post], 'post_meta');
     1720
     1721    return $cache;
    18091722}
    18101723
Note: See TracChangeset for help on using the changeset viewer.