WP_Site_Health::should_suggest_persistent_object_cache()publicWP 6.1.0

Determines whether to suggest using a persistent object cache.

Метод класса: WP_Site_Health{}

Возвращает

true|false. Whether to suggest using a persistent object cache.

Использование

$WP_Site_Health = new WP_Site_Health();
$WP_Site_Health->should_suggest_persistent_object_cache();

Заметки

  • Global. wpdb. $wpdb WordPress database abstraction object.

Список изменений

С версии 6.1.0 Введена.

Код WP_Site_Health::should_suggest_persistent_object_cache() WP 6.1.1

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