WP_Site_Health::should_suggest_persistent_object_cache(): bool

In this article

Determines whether to suggest using a persistent object cache.

Return

bool Whether to suggest using a persistent object cache.

Source

public function should_suggest_persistent_object_cache() {
	global $wpdb;

	/**
	 * Filters whether to suggest use of a persistent object cache and bypass default threshold checks.
	 *
	 * Using this filter allows to override the default logic, effectively short-circuiting the method.
	 *
	 * @since 6.1.0
	 *
	 * @param bool|null $suggest Boolean to short-circuit, for whether to suggest using a persistent object cache.
	 *                           Default null.
	 */
	$short_circuit = apply_filters( 'site_status_should_suggest_persistent_object_cache', null );
	if ( is_bool( $short_circuit ) ) {
		return $short_circuit;
	}

	if ( is_multisite() ) {
		return true;
	}

	/**
	 * Filters the thresholds used to determine whether to suggest the use of a persistent object cache.
	 *
	 * @since 6.1.0
	 *
	 * @param int[] $thresholds The list of threshold numbers keyed by threshold name.
	 */
	$thresholds = apply_filters(
		'site_status_persistent_object_cache_thresholds',
		array(
			'alloptions_count' => 500,
			'alloptions_bytes' => 100000,
			'comments_count'   => 1000,
			'options_count'    => 1000,
			'posts_count'      => 1000,
			'terms_count'      => 1000,
			'users_count'      => 1000,
		)
	);

	$alloptions = wp_load_alloptions();

	if ( $thresholds['alloptions_count'] < count( $alloptions ) ) {
		return true;
	}

	if ( $thresholds['alloptions_bytes'] < strlen( serialize( $alloptions ) ) ) {
		return true;
	}

	$table_names = implode( "','", array( $wpdb->comments, $wpdb->options, $wpdb->posts, $wpdb->terms, $wpdb->users ) );

	// With InnoDB the `TABLE_ROWS` are estimates, which are accurate enough and faster to retrieve than individual `COUNT()` queries.
	$results = $wpdb->get_results(
		$wpdb->prepare(
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- This query cannot use interpolation.
			"SELECT TABLE_NAME AS 'table', TABLE_ROWS AS 'rows', SUM(data_length + index_length) as 'bytes' FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_NAME IN ('$table_names') GROUP BY TABLE_NAME;",
			DB_NAME
		),
		OBJECT_K
	);

	$threshold_map = array(
		'comments_count' => $wpdb->comments,
		'options_count'  => $wpdb->options,
		'posts_count'    => $wpdb->posts,
		'terms_count'    => $wpdb->terms,
		'users_count'    => $wpdb->users,
	);

	foreach ( $threshold_map as $threshold => $table ) {
		if ( $thresholds[ $threshold ] <= $results[ $table ]->rows ) {
			return true;
		}
	}

	return false;
}

Hooks

apply_filters( ‘site_status_persistent_object_cache_thresholds’, int[] $thresholds )

Filters the thresholds used to determine whether to suggest the use of a persistent object cache.

apply_filters( ‘site_status_should_suggest_persistent_object_cache’, bool|null $suggest )

Filters whether to suggest use of a persistent object cache and bypass default threshold checks.

Changelog

VersionDescription
6.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.