wp_suspend_cache_invalidation()WP 2.7.0

Suspends the deletion/resetting of cache objects.

Enables/disables cache invalidation. Useful during import (data addition) when it is not necessary to invalidate (delete) the cache every time, for example, when inserting posts.

This is important when all functions must be sure that they are working with the same data from the cache; for this, we disable cache resetting so that the same cache, added the first time, is always used.

With wp_suspend_cache_invalidation( true ) the following functions will be disabled:

No Hooks.

Returns

true|false. The current value.

Usage

wp_suspend_cache_invalidation( $suspend );
$suspend(true|false)
Whether to suspend/enable cache invalidation.
Default: true

Examples

0

#1 Temporarily disable all cache zeroing functions

Suppose we are adding a lot of data to the database, we are only adding data and so we don't need to zero the cache (because it doesn't change). It takes some time to zero the cache, to save it, we can disable cache zeroing at the moment of data insertion into the database:

// Suspend a bunch of stuff in the WP core
wp_defer_term_counting( true );
wp_defer_comment_counting( true );
wp_suspend_cache_invalidation( true );

//insert data
insert_categories();
insert_tags();
insert_terms();
insert_posts();

// Enabling a heap of everything in the WP core
wp_suspend_cache_invalidation( false );
wp_cache_flush(); // reset all cache
wp_defer_term_counting( false ); // recount everything
wp_defer_comment_counting( false ); // recount everything

Notes

  • Global. true|false. $_wp_suspend_cache_invalidation

Changelog

Since 2.7.0 Introduced.

wp_suspend_cache_invalidation() code WP 6.8.3

function wp_suspend_cache_invalidation( $suspend = true ) {
	global $_wp_suspend_cache_invalidation;

	$current_suspend                = $_wp_suspend_cache_invalidation;
	$_wp_suspend_cache_invalidation = $suspend;
	return $current_suspend;
}