Make WordPress Core

Changeset 5555


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

Term cache work. see #4189

Location:
trunk/wp-includes
Files:
4 edited

Legend:

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

    r5553 r5555  
    123123}
    124124
     125//
     126// Cache
     127//
     128
     129function update_category_cache() {
     130    return true;
     131}
     132
     133function clean_category_cache($id) {
     134    clean_term_cache($id, 'category');
     135}
     136
    125137?>
  • 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
  • trunk/wp-includes/post.php

    r5553 r5555  
    413413    if ( 'publish' == $post->post_status && 'post' == $post->post_type ) {
    414414        $categories = wp_get_post_categories($post->ID);
    415         if( is_array( $categories ) ) {
     415        if ( is_array( $categories ) ) {
    416416            foreach ( $categories as $cat_id ) {
    417417                $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
     
    15951595}
    15961596
     1597function get_lastpostdate($timezone = 'server') {
     1598    global $cache_lastpostdate, $pagenow, $wpdb, $blog_id;
     1599    $add_seconds_blog = get_option('gmt_offset') * 3600;
     1600    $add_seconds_server = date('Z');
     1601    if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
     1602        switch(strtolower($timezone)) {
     1603            case 'gmt':
     1604                $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
     1605                break;
     1606            case 'blog':
     1607                $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
     1608                break;
     1609            case 'server':
     1610                $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");
     1611                break;
     1612        }
     1613        $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
     1614    } else {
     1615        $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
     1616    }
     1617    return $lastpostdate;
     1618}
     1619
     1620function get_lastpostmodified($timezone = 'server') {
     1621    global $cache_lastpostmodified, $pagenow, $wpdb, $blog_id;
     1622    $add_seconds_blog = get_option('gmt_offset') * 3600;
     1623    $add_seconds_server = date('Z');
     1624    if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
     1625        switch(strtolower($timezone)) {
     1626            case 'gmt':
     1627                $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
     1628                break;
     1629            case 'blog':
     1630                $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
     1631                break;
     1632            case 'server':
     1633                $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");
     1634                break;
     1635        }
     1636        $lastpostdate = get_lastpostdate($timezone);
     1637        if ( $lastpostdate > $lastpostmodified ) {
     1638            $lastpostmodified = $lastpostdate;
     1639        }
     1640        $cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
     1641    } else {
     1642        $lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
     1643    }
     1644    return $lastpostmodified;
     1645}
     1646
     1647//
     1648// Cache
     1649//
     1650
     1651function update_post_cache(&$posts) {
     1652    global $post_cache, $blog_id;
     1653
     1654    if ( !$posts )
     1655        return;
     1656
     1657    for ($i = 0; $i < count($posts); $i++) {
     1658        $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
     1659    }
     1660}
     1661
     1662function clean_post_cache($id) {
     1663    global $post_cache, $post_meta_cache, $category_cache, $tag_cache, $blog_id;
     1664
     1665    if ( isset( $post_cache[$blog_id][$id] ) )
     1666        unset( $post_cache[$blog_id][$id] );
     1667
     1668    if ( isset ($post_meta_cache[$blog_id][$id] ) )
     1669        unset( $post_meta_cache[$blog_id][$id] );
     1670
     1671    if ( isset( $category_cache[$blog_id][$id]) )
     1672        unset ( $category_cache[$blog_id][$id] );
     1673
     1674    if ( isset( $tag_cache[$blog_id][$id]) )
     1675        unset ( $tag_cache[$blog_id][$id] );
     1676}
     1677
     1678function update_page_cache(&$pages) {
     1679    global $page_cache, $blog_id;
     1680
     1681    if ( !$pages )
     1682        return;
     1683
     1684    for ($i = 0; $i < count($pages); $i++) {
     1685        $page_cache[$blog_id][$pages[$i]->ID] = &$pages[$i];
     1686        wp_cache_add($pages[$i]->ID, $pages[$i], 'pages');
     1687    }
     1688}
     1689
     1690function clean_page_cache($id) {
     1691    global $page_cache, $blog_id;
     1692
     1693    if ( isset( $page_cache[$blog_id][$id] ) )
     1694        unset( $page_cache[$blog_id][$id] );
     1695
     1696    wp_cache_delete($id, 'pages');
     1697    wp_cache_delete( 'all_page_ids', 'pages' );
     1698    wp_cache_delete( 'get_pages', 'page' );
     1699}
     1700
     1701function update_post_category_cache($post_ids) {
     1702    global $wpdb, $category_cache, $tag_cache, $blog_id;
     1703    // TODO
     1704    return;
     1705    if ( empty($post_ids) )
     1706        return;
     1707
     1708    if ( is_array($post_ids) )
     1709        $post_id_list = implode(',', $post_ids);
     1710
     1711    $post_id_array = (array) explode(',', $post_ids);
     1712    $count = count( $post_id_array);
     1713    for ( $i = 0; $i < $count; $i++ ) {
     1714        $post_id = (int) $post_id_array[ $i ];
     1715        if ( isset( $category_cache[$blog_id][$post_id] ) ) {
     1716            unset( $post_id_array[ $i ] );
     1717            continue;
     1718        }
     1719    }
     1720    if ( count( $post_id_array ) == 0 )
     1721        return;
     1722    $post_id_list = join( ',', $post_id_array ); // with already cached stuff removed
     1723
     1724    $dogs = $wpdb->get_results("SELECT post_id, category_id, rel_type FROM $wpdb->post2cat WHERE post_id IN ($post_id_list)");
     1725
     1726    if ( empty($dogs) )
     1727        return;
     1728
     1729    foreach ($dogs as $catt) {
     1730        if ( 'category' == $catt->rel_type )
     1731            $category_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
     1732        elseif ( 'tag' == $catt->rel_type )
     1733            $tag_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
     1734    }
     1735}
     1736
     1737function update_post_caches(&$posts) {
     1738    global $post_cache, $category_cache, $post_meta_cache, $tag_cache;
     1739    global $wpdb, $blog_id;
     1740
     1741    // No point in doing all this work if we didn't match any posts.
     1742    if ( !$posts )
     1743        return;
     1744
     1745    // Get the categories for all the posts
     1746    for ($i = 0; $i < count($posts); $i++) {
     1747        $post_id_array[] = $posts[$i]->ID;
     1748        $post_cache[$blog_id][$posts[$i]->ID] = &$posts[$i];
     1749    }
     1750
     1751    $post_id_list = implode(',', $post_id_array);
     1752
     1753    update_post_category_cache($post_id_list);
     1754
     1755    update_postmeta_cache($post_id_list);
     1756}
     1757
     1758function update_postmeta_cache($post_id_list = '') {
     1759    global $wpdb, $post_meta_cache, $blog_id;
     1760
     1761    // We should validate this comma-separated list for the upcoming SQL query
     1762    $post_id_list = preg_replace('|[^0-9,]|', '', $post_id_list);
     1763
     1764    if ( empty( $post_id_list ) )
     1765        return false;
     1766
     1767    // 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
     1768    // any posts that DO have keys will have this empty array overwritten with a proper array, down below
     1769    $post_id_array = (array) explode(',', $post_id_list);
     1770    $count = count( $post_id_array);
     1771    for ( $i = 0; $i < $count; $i++ ) {
     1772        $post_id = (int) $post_id_array[ $i ];
     1773        if ( isset( $post_meta_cache[$blog_id][$post_id] ) ) { // If the meta is already cached
     1774            unset( $post_id_array[ $i ] );
     1775            continue;
     1776        }
     1777        $post_meta_cache[$blog_id][$post_id] = array();
     1778    }
     1779    if ( count( $post_id_array ) == 0 )
     1780        return;
     1781    $post_id_list = join( ',', $post_id_array ); // with already cached stuff removeds
     1782
     1783    // Get post-meta info
     1784    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) ) {
     1785        // Change from flat structure to hierarchical:
     1786        if ( !isset($post_meta_cache) )
     1787            $post_meta_cache[$blog_id] = array();
     1788
     1789        foreach ($meta_list as $metarow) {
     1790            $mpid = (int) $metarow['post_id'];
     1791            $mkey = $metarow['meta_key'];
     1792            $mval = $metarow['meta_value'];
     1793
     1794            // Force subkeys to be array type:
     1795            if ( !isset($post_meta_cache[$blog_id][$mpid]) || !is_array($post_meta_cache[$blog_id][$mpid]) )
     1796                $post_meta_cache[$blog_id][$mpid] = array();
     1797            if ( !isset($post_meta_cache[$blog_id][$mpid]["$mkey"]) || !is_array($post_meta_cache[$blog_id][$mpid]["$mkey"]) )
     1798                $post_meta_cache[$blog_id][$mpid]["$mkey"] = array();
     1799
     1800            // Add a value to the current pid/key:
     1801            $post_meta_cache[$blog_id][$mpid][$mkey][] = $mval;
     1802        }
     1803    }
     1804}
     1805
    15971806?>
  • trunk/wp-includes/taxonomy.php

    r5553 r5555  
    9696    $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
    9797
    98     //clean_term_cache($term_id);
     98    clean_term_cache($term_id, $taxonomy);
    9999
    100100    do_action("created_term", $term_id, $tt_id);
     
    147147    $wpdb->query("DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = '$tt_id'");
    148148
    149     //clean_term_cache($term, $taxonomy);
     149    clean_term_cache($term, $taxonomy);
     150
    150151    do_action("delete_$taxonomy", $term, $tt_id);
    151152
     
    210211    $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
    211212
    212     //clean_term_cache($term_id);
     213    clean_term_cache($term_id, $taxonomy);
    213214
    214215    do_action("edited_term", $term_id, $tt_id);
     
    510511
    511512    $cache[ $key ] = $terms;
    512     wp_cache_add( 'get_terms', $cache, 'terms' );
     513    wp_cache_add( 'get_terms', $cache, 'term' );
    513514
    514515    $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
     
    583584}
    584585
     586function clean_term_cache($id, $taxonomy) {
     587    wp_cache_delete($id, $taxonomy);
     588    wp_cache_delete('all_ids', $taxonomy);
     589    wp_cache_delete('get', $taxonomy);
     590    delete_option("{$taxonomy}_children");
     591    wp_cache_delete('get_terms', 'terms');
     592}
     593
    585594function _get_term_hierarchy($taxonomy) {
    586595    // TODO Make sure taxonomy is hierarchical
Note: See TracChangeset for help on using the changeset viewer.