Some term caching. see #4189

git-svn-id: http://svn.automattic.com/wordpress/trunk@5598 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-05-30 03:36:59 +00:00
parent 7c39499aa0
commit df1a8696d5
3 changed files with 64 additions and 31 deletions

View File

@ -60,13 +60,15 @@ function get_category_parents($id, $link = FALSE, $separator = '/', $nicename =
} }
function get_the_category($id = false) { function get_the_category($id = false) {
global $post, $category_cache, $blog_id; global $post, $term_cache, $blog_id;
$id = (int) $id; $id = (int) $id;
if ( !$id ) if ( !$id )
$id = (int) $post->ID; $id = (int) $post->ID;
$categories = get_object_terms($id, 'category'); $categories = get_post_term_cache($id, 'category');
if ( false === $categories )
$categories = get_object_terms($id, 'category');
if ( !empty($categories) ) if ( !empty($categories) )
usort($categories, '_usort_terms_by_name'); usort($categories, '_usort_terms_by_name');
@ -416,7 +418,10 @@ function get_the_tags( $id = 0 ) {
if ( !$id ) if ( !$id )
$id = (int) $post->ID; $id = (int) $post->ID;
$tags = wp_get_post_tags( $id ); $tags = get_post_term_cache($id, 'post_tag');
if ( false === $tags )
$tags = get_object_terms($id, 'post_tag');
$tags = apply_filters( 'get_the_tags', $tags ); $tags = apply_filters( 'get_the_tags', $tags );
if ( empty( $tags ) ) if ( empty( $tags ) )
return false; return false;

View File

@ -1656,7 +1656,7 @@ function update_post_cache(&$posts) {
} }
function clean_post_cache($id) { function clean_post_cache($id) {
global $post_cache, $post_meta_cache, $category_cache, $tag_cache, $blog_id; global $post_cache, $post_meta_cache, $post_term_cache, $blog_id;
if ( isset( $post_cache[$blog_id][$id] ) ) if ( isset( $post_cache[$blog_id][$id] ) )
unset( $post_cache[$blog_id][$id] ); unset( $post_cache[$blog_id][$id] );
@ -1664,11 +1664,8 @@ function clean_post_cache($id) {
if ( isset ($post_meta_cache[$blog_id][$id] ) ) if ( isset ($post_meta_cache[$blog_id][$id] ) )
unset( $post_meta_cache[$blog_id][$id] ); unset( $post_meta_cache[$blog_id][$id] );
if ( isset( $category_cache[$blog_id][$id]) ) if ( isset( $post_term_cache[$blog_id][$id]) )
unset ( $category_cache[$blog_id][$id] ); unset ( $post_term_cache[$blog_id][$id] );
if ( isset( $tag_cache[$blog_id][$id]) )
unset ( $tag_cache[$blog_id][$id] );
} }
function update_page_cache(&$pages) { function update_page_cache(&$pages) {
@ -1694,10 +1691,19 @@ function clean_page_cache($id) {
wp_cache_delete( 'get_pages', 'page' ); wp_cache_delete( 'get_pages', 'page' );
} }
function update_post_category_cache($post_ids) { function &get_post_term_cache($id, $taxonomy) {
global $wpdb, $term_cache, $blog_id; global $post_term_cache, $blog_id;
// TODO
return; if ( isset($post_term_cache[$blog_id][$id][$taxonomy]) )
return $post_term_cache[$blog_id][$id][$taxonomy];
return false;
}
// TODO abstract this to work for any object.
function update_post_term_cache($post_ids) {
global $wpdb, $post_term_cache, $blog_id;
if ( empty($post_ids) ) if ( empty($post_ids) )
return; return;
@ -1708,7 +1714,7 @@ function update_post_category_cache($post_ids) {
$count = count( $post_id_array); $count = count( $post_id_array);
for ( $i = 0; $i < $count; $i++ ) { for ( $i = 0; $i < $count; $i++ ) {
$post_id = (int) $post_id_array[ $i ]; $post_id = (int) $post_id_array[ $i ];
if ( isset( $term_cache[$blog_id][$post_id] ) ) { if ( isset( $post_term_cache[$blog_id][$post_id] ) ) {
unset( $post_id_array[ $i ] ); unset( $post_id_array[ $i ] );
continue; continue;
} }
@ -1716,13 +1722,19 @@ function update_post_category_cache($post_ids) {
if ( count( $post_id_array ) == 0 ) if ( count( $post_id_array ) == 0 )
return; return;
$dogs = get_object_terms($post_id_array, array('category', 'post_tag')); $terms = get_object_terms($post_id_array, array('category', 'post_tag'), 'fields=all_with_object_id');
if ( empty($dogs) ) if ( empty($terms) )
return; return;
foreach ($dogs as $catt) { foreach ( $terms as $term )
$term_cache[$blog_id][$catt->post_id][$catt->taxonomy][$catt->category_id] = &get_category($catt->category_id); $post_term_cache[$blog_id][$term->object_id][$term->taxonomy][$term->term_id] = $term;
foreach ( $post_id_array as $id ) {
if ( ! isset($post_term_cache[$blog_id][$id]) ) {
$post_term_cache[$blog_id][$id]['category'] = array();
$post_term_cache[$blog_id][$id]['post_tag'] = array();
}
} }
} }
@ -1742,7 +1754,7 @@ function update_post_caches(&$posts) {
$post_id_list = implode(',', $post_id_array); $post_id_list = implode(',', $post_id_array);
update_post_category_cache($post_id_list); update_post_term_cache($post_id_list);
update_postmeta_cache($post_id_list); update_postmeta_cache($post_id_list);
} }

View File

@ -428,23 +428,27 @@ function get_object_terms($object_ids, $taxonomies, $args = array()) {
$object_ids = implode(', ', $object_ids); $object_ids = implode(', ', $object_ids);
if ( 'all' == $fields ) if ( 'all' == $fields )
$select_this = 't.*'; $select_this = 't.*, tt.*';
else if ( 'ids' == $fields ) else if ( 'ids' == $fields )
$select_this = 't.term_id'; $select_this = 't.term_id';
else if ( 'all_with_object_id' == $fields )
$select_this = 't.*, tt.*, tr.object_id';
$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"; $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";
if ( 'all' == $fields ) if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
$taxonomy_data = $wpdb->get_results($query); $terms = $wpdb->get_results($query);
else if ( 'ids' == $fields ) update_term_cache($terms);
$taxonomy_data = $wpdb->get_col($query); } else if ( 'ids' == $fields ) {
else if ( 'tt_ids' == $fields ) $terms = $wpdb->get_col($query);
$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"); } else if ( 'tt_ids' == $fields ) {
$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");
}
if ( ! $taxonomy_data ) if ( ! $terms )
return array(); return array();
return $taxonomy_data; return $terms;
} }
function &get_terms($taxonomies, $args = '') { function &get_terms($taxonomies, $args = '') {
@ -553,10 +557,12 @@ function &get_terms($taxonomies, $args = '') {
$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"; $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";
if ( 'all' == $fields ) if ( 'all' == $fields ) {
$terms = $wpdb->get_results($query); $terms = $wpdb->get_results($query);
else if ( 'ids' == $fields ) update_term_cache($terms);
} else if ( 'ids' == $fields ) {
$terms = $wpdb->get_col($query); $terms = $wpdb->get_col($query);
}
if ( empty($terms) ) if ( empty($terms) )
return array(); return array();
@ -590,7 +596,7 @@ function &get_terms($taxonomies, $args = '') {
*/ */
$cache[ $key ] = $terms; $cache[ $key ] = $terms;
wp_cache_add( 'get_terms', $cache, 'term' ); wp_cache_set( 'get_terms', $cache, 'term' );
$terms = apply_filters('get_terms', $terms, $taxonomies, $args); $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
return $terms; return $terms;
@ -679,6 +685,16 @@ function get_term_children( $term, $taxonomy ) {
return $children; return $children;
} }
function update_term_cache($terms, $taxonomy = '') {
foreach ( $terms as $term ) {
$term_taxonomy = $taxonomy;
if ( empty($term_taxonomy) )
$term_taxonomy = $term->taxonomy;
wp_cache_add($term->term_id, $term, $term_taxonomy);
}
}
function clean_term_cache($ids, $taxonomy) { function clean_term_cache($ids, $taxonomy) {
if ( !is_array($ids) ) if ( !is_array($ids) )
$ids = array($ids); $ids = array($ids);