wp_suspend_cache_invalidation()WP 2.7.0

Suspends cache invalidation.

Turns cache invalidation on and off. Useful during imports where you don't want to do invalidations every time a post is inserted. Callers must be sure that what they are doing won't lead to an inconsistent cache when invalidation is suspended.

No Hooks.

Return

true|false. The current suspend setting.

Usage

wp_suspend_cache_invalidation( $suspend );
$suspend(true|false)
Whether to suspend or 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.1.1

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;
}