Make WordPress Core

Ticket #14485: garyc40-14485-rev6.patch

File garyc40-14485-rev6.patch, 4.2 KB (added by garyc40, 14 years ago)

properly invalidate get_terms cache of specific taxonomies

  • wp-includes/taxonomy.php

    diff --git wp-includes/taxonomy.php wp-includes/taxonomy.php
    index c2797b0..0bf426a 100644
    function &get_terms($taxonomies, $args = '') { 
    11501150        // $args can be whatever, only use the args defined in defaults to compute the key
    11511151        $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
    11521152        $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
    1153         $last_changed = wp_cache_get('last_changed', 'terms');
    1154         if ( !$last_changed ) {
    1155                 $last_changed = time();
    1156                 wp_cache_set('last_changed', $last_changed, 'terms');
     1153        $cache_key = "get_terms:$key";
     1154       
     1155        // save $key so that it could be invalidated later
     1156        $cache_keys = wp_cache_get( 'get_terms:cache_keys', 'terms' );
     1157        if ( ! $cache_keys ) {
     1158                $cache_keys = array();
    11571159        }
    1158         $cache_key = "get_terms:$key:$last_changed";
     1160        if ( ! isset( $cache_keys[$key] ) ) {
     1161                $cache_keys[$key] = $taxonomies;
     1162        }
     1163        wp_cache_set( 'get_terms:cache_keys', $cache_keys, 'terms' );
     1164
    11591165        $cache = wp_cache_get( $cache_key, 'terms' );
    11601166        if ( false !== $cache ) {
    11611167                $cache = apply_filters('get_terms', $cache, $taxonomies, $args);
    function &get_terms($taxonomies, $args = '') { 
    12971303
    12981304        if ( 'count' == $fields ) {
    12991305                $term_count = $wpdb->get_var($query);
     1306                wp_cache_add( $cache_key, $term_count, 'terms' );
    13001307                return $term_count;
    13011308        }
    13021309
    function wp_delete_term( $term, $taxonomy, $args = array() ) { 
    17071714        if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
    17081715                $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) );
    17091716
    1710         clean_term_cache($term, $taxonomy);
     1717        clean_term_cache($term, $taxonomy, true, true);
    17111718
    17121719        do_action('delete_term', $term, $tt_id, $taxonomy);
    17131720        do_action("delete_$taxonomy", $term, $tt_id);
    function wp_insert_term( $term, $taxonomy, $args = array() ) { 
    20012008
    20022009        $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
    20032010
    2004         clean_term_cache($term_id, $taxonomy);
     2011        clean_term_cache( $term_id, $taxonomy, true, (bool) $parent );
    20052012
    20062013        do_action("created_term", $term_id, $tt_id, $taxonomy);
    20072014        do_action("created_$taxonomy", $term_id, $tt_id);
    function clean_object_term_cache($object_ids, $object_type) { 
    24402447 * @param int|array $ids Single or list of Term IDs
    24412448 * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context.
    24422449 * @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true.
     2450 * @param bool $force_clean_taxonomy Whether to force clean taxonomy wide caches (true), or skip if it's already done (false). Default is false
     2451 *
    24432452 */
    2444 function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
     2453function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true, $force_clean_taxonomy = false) {
    24452454        global $wpdb;
    24462455        static $cleaned = array();
    24472456
    function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { 
    24712480        }
    24722481
    24732482        foreach ( $taxonomies as $taxonomy ) {
    2474                 if ( isset($cleaned[$taxonomy]) )
     2483                if ( isset($cleaned[$taxonomy]) && ! $force_clean_taxonomy )
    24752484                        continue;
    24762485                $cleaned[$taxonomy] = true;
    24772486
    24782487                if ( $clean_taxonomy ) {
    24792488                        wp_cache_delete('all_ids', $taxonomy);
    24802489                        wp_cache_delete('get', $taxonomy);
     2490                       
     2491                        // clear get_terms cache
     2492                        $cache_keys = wp_cache_get( 'get_terms:cache_keys', 'terms' );
     2493                        if ( ! empty( $cache_keys ) ) {
     2494                                foreach ( $cache_keys as $key => $cache_taxonomies ) {
     2495                                        if ( in_array( $taxonomy, $cache_taxonomies ) ) {
     2496                                                wp_cache_delete( "get_terms:{$key}", 'terms' );
     2497                                                unset( $cache_keys[$key] );
     2498                                        }
     2499                                }
     2500                        } else {
     2501                                $cache_keys = array();
     2502                        }
     2503                        wp_cache_set( 'get_terms:cache_keys', $cache_keys, 'terms' );
     2504                       
    24812505                        delete_option("{$taxonomy}_children");
    24822506                        // Regenerate {$taxonomy}_children
    24832507                        _get_term_hierarchy($taxonomy);
    function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { 
    24852509
    24862510                do_action('clean_term_cache', $ids, $taxonomy);
    24872511        }
    2488 
    2489         wp_cache_set('last_changed', time(), 'terms');
    24902512}
    24912513
    24922514