Term cache work. see #4189

git-svn-id: https://develop.svn.wordpress.org/trunk@5555 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2007-05-26 23:32:06 +00:00
parent b75bf365f8
commit 455d73121f
4 changed files with 235 additions and 221 deletions

View File

@ -122,4 +122,16 @@ function &get_tags($args = '') {
return $tags;
}
//
// Cache
//
function update_category_cache() {
return true;
}
function clean_category_cache($id) {
clean_term_cache($id, 'category');
}
?>

View File

@ -116,56 +116,6 @@ function get_weekstartend($mysqlstring, $start_of_week) {
return $week;
}
function get_lastpostdate($timezone = 'server') {
global $cache_lastpostdate, $pagenow, $wpdb, $blog_id;
$add_seconds_blog = get_option('gmt_offset') * 3600;
$add_seconds_server = date('Z');
if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
switch(strtolower($timezone)) {
case 'gmt':
$lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
break;
case 'blog':
$lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
break;
case 'server':
$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");
break;
}
$cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
} else {
$lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
}
return $lastpostdate;
}
function get_lastpostmodified($timezone = 'server') {
global $cache_lastpostmodified, $pagenow, $wpdb, $blog_id;
$add_seconds_blog = get_option('gmt_offset') * 3600;
$add_seconds_server = date('Z');
if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
switch(strtolower($timezone)) {
case 'gmt':
$lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
break;
case 'blog':
$lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
break;
case 'server':
$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");
break;
}
$lastpostdate = get_lastpostdate($timezone);
if ( $lastpostdate > $lastpostmodified ) {
$lastpostmodified = $lastpostdate;
}
$cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
} else {
$lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
}
return $lastpostmodified;
}
function maybe_unserialize($original) {
if ( is_serialized($original) ) // don't attempt to unserialize data that wasn't serialized going in
if ( false !== $gm = @ unserialize($original) )
@ -600,172 +550,6 @@ function is_new_day() {
}
}
function update_post_cache(&$posts) {
global $post_cache, $blog_id;
if ( !$posts )
return;
for ($i = 0; $i < count($posts); $i++) {
$post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
}
}
function clean_post_cache($id) {
global $post_cache, $post_meta_cache, $category_cache, $tag_cache, $blog_id;
if ( isset( $post_cache[$blog_id][$id] ) )
unset( $post_cache[$blog_id][$id] );
if ( isset ($post_meta_cache[$blog_id][$id] ) )
unset( $post_meta_cache[$blog_id][$id] );
if ( isset( $category_cache[$blog_id][$id]) )
unset ( $category_cache[$blog_id][$id] );
if ( isset( $tag_cache[$blog_id][$id]) )
unset ( $tag_cache[$blog_id][$id] );
}
function update_page_cache(&$pages) {
global $page_cache, $blog_id;
if ( !$pages )
return;
for ($i = 0; $i < count($pages); $i++) {
$page_cache[$blog_id][$pages[$i]->ID] = &$pages[$i];
wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
}
}
function clean_page_cache($id) {
global $page_cache, $blog_id;
if ( isset( $page_cache[$blog_id][$id] ) )
unset( $page_cache[$blog_id][$id] );
wp_cache_delete($id, 'pages');
wp_cache_delete( 'all_page_ids', 'pages' );
wp_cache_delete( 'get_pages', 'page' );
}
function update_post_category_cache($post_ids) {
global $wpdb, $category_cache, $tag_cache, $blog_id;
// TODO
return;
if ( empty($post_ids) )
return;
if ( is_array($post_ids) )
$post_id_list = implode(',', $post_ids);
$post_id_array = (array) explode(',', $post_ids);
$count = count( $post_id_array);
for ( $i = 0; $i < $count; $i++ ) {
$post_id = (int) $post_id_array[ $i ];
if ( isset( $category_cache[$blog_id][$post_id] ) ) {
unset( $post_id_array[ $i ] );
continue;
}
}
if ( count( $post_id_array ) == 0 )
return;
$post_id_list = join( ',', $post_id_array ); // with already cached stuff removed
$dogs = $wpdb->get_results("SELECT post_id, category_id, rel_type FROM $wpdb->post2cat WHERE post_id IN ($post_id_list)");
if ( empty($dogs) )
return;
foreach ($dogs as $catt) {
if ( 'category' == $catt->rel_type )
$category_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
elseif ( 'tag' == $catt->rel_type )
$tag_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
}
}
function update_post_caches(&$posts) {
global $post_cache, $category_cache, $post_meta_cache, $tag_cache;
global $wpdb, $blog_id;
// No point in doing all this work if we didn't match any posts.
if ( !$posts )
return;
// Get the categories for all the posts
for ($i = 0; $i < count($posts); $i++) {
$post_id_array[] = $posts[$i]->ID;
$post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
}
$post_id_list = implode(',', $post_id_array);
update_post_category_cache($post_id_list);
update_postmeta_cache($post_id_list);
}
function update_postmeta_cache($post_id_list = '') {
global $wpdb, $post_meta_cache, $blog_id;
// We should validate this comma-separated list for the upcoming SQL query
$post_id_list = preg_replace('|[^0-9,]|', '', $post_id_list);
if ( empty( $post_id_list ) )
return false;
// 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
// any posts that DO have keys will have this empty array overwritten with a proper array, down below
$post_id_array = (array) explode(',', $post_id_list);
$count = count( $post_id_array);
for ( $i = 0; $i < $count; $i++ ) {
$post_id = (int) $post_id_array[ $i ];
if ( isset( $post_meta_cache[$blog_id][$post_id] ) ) { // If the meta is already cached
unset( $post_id_array[ $i ] );
continue;
}
$post_meta_cache[$blog_id][$post_id] = array();
}
if ( count( $post_id_array ) == 0 )
return;
$post_id_list = join( ',', $post_id_array ); // with already cached stuff removeds
// Get post-meta info
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) ) {
// Change from flat structure to hierarchical:
if ( !isset($post_meta_cache) )
$post_meta_cache[$blog_id] = array();
foreach ($meta_list as $metarow) {
$mpid = (int) $metarow['post_id'];
$mkey = $metarow['meta_key'];
$mval = $metarow['meta_value'];
// Force subkeys to be array type:
if ( !isset($post_meta_cache[$blog_id][$mpid]) || !is_array($post_meta_cache[$blog_id][$mpid]) )
$post_meta_cache[$blog_id][$mpid] = array();
if ( !isset($post_meta_cache[$blog_id][$mpid]["$mkey"]) || !is_array($post_meta_cache[$blog_id][$mpid]["$mkey"]) )
$post_meta_cache[$blog_id][$mpid]["$mkey"] = array();
// Add a value to the current pid/key:
$post_meta_cache[$blog_id][$mpid][$mkey][] = $mval;
}
}
}
function update_category_cache() {
return true;
}
function clean_category_cache($id) {
wp_cache_delete($id, 'category');
wp_cache_delete('all_category_ids', 'category');
wp_cache_delete('get_categories', 'category');
delete_option('category_children');
}
/*
add_query_arg: Returns a modified querystring by adding
a single key & value or an associative array.

View File

@ -412,7 +412,7 @@ function wp_delete_post($postid = 0) {
if ( 'publish' == $post->post_status && 'post' == $post->post_type ) {
$categories = wp_get_post_categories($post->ID);
if( is_array( $categories ) ) {
if ( is_array( $categories ) ) {
foreach ( $categories as $cat_id ) {
$wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
wp_cache_delete($cat_id, 'category');
@ -1594,4 +1594,213 @@ function get_private_posts_cap_sql($post_type) {
return $sql;
}
function get_lastpostdate($timezone = 'server') {
global $cache_lastpostdate, $pagenow, $wpdb, $blog_id;
$add_seconds_blog = get_option('gmt_offset') * 3600;
$add_seconds_server = date('Z');
if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
switch(strtolower($timezone)) {
case 'gmt':
$lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
break;
case 'blog':
$lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
break;
case 'server':
$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");
break;
}
$cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
} else {
$lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
}
return $lastpostdate;
}
function get_lastpostmodified($timezone = 'server') {
global $cache_lastpostmodified, $pagenow, $wpdb, $blog_id;
$add_seconds_blog = get_option('gmt_offset') * 3600;
$add_seconds_server = date('Z');
if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
switch(strtolower($timezone)) {
case 'gmt':
$lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
break;
case 'blog':
$lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
break;
case 'server':
$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");
break;
}
$lastpostdate = get_lastpostdate($timezone);
if ( $lastpostdate > $lastpostmodified ) {
$lastpostmodified = $lastpostdate;
}
$cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
} else {
$lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
}
return $lastpostmodified;
}
//
// Cache
//
function update_post_cache(&$posts) {
global $post_cache, $blog_id;
if ( !$posts )
return;
for ($i = 0; $i < count($posts); $i++) {
$post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
}
}
function clean_post_cache($id) {
global $post_cache, $post_meta_cache, $category_cache, $tag_cache, $blog_id;
if ( isset( $post_cache[$blog_id][$id] ) )
unset( $post_cache[$blog_id][$id] );
if ( isset ($post_meta_cache[$blog_id][$id] ) )
unset( $post_meta_cache[$blog_id][$id] );
if ( isset( $category_cache[$blog_id][$id]) )
unset ( $category_cache[$blog_id][$id] );
if ( isset( $tag_cache[$blog_id][$id]) )
unset ( $tag_cache[$blog_id][$id] );
}
function update_page_cache(&$pages) {
global $page_cache, $blog_id;
if ( !$pages )
return;
for ($i = 0; $i < count($pages); $i++) {
$page_cache[$blog_id][$pages[$i]->ID] = &$pages[$i];
wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
}
}
function clean_page_cache($id) {
global $page_cache, $blog_id;
if ( isset( $page_cache[$blog_id][$id] ) )
unset( $page_cache[$blog_id][$id] );
wp_cache_delete($id, 'pages');
wp_cache_delete( 'all_page_ids', 'pages' );
wp_cache_delete( 'get_pages', 'page' );
}
function update_post_category_cache($post_ids) {
global $wpdb, $category_cache, $tag_cache, $blog_id;
// TODO
return;
if ( empty($post_ids) )
return;
if ( is_array($post_ids) )
$post_id_list = implode(',', $post_ids);
$post_id_array = (array) explode(',', $post_ids);
$count = count( $post_id_array);
for ( $i = 0; $i < $count; $i++ ) {
$post_id = (int) $post_id_array[ $i ];
if ( isset( $category_cache[$blog_id][$post_id] ) ) {
unset( $post_id_array[ $i ] );
continue;
}
}
if ( count( $post_id_array ) == 0 )
return;
$post_id_list = join( ',', $post_id_array ); // with already cached stuff removed
$dogs = $wpdb->get_results("SELECT post_id, category_id, rel_type FROM $wpdb->post2cat WHERE post_id IN ($post_id_list)");
if ( empty($dogs) )
return;
foreach ($dogs as $catt) {
if ( 'category' == $catt->rel_type )
$category_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
elseif ( 'tag' == $catt->rel_type )
$tag_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
}
}
function update_post_caches(&$posts) {
global $post_cache, $category_cache, $post_meta_cache, $tag_cache;
global $wpdb, $blog_id;
// No point in doing all this work if we didn't match any posts.
if ( !$posts )
return;
// Get the categories for all the posts
for ($i = 0; $i < count($posts); $i++) {
$post_id_array[] = $posts[$i]->ID;
$post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
}
$post_id_list = implode(',', $post_id_array);
update_post_category_cache($post_id_list);
update_postmeta_cache($post_id_list);
}
function update_postmeta_cache($post_id_list = '') {
global $wpdb, $post_meta_cache, $blog_id;
// We should validate this comma-separated list for the upcoming SQL query
$post_id_list = preg_replace('|[^0-9,]|', '', $post_id_list);
if ( empty( $post_id_list ) )
return false;
// 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
// any posts that DO have keys will have this empty array overwritten with a proper array, down below
$post_id_array = (array) explode(',', $post_id_list);
$count = count( $post_id_array);
for ( $i = 0; $i < $count; $i++ ) {
$post_id = (int) $post_id_array[ $i ];
if ( isset( $post_meta_cache[$blog_id][$post_id] ) ) { // If the meta is already cached
unset( $post_id_array[ $i ] );
continue;
}
$post_meta_cache[$blog_id][$post_id] = array();
}
if ( count( $post_id_array ) == 0 )
return;
$post_id_list = join( ',', $post_id_array ); // with already cached stuff removeds
// Get post-meta info
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) ) {
// Change from flat structure to hierarchical:
if ( !isset($post_meta_cache) )
$post_meta_cache[$blog_id] = array();
foreach ($meta_list as $metarow) {
$mpid = (int) $metarow['post_id'];
$mkey = $metarow['meta_key'];
$mval = $metarow['meta_value'];
// Force subkeys to be array type:
if ( !isset($post_meta_cache[$blog_id][$mpid]) || !is_array($post_meta_cache[$blog_id][$mpid]) )
$post_meta_cache[$blog_id][$mpid] = array();
if ( !isset($post_meta_cache[$blog_id][$mpid]["$mkey"]) || !is_array($post_meta_cache[$blog_id][$mpid]["$mkey"]) )
$post_meta_cache[$blog_id][$mpid]["$mkey"] = array();
// Add a value to the current pid/key:
$post_meta_cache[$blog_id][$mpid][$mkey][] = $mval;
}
}
}
?>

View File

@ -95,7 +95,7 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) {
$term_id = apply_filters('term_id_filter', $term_id, $tt_id);
//clean_term_cache($term_id);
clean_term_cache($term_id, $taxonomy);
do_action("created_term", $term_id, $tt_id);
do_action("created_$taxonomy", $term_id, $tt_id);
@ -146,7 +146,8 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) {
$wpdb->query("DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = '$tt_id'");
//clean_term_cache($term, $taxonomy);
clean_term_cache($term, $taxonomy);
do_action("delete_$taxonomy", $term, $tt_id);
return true;
@ -209,7 +210,7 @@ function wp_update_term( $term, $taxonomy, $args = array() ) {
$term_id = apply_filters('term_id_filter', $term_id, $tt_id);
//clean_term_cache($term_id);
clean_term_cache($term_id, $taxonomy);
do_action("edited_term", $term_id, $tt_id);
do_action("edited_$taxonomy", $term_id, $tt_id);
@ -509,7 +510,7 @@ function &get_terms($taxonomies, $args = '') {
*/
$cache[ $key ] = $terms;
wp_cache_add( 'get_terms', $cache, 'terms' );
wp_cache_add( 'get_terms', $cache, 'term' );
$terms = apply_filters('get_terms', $terms, $taxonomies, $args);
return $terms;
@ -582,6 +583,14 @@ function get_term_by($field, $value, $taxonomy, $output = OBJECT) {
}
}
function clean_term_cache($id, $taxonomy) {
wp_cache_delete($id, $taxonomy);
wp_cache_delete('all_ids', $taxonomy);
wp_cache_delete('get', $taxonomy);
delete_option("{$taxonomy}_children");
wp_cache_delete('get_terms', 'terms');
}
function _get_term_hierarchy($taxonomy) {
// TODO Make sure taxonomy is hierarchical
$children = get_option("{$taxonomy}_children");