WordPress.org

Make WordPress Core

Changeset 5598


Ignore:
Timestamp:
05/30/07 03:36:59 (7 years ago)
Author:
ryan
Message:

Some term caching. see #4189

Location:
trunk/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/category-template.php

    r5590 r5598  
    6161 
    6262function get_the_category($id = false) { 
    63     global $post, $category_cache, $blog_id; 
     63    global $post, $term_cache, $blog_id; 
    6464 
    6565    $id = (int) $id; 
     
    6767        $id = (int) $post->ID; 
    6868 
    69     $categories = get_object_terms($id, 'category'); 
     69    $categories = get_post_term_cache($id, 'category'); 
     70    if ( false === $categories ) 
     71        $categories = get_object_terms($id, 'category'); 
    7072 
    7173    if ( !empty($categories) ) 
     
    417419        $id = (int) $post->ID; 
    418420 
    419     $tags = wp_get_post_tags( $id ); 
     421    $tags = get_post_term_cache($id, 'post_tag'); 
     422    if ( false === $tags ) 
     423        $tags = get_object_terms($id, 'post_tag'); 
     424 
    420425    $tags = apply_filters( 'get_the_tags', $tags ); 
    421426    if ( empty( $tags ) )  
  • trunk/wp-includes/post.php

    r5592 r5598  
    16571657 
    16581658function clean_post_cache($id) { 
    1659     global $post_cache, $post_meta_cache, $category_cache, $tag_cache, $blog_id; 
     1659    global $post_cache, $post_meta_cache, $post_term_cache, $blog_id; 
    16601660 
    16611661    if ( isset( $post_cache[$blog_id][$id] ) ) 
     
    16651665        unset( $post_meta_cache[$blog_id][$id] ); 
    16661666 
    1667     if ( isset( $category_cache[$blog_id][$id]) ) 
    1668         unset ( $category_cache[$blog_id][$id] ); 
    1669  
    1670     if ( isset( $tag_cache[$blog_id][$id]) ) 
    1671         unset ( $tag_cache[$blog_id][$id] ); 
     1667    if ( isset( $post_term_cache[$blog_id][$id]) ) 
     1668        unset ( $post_term_cache[$blog_id][$id] ); 
    16721669} 
    16731670 
     
    16951692} 
    16961693 
    1697 function update_post_category_cache($post_ids) { 
    1698     global $wpdb, $term_cache, $blog_id; 
    1699     // TODO 
    1700     return; 
     1694function &get_post_term_cache($id, $taxonomy) { 
     1695    global $post_term_cache, $blog_id; 
     1696 
     1697    if ( isset($post_term_cache[$blog_id][$id][$taxonomy]) ) 
     1698        return $post_term_cache[$blog_id][$id][$taxonomy]; 
     1699 
     1700    return false; 
     1701} 
     1702 
     1703// TODO abstract this to work for any object. 
     1704function update_post_term_cache($post_ids) { 
     1705    global $wpdb, $post_term_cache, $blog_id; 
     1706 
    17011707    if ( empty($post_ids) ) 
    17021708        return; 
     
    17091715    for ( $i = 0; $i < $count; $i++ ) { 
    17101716        $post_id = (int) $post_id_array[ $i ]; 
    1711         if ( isset( $term_cache[$blog_id][$post_id] ) ) { 
     1717        if ( isset( $post_term_cache[$blog_id][$post_id] ) ) { 
    17121718            unset( $post_id_array[ $i ] ); 
    17131719            continue; 
     
    17171723        return; 
    17181724 
    1719     $dogs = get_object_terms($post_id_array, array('category', 'post_tag')); 
    1720  
    1721     if ( empty($dogs) ) 
     1725    $terms = get_object_terms($post_id_array, array('category', 'post_tag'), 'fields=all_with_object_id'); 
     1726 
     1727    if ( empty($terms) ) 
    17221728        return; 
    17231729 
    1724     foreach ($dogs as $catt) { 
    1725         $term_cache[$blog_id][$catt->post_id][$catt->taxonomy][$catt->category_id] = &get_category($catt->category_id); 
     1730    foreach ( $terms as $term ) 
     1731        $post_term_cache[$blog_id][$term->object_id][$term->taxonomy][$term->term_id] = $term; 
     1732 
     1733    foreach ( $post_id_array as $id ) { 
     1734        if ( ! isset($post_term_cache[$blog_id][$id]) ) { 
     1735            $post_term_cache[$blog_id][$id]['category'] = array(); 
     1736            $post_term_cache[$blog_id][$id]['post_tag'] = array(); 
     1737        } 
    17261738    } 
    17271739} 
     
    17431755    $post_id_list = implode(',', $post_id_array); 
    17441756 
    1745     update_post_category_cache($post_id_list); 
     1757    update_post_term_cache($post_id_list); 
    17461758 
    17471759    update_postmeta_cache($post_id_list); 
  • trunk/wp-includes/taxonomy.php

    r5597 r5598  
    429429 
    430430    if ( 'all' == $fields ) 
    431         $select_this = 't.*'; 
     431        $select_this = 't.*, tt.*'; 
    432432    else if ( 'ids' == $fields ) 
    433433        $select_this = 't.term_id'; 
     434    else if ( 'all_with_object_id' == $fields ) 
     435        $select_this = 't.*, tt.*, tr.object_id'; 
    434436 
    435437    $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY $orderby $order"; 
    436438 
    437     if ( 'all' == $fields ) 
    438         $taxonomy_data = $wpdb->get_results($query); 
    439     else if ( 'ids' == $fields ) 
    440         $taxonomy_data = $wpdb->get_col($query); 
    441     else if ( 'tt_ids' == $fields ) 
    442         $taxonomy_data = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) ORDER BY tr.term_taxonomy_id $order"); 
    443  
    444     if ( ! $taxonomy_data ) 
     439    if ( 'all' == $fields || 'all_with_object_id' == $fields ) { 
     440        $terms = $wpdb->get_results($query); 
     441        update_term_cache($terms); 
     442    } else if ( 'ids' == $fields ) { 
     443        $terms = $wpdb->get_col($query); 
     444    } else if ( 'tt_ids' == $fields ) { 
     445        $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) ORDER BY tr.term_taxonomy_id $order"); 
     446    } 
     447 
     448    if ( ! $terms ) 
    445449        return array(); 
    446450 
    447     return $taxonomy_data; 
     451    return $terms; 
    448452} 
    449453 
     
    554558    $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $number"; 
    555559 
    556     if ( 'all' == $fields ) 
     560    if ( 'all' == $fields ) { 
    557561        $terms = $wpdb->get_results($query); 
    558     else if ( 'ids' == $fields ) 
     562        update_term_cache($terms); 
     563    } else if ( 'ids' == $fields ) { 
    559564        $terms = $wpdb->get_col($query); 
     565    } 
    560566 
    561567    if ( empty($terms) ) 
     
    591597 
    592598    $cache[ $key ] = $terms; 
    593     wp_cache_add( 'get_terms', $cache, 'term' ); 
     599    wp_cache_set( 'get_terms', $cache, 'term' ); 
    594600 
    595601    $terms = apply_filters('get_terms', $terms, $taxonomies, $args); 
     
    680686} 
    681687 
     688function update_term_cache($terms, $taxonomy = '') { 
     689    foreach ( $terms as $term ) { 
     690        $term_taxonomy = $taxonomy; 
     691        if ( empty($term_taxonomy) ) 
     692            $term_taxonomy = $term->taxonomy; 
     693 
     694        wp_cache_add($term->term_id, $term, $term_taxonomy); 
     695    } 
     696} 
     697 
    682698function clean_term_cache($ids, $taxonomy) { 
    683699    if ( !is_array($ids) ) 
Note: See TracChangeset for help on using the changeset viewer.