Regenerate the term hierarchy cache (`{taxonomy}_children`) when it is out of sync with the passed taxonomy's `last_changed` value.
Introduces `taxonomy_hierarchy_is_fresh()`, which is only called in `_get_term_hierarchy()`. The taxonomy's `last_changed` value is checked against the value of `wp_cache_get( 'hierarchy_last_changed', $taxonomy )`. Adds a unit test - `Tests_Term:test_hierachy_invalidation()`. See [27101], which makes this type of cache invalidation possible. Fixes #14485. Built from https://develop.svn.wordpress.org/trunk@27102 git-svn-id: http://core.svn.wordpress.org/trunk@26971 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
578dbcd9f5
commit
5d03d8eb50
|
@ -2948,7 +2948,10 @@ function update_term_cache($terms, $taxonomy = '') {
|
|||
function _get_term_hierarchy($taxonomy) {
|
||||
if ( !is_taxonomy_hierarchical($taxonomy) )
|
||||
return array();
|
||||
$children = false;
|
||||
if ( taxonomy_hierarchy_is_fresh( $taxonomy ) ) {
|
||||
$children = get_option("{$taxonomy}_children");
|
||||
}
|
||||
|
||||
if ( is_array($children) )
|
||||
return $children;
|
||||
|
@ -3521,6 +3524,7 @@ function set_taxonomy_last_changed( $taxonomy ) {
|
|||
/**
|
||||
* Determine if a post's cache for the passed taxonomy
|
||||
* is in sync.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @param int $id
|
||||
|
@ -3536,3 +3540,23 @@ function post_taxonomy_is_fresh( $id, $taxonomy ) {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a hierarchy's cache for the passed taxonomy
|
||||
* is in sync.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @param int $id
|
||||
* @param string $taxonomy
|
||||
* @return boolean
|
||||
*/
|
||||
function taxonomy_hierarchy_is_fresh( $taxonomy ) {
|
||||
$last_changed = get_taxonomy_last_changed( $taxonomy );
|
||||
$hierarchy_last_changed = wp_cache_get( 'hierarchy_last_changed', $taxonomy );
|
||||
if ( ! $hierarchy_last_changed || $last_changed !== $hierarchy_last_changed ) {
|
||||
wp_cache_set( 'hierarchy_last_changed', $last_changed, $taxonomy );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
Loading…
Reference in New Issue