Ticket #5182: post_cache.diff
File post_cache.diff, 16.4 KB (added by , 17 years ago) |
---|
-
wp-app.php
696 696 } 697 697 698 698 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; 700 700 log_app('function',"get_feed($page, '$post_type')"); 701 701 ob_start(); 702 702 … … 715 715 $wp_query = $GLOBALS['wp_query']; 716 716 $wpdb = $GLOBALS['wpdb']; 717 717 $blog_id = (int) $GLOBALS['blog_id']; 718 $post_cache = $GLOBALS['post_cache'];719 718 log_app('function',"query_posts(# " . print_r($wp_query, true) . "#)"); 720 719 721 720 log_app('function',"total_count(# $wp_query->max_num_pages #)"); … … 756 755 function get_entry($postID, $post_type = 'post') { 757 756 log_app('function',"get_entry($postID, '$post_type')"); 758 757 ob_start(); 759 global $posts, $post, $wp_query, $wp, $wpdb, $blog_id , $post_cache;758 global $posts, $post, $wp_query, $wp, $wpdb, $blog_id; 760 759 switch($post_type) { 761 760 case 'post': 762 761 $varname = 'p'; -
wp-includes/post.php
26 26 } 27 27 28 28 function &get_children($args = '', $output = OBJECT) { 29 global $ post_cache, $wpdb, $blog_id;29 global $wpdb; 30 30 31 31 if ( empty( $args ) ) { 32 32 if ( isset( $GLOBALS['post'] ) ) { … … 49 49 50 50 $children = get_posts( $r ); 51 51 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 { 52 if ( !$children ) 58 53 return false; 59 }60 54 55 update_post_cache($children); 56 57 foreach ( $children as $key => $child ) 58 $kids[$child->ID] =& $children[$key]; 59 61 60 if ( $output == OBJECT ) { 62 61 return $kids; 63 62 } elseif ( $output == ARRAY_A ) { … … 93 92 // Retrieves post data given a post ID or post object. 94 93 // Handles post caching. 95 94 function &get_post(&$post, $output = OBJECT, $filter = 'raw') { 96 global $ post_cache, $wpdb, $blog_id;95 global $wpdb; 97 96 98 97 if ( empty($post) ) { 99 98 if ( isset($GLOBALS['post']) ) 100 99 $_post = & $GLOBALS['post']; 101 100 else 102 $_post =null;101 return null; 103 102 } 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; 109 105 } else { 110 106 $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') ) { 116 108 $_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; 109 wp_cache_add($_post->ID, $_post, 'posts'); 120 110 } 121 111 } 122 112 123 if ( defined('WP_IMPORTING') )124 unset($post_cache[$blog_id]);125 126 113 $_post = sanitize_post($_post, $filter); 127 114 128 115 if ( $output == OBJECT ) { … … 273 260 // 274 261 275 262 function add_post_meta($post_id, $key, $value, $unique = false) { 276 global $wpdb , $post_meta_cache, $blog_id;263 global $wpdb; 277 264 278 265 if ( $unique ) { 279 266 // expected_slashed ($key) … … 282 269 } 283 270 } 284 271 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; 286 276 277 wp_cache_set($post_id, $cache, 'post_meta'); 278 287 279 $value = maybe_serialize($value); 288 280 289 281 // expected_slashed ($key) … … 293 285 } 294 286 295 287 function delete_post_meta($post_id, $key, $value = '') { 296 global $wpdb , $post_meta_cache, $blog_id;288 global $wpdb; 297 289 298 290 if ( empty($value) ) { 299 291 // expected_slashed ($key) … … 309 301 if ( empty($value) ) { 310 302 // expected_slashed ($key) 311 303 $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]);313 304 } else { 314 305 // expected_slashed ($key, $value) 315 306 $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 307 } 321 308 322 unset($post_meta_cache[$blog_id][$post_id][$key]);309 wp_cache_delete($post_id, 'post_meta'); 323 310 324 311 return true; 325 312 } 326 313 327 314 function get_post_meta($post_id, $key, $single = false) { 328 global $wpdb , $post_meta_cache, $blog_id;315 global $wpdb; 329 316 330 317 $post_id = (int) $post_id; 331 318 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]) ) { 333 322 if ( $single ) { 334 return maybe_unserialize( $ post_meta_cache[$blog_id][$post_id][$key][0] );323 return maybe_unserialize( $meta_cache[$key][0] ); 335 324 } else { 336 return maybe_unserialize( $ post_meta_cache[$blog_id][$post_id][$key] );325 return maybe_unserialize( $meta_cache[$key] ); 337 326 } 338 327 } 339 328 340 if ( ! isset($post_meta_cache[$blog_id][$post_id]) )329 if ( !$meta_cache ) { 341 330 update_postmeta_cache($post_id); 331 $meta_cache = wp_cache_get($post_id, 'post_meta'); 332 } 342 333 343 334 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]); 346 337 else 347 338 return ''; 348 } 349 return maybe_unserialize($ post_meta_cache[$blog_id][$post_id][$key]);339 } else { 340 return maybe_unserialize($meta_cache[$key]); 350 341 } 351 342 } 352 343 353 344 function update_post_meta($post_id, $key, $value, $prev_value = '') { 354 global $wpdb , $post_meta_cache, $blog_id;345 global $wpdb; 355 346 356 347 $original_value = $value; 357 348 $value = maybe_serialize($value); … … 367 358 if ( empty($prev_value) ) { 368 359 // expected_slashed ($key) 369 360 $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;374 361 } else { 375 362 // expected_slashed ($key) 376 363 $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 } 383 365 366 wp_cache_delete($post_id, 'post_meta'); 367 384 368 return true; 385 369 } 386 370 387 371 388 372 function delete_post_meta_by_key($post_meta_key) { 389 global $wpdb , $post_meta_cache, $blog_id;373 global $wpdb; 390 374 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'); 392 377 return true; 393 378 } 394 379 return false; … … 396 381 397 382 398 383 function get_post_custom($post_id = 0) { 399 global $id, $ post_meta_cache, $wpdb, $blog_id;384 global $id, $wpdb; 400 385 401 386 if ( !$post_id ) 402 387 $post_id = (int) $id; 403 388 404 389 $post_id = (int) $post_id; 405 390 406 if ( ! isset($post_meta_cache[$blog_id][$post_id]) )391 if ( ! wp_cache_get($post_id, 'post_meta') ) 407 392 update_postmeta_cache($post_id); 408 393 409 return $post_meta_cache[$blog_id][$post_id];394 return wp_cache_get($post_id, 'post_meta'); 410 395 } 411 396 412 397 function get_post_custom_keys( $post_id = 0 ) { … … 977 962 // Retrieves page data given a page ID or page object. 978 963 // Handles page caching. 979 964 function &get_page(&$page, $output = OBJECT, $filter = 'raw') { 980 global $wpdb, $blog_id;981 982 965 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 } 966 if ( isset( $GLOBALS['page'] ) && isset( $GLOBALS['page']->ID ) ) 967 return get_post($GLOBALS['page'], $output, $filter); 968 else 969 return null; 1016 970 } 1017 971 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 } 972 return get_post($page, $output, $filter); 1031 973 } 1032 974 1033 975 function get_page_by_path($page_path, $output = OBJECT) { … … 1071 1013 } 1072 1014 1073 1015 function &get_page_children($page_id, $pages) { 1074 global $page_cache, $blog_id;1075 1076 if ( empty($pages) )1077 $pages = &$page_cache[$blog_id];1078 1079 1016 $page_list = array(); 1080 1017 foreach ( $pages as $page ) { 1081 1018 if ( $page->post_parent == $page_id ) { … … 1694 1631 // 1695 1632 1696 1633 function update_post_cache(&$posts) { 1697 global $post_cache, $blog_id;1698 1699 1634 if ( !$posts ) 1700 1635 return; 1701 1636 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'); 1705 1639 } 1706 1640 1707 1641 function clean_post_cache($id) { 1708 global $post_cache, $post_meta_cache, $post_term_cache, $blog_id; 1642 wp_cache_delete($id, 'posts'); 1643 wp_cache_delete($id, 'post_meta'); 1709 1644 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] );1715 1716 1645 clean_object_term_cache($id, 'post'); 1717 1646 } 1718 1647 1719 1648 function 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); 1729 1650 } 1730 1651 1731 1652 function clean_page_cache($id) { 1732 global $page_cache, $blog_id;1653 clean_post_cache($id); 1733 1654 1734 if ( isset( $page_cache[$blog_id][$id] ) )1735 unset( $page_cache[$blog_id][$id] );1736 1737 wp_cache_delete($id, 'pages');1738 1655 wp_cache_delete( 'all_page_ids', 'pages' ); 1739 1656 wp_cache_delete( 'get_pages', 'page' ); 1740 1657 } 1741 1658 1742 1659 function update_post_caches(&$posts) { 1743 global $post_cache; 1744 global $wpdb, $blog_id; 1660 global $wpdb; 1745 1661 1746 1662 // No point in doing all this work if we didn't match any posts. 1747 1663 if ( !$posts ) 1748 1664 return; 1749 1665 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 } 1666 update_post_cache($posts); 1755 1667 1756 $post_id _list = implode(',', $post_id_array);1668 $post_ids = array(); 1757 1669 1758 update_object_term_cache($post_id_list, 'post'); 1670 for ($i = 0; $i < count($posts); $i++) 1671 $post_ids[] = $posts[$i]->ID; 1759 1672 1760 update_postmeta_cache($post_id_list); 1673 update_object_term_cache($post_ids, 'post'); 1674 1675 update_postmeta_cache($post_ids); 1761 1676 } 1762 1677 1763 function update_postmeta_cache($post_id _list = '') {1764 global $wpdb , $post_meta_cache, $blog_id;1678 function update_postmeta_cache($post_ids) { 1679 global $wpdb; 1765 1680 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 ) ) 1681 if ( empty( $post_ids ) ) 1770 1682 return false; 1771 1683 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(); 1684 $ids = array(); 1685 foreach ( $post_ids as $id ) { 1686 if ( false === wp_cache_get($id, 'post_meta') ) 1687 $ids[] = $id; 1783 1688 } 1784 if ( count( $post_id_array ) == 0 )1785 return;1786 $post_id_list = join( ',', $post_id_array ); // with already cached stuff removeds1787 1689 1788 // 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(); 1690 if ( empty( $ids ) ) 1691 return false; 1793 1692 1693 // Get post-meta info 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) ) { 1794 1697 foreach ($meta_list as $metarow) { 1795 1698 $mpid = (int) $metarow['post_id']; 1796 1699 $mkey = $metarow['meta_key']; 1797 1700 $mval = $metarow['meta_value']; 1798 1701 1799 1702 // 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(); 1804 1707 1805 1708 // Add a value to the current pid/key: 1806 $ post_meta_cache[$blog_id][$mpid][$mkey][] = $mval;1709 $cache[$mpid][$mkey][] = $mval; 1807 1710 } 1808 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; 1809 1722 } 1810 1723 1811 1724 // -
wp-admin/includes/post.php
348 348 if ( in_array($metakey, $protected) ) 349 349 return false; 350 350 351 wp_cache_delete($post_ID, 'post_meta'); 352 351 353 $result = $wpdb->query( " 352 354 INSERT INTO $wpdb->postmeta 353 355 (post_id,meta_key,meta_value ) … … 362 364 global $wpdb; 363 365 $mid = (int) $mid; 364 366 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 365 370 return $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id = '$mid'" ); 366 371 } 367 372 … … 408 413 if ( in_array($mkey, $protected) ) 409 414 return false; 410 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'); 418 411 419 $mvalue = maybe_serialize( stripslashes( $mvalue )); 412 420 $mvalue = $wpdb->escape( $mvalue ); 413 421 $mid = (int) $mid;