Make WordPress Core


Ignore:
Timestamp:
05/26/2007 11:32:06 PM (17 years ago)
Author:
ryan
Message:

Term cache work. see #4189

File:
1 edited

Legend:

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

    r5541 r5555  
    115115    $week['end'] = $week['start'] + 604799;
    116116    return $week;
    117 }
    118 
    119 function get_lastpostdate($timezone = 'server') {
    120     global $cache_lastpostdate, $pagenow, $wpdb, $blog_id;
    121     $add_seconds_blog = get_option('gmt_offset') * 3600;
    122     $add_seconds_server = date('Z');
    123     if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
    124         switch(strtolower($timezone)) {
    125             case 'gmt':
    126                 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
    127                 break;
    128             case 'blog':
    129                 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
    130                 break;
    131             case 'server':
    132                 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
    133                 break;
    134         }
    135         $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
    136     } else {
    137         $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
    138     }
    139     return $lastpostdate;
    140 }
    141 
    142 function get_lastpostmodified($timezone = 'server') {
    143     global $cache_lastpostmodified, $pagenow, $wpdb, $blog_id;
    144     $add_seconds_blog = get_option('gmt_offset') * 3600;
    145     $add_seconds_server = date('Z');
    146     if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
    147         switch(strtolower($timezone)) {
    148             case 'gmt':
    149                 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
    150                 break;
    151             case 'blog':
    152                 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
    153                 break;
    154             case 'server':
    155                 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
    156                 break;
    157         }
    158         $lastpostdate = get_lastpostdate($timezone);
    159         if ( $lastpostdate > $lastpostmodified ) {
    160             $lastpostmodified = $lastpostdate;
    161         }
    162         $cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
    163     } else {
    164         $lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
    165     }
    166     return $lastpostmodified;
    167117}
    168118
     
    599549        return(0);
    600550    }
    601 }
    602 
    603 function update_post_cache(&$posts) {
    604     global $post_cache, $blog_id;
    605 
    606     if ( !$posts )
    607         return;
    608 
    609     for ($i = 0; $i < count($posts); $i++) {
    610         $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
    611     }
    612 }
    613 
    614 function clean_post_cache($id) {
    615     global $post_cache, $post_meta_cache, $category_cache, $tag_cache, $blog_id;
    616 
    617     if ( isset( $post_cache[$blog_id][$id] ) )
    618         unset( $post_cache[$blog_id][$id] );
    619 
    620     if ( isset ($post_meta_cache[$blog_id][$id] ) )
    621         unset( $post_meta_cache[$blog_id][$id] );
    622 
    623     if ( isset( $category_cache[$blog_id][$id]) )
    624         unset ( $category_cache[$blog_id][$id] );
    625 
    626     if ( isset( $tag_cache[$blog_id][$id]) )
    627         unset ( $tag_cache[$blog_id][$id] );
    628 }
    629 
    630 function update_page_cache(&$pages) {
    631     global $page_cache, $blog_id;
    632 
    633     if ( !$pages )
    634         return;
    635 
    636     for ($i = 0; $i < count($pages); $i++) {
    637         $page_cache[$blog_id][$pages[$i]->ID] = &$pages[$i];
    638         wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
    639     }
    640 }
    641 
    642 function clean_page_cache($id) {
    643     global $page_cache, $blog_id;
    644 
    645     if ( isset( $page_cache[$blog_id][$id] ) )
    646         unset( $page_cache[$blog_id][$id] );
    647 
    648     wp_cache_delete($id, 'pages');
    649     wp_cache_delete( 'all_page_ids', 'pages' );
    650     wp_cache_delete( 'get_pages', 'page' );
    651 }
    652 
    653 function update_post_category_cache($post_ids) {
    654     global $wpdb, $category_cache, $tag_cache, $blog_id;
    655     // TODO
    656     return;
    657     if ( empty($post_ids) )
    658         return;
    659 
    660     if ( is_array($post_ids) )
    661         $post_id_list = implode(',', $post_ids);
    662 
    663     $post_id_array = (array) explode(',', $post_ids);
    664     $count = count( $post_id_array);
    665     for ( $i = 0; $i < $count; $i++ ) {
    666         $post_id = (int) $post_id_array[ $i ];
    667         if ( isset( $category_cache[$blog_id][$post_id] ) ) {
    668             unset( $post_id_array[ $i ] );
    669             continue;
    670         }
    671     }
    672     if ( count( $post_id_array ) == 0 )
    673         return;
    674     $post_id_list = join( ',', $post_id_array ); // with already cached stuff removed
    675 
    676     $dogs = $wpdb->get_results("SELECT post_id, category_id, rel_type FROM $wpdb->post2cat WHERE post_id IN ($post_id_list)");
    677 
    678     if ( empty($dogs) )
    679         return;
    680 
    681     foreach ($dogs as $catt) {
    682         if ( 'category' == $catt->rel_type )
    683             $category_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
    684         elseif ( 'tag' == $catt->rel_type )
    685             $tag_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
    686     }
    687 }
    688 
    689 function update_post_caches(&$posts) {
    690     global $post_cache, $category_cache, $post_meta_cache, $tag_cache;
    691     global $wpdb, $blog_id;
    692 
    693     // No point in doing all this work if we didn't match any posts.
    694     if ( !$posts )
    695         return;
    696 
    697     // Get the categories for all the posts
    698     for ($i = 0; $i < count($posts); $i++) {
    699         $post_id_array[] = $posts[$i]->ID;
    700         $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
    701     }
    702 
    703     $post_id_list = implode(',', $post_id_array);
    704 
    705     update_post_category_cache($post_id_list);
    706 
    707     update_postmeta_cache($post_id_list);
    708 }
    709 
    710 function update_postmeta_cache($post_id_list = '') {
    711     global $wpdb, $post_meta_cache, $blog_id;
    712 
    713     // We should validate this comma-separated list for the upcoming SQL query
    714     $post_id_list = preg_replace('|[^0-9,]|', '', $post_id_list);
    715 
    716     if ( empty( $post_id_list ) )
    717         return false;
    718 
    719     // 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
    720     // any posts that DO have keys will have this empty array overwritten with a proper array, down below
    721     $post_id_array = (array) explode(',', $post_id_list);
    722     $count = count( $post_id_array);
    723     for ( $i = 0; $i < $count; $i++ ) {
    724         $post_id = (int) $post_id_array[ $i ];
    725         if ( isset( $post_meta_cache[$blog_id][$post_id] ) ) { // If the meta is already cached
    726             unset( $post_id_array[ $i ] );
    727             continue;
    728         }
    729         $post_meta_cache[$blog_id][$post_id] = array();
    730     }
    731     if ( count( $post_id_array ) == 0 )
    732         return;
    733     $post_id_list = join( ',', $post_id_array ); // with already cached stuff removeds
    734 
    735     // Get post-meta info
    736     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) ) {
    737         // Change from flat structure to hierarchical:
    738         if ( !isset($post_meta_cache) )
    739             $post_meta_cache[$blog_id] = array();
    740 
    741         foreach ($meta_list as $metarow) {
    742             $mpid = (int) $metarow['post_id'];
    743             $mkey = $metarow['meta_key'];
    744             $mval = $metarow['meta_value'];
    745 
    746             // Force subkeys to be array type:
    747             if ( !isset($post_meta_cache[$blog_id][$mpid]) || !is_array($post_meta_cache[$blog_id][$mpid]) )
    748                 $post_meta_cache[$blog_id][$mpid] = array();
    749             if ( !isset($post_meta_cache[$blog_id][$mpid]["$mkey"]) || !is_array($post_meta_cache[$blog_id][$mpid]["$mkey"]) )
    750                 $post_meta_cache[$blog_id][$mpid]["$mkey"] = array();
    751 
    752             // Add a value to the current pid/key:
    753             $post_meta_cache[$blog_id][$mpid][$mkey][] = $mval;
    754         }
    755     }
    756 }
    757 
    758 function update_category_cache() {
    759     return true;
    760 }
    761 
    762 function clean_category_cache($id) {
    763     wp_cache_delete($id, 'category');
    764     wp_cache_delete('all_category_ids', 'category');
    765     wp_cache_delete('get_categories', 'category');
    766     delete_option('category_children');
    767551}
    768552
Note: See TracChangeset for help on using the changeset viewer.