Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Site Health test for Object Caching #1301

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/performance-lab/includes/site-health/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@
// AVIF Support site health check.
require_once __DIR__ . '/avif-support/helper.php';
require_once __DIR__ . '/avif-support/hooks.php';

// Object Cache Support site health info.
require_once __DIR__ . '/object-cache/helper.php';
require_once __DIR__ . '/object-cache/hooks.php';
127 changes: 127 additions & 0 deletions plugins/performance-lab/includes/site-health/object-cache/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Helper functions used for Object Cache Support Info.
*
* @package performance-lab
* @since n.e.x.t
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Callback for Object Cache Info fields.
*
* @return array<string, array{label: string, value: string}> Fields.
* @since n.e.x.t
Comment on lines +15 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*
* @return array<string, array{label: string, value: string}> Fields.
* @since n.e.x.t
*
* @since n.e.x.t
*
* @return array<string, array{label: string, value: string}> Fields.

*/
function perflab_object_cache_supported_fields(): array {
return array(
'extension' => array(
'label' => __( 'Extension', 'performance-lab' ),
'value' => perflab_get_cache_type(),
),
'multiple_gets' => array(
'label' => __( 'Multiple gets', 'performance-lab' ),
'value' => wp_cache_supports( 'get_multiple' ) ? __( 'Enabled', 'performance-lab' ) : __( 'Disabled', 'performance-lab' ),
),
'multiple_sets' => array(
'label' => __( 'Multiple sets', 'performance-lab' ),
'value' => wp_cache_supports( 'set_multiple' ) ? __( 'Enabled', 'performance-lab' ) : __( 'Disabled', 'performance-lab' ),
),
'multiple_deletes' => array(
'label' => __( 'Multiple deletes', 'performance-lab' ),
'value' => wp_cache_supports( 'delete_multiple' ) ? __( 'Enabled', 'performance-lab' ) : __( 'Disabled', 'performance-lab' ),
),
'flush_group' => array(
'label' => __( 'Flush group', 'performance-lab' ),
'value' => wp_cache_supports( 'flush_group' ) ? __( 'Enabled', 'performance-lab' ) : __( 'Disabled', 'performance-lab' ),
),
);
}

/**
* Attempts to determine which object cache is being used as in wp-cli repository.
*
* Note that the guesses made by this function are based on the WP_Object_Cache classes
* that define the 3rd party object cache extension. Changes to those classes could render
* problems with this function's ability to determine which object cache is being used.
*
westonruter marked this conversation as resolved.
Show resolved Hide resolved
* This function was copied from WP-CLI.
*
* @link https://github.com/wp-cli/wp-cli/blob/0ca6d920123ac904c918d69181edc5071dc92c9d/php/utils-wp.php#L259-L331.
*
* @global bool $_wp_using_ext_object_cache Whether external object cache is being used.
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
*
* @return string Object cache type.
* @since n.e.x.t
Comment on lines +52 to +59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically the @since goes among the first tags.

Suggested change
*
* @link https://github.com/wp-cli/wp-cli/blob/0ca6d920123ac904c918d69181edc5071dc92c9d/php/utils-wp.php#L259-L331.
*
* @global bool $_wp_using_ext_object_cache Whether external object cache is being used.
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
*
* @return string Object cache type.
* @since n.e.x.t
*
* @since n.e.x.t
* @link https://github.com/wp-cli/wp-cli/blob/0ca6d920123ac904c918d69181edc5071dc92c9d/php/utils-wp.php#L259-L331.
*
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
*
* @return string Object cache type.

This also removes the $_wp_using_ext_object_cache per below.

*/
function perflab_get_cache_type(): string {
global $_wp_using_ext_object_cache, $wp_object_cache;
westonruter marked this conversation as resolved.
Show resolved Hide resolved

$message = '';

if ( ! empty( $_wp_using_ext_object_cache ) ) {
Comment on lines +62 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per #1219 let's avoid using empty() here. And it seems we can avoid using the $_wp_using_ext_object_cache private global by instead just using the wp_using_ext_object_cache() function:

Suggested change
global $_wp_using_ext_object_cache, $wp_object_cache;
$message = '';
if ( ! empty( $_wp_using_ext_object_cache ) ) {
global $wp_object_cache;
$message = '';
if ( wp_using_ext_object_cache() ) {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter will fix mentioned issues after 12 of August, as I just started holidays.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Enjoy!

// Test for Memcached PECL extension memcached object cache (https://github.com/tollmanz/wordpress-memcached-backend).
if ( isset( $wp_object_cache->m ) && $wp_object_cache->m instanceof \Memcached ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're not using namespaces, the \ an be removed.

Suggested change
if ( isset( $wp_object_cache->m ) && $wp_object_cache->m instanceof \Memcached ) {
if ( isset( $wp_object_cache->m ) && $wp_object_cache->m instanceof Memcached ) {

$message = 'Memcached';

// Test for Memcache PECL extension memcached object cache (https://wordpress.org/extend/plugins/memcached/).
} elseif ( isset( $wp_object_cache->mc ) ) {
$is_memcache = true;
foreach ( $wp_object_cache->mc as $bucket ) {
if ( ! $bucket instanceof \Memcache && ! $bucket instanceof \Memcached ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( ! $bucket instanceof \Memcache && ! $bucket instanceof \Memcached ) {
if ( ! $bucket instanceof Memcache && ! $bucket instanceof Memcached ) {

$is_memcache = false;
}
}

if ( $is_memcache ) {
$message = 'Memcache';
}

// Test for Xcache object cache (https://plugins.svn.wordpress.org/xcache/trunk/object-cache.php).
} elseif ( $wp_object_cache instanceof \XCache_Object_Cache ) { // @phpstan-ignore-line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What specifically is being ignored for PHPStan on this line? The specific error identifier should be used here to prevent ignoring something unintentionally.

Suggested change
} elseif ( $wp_object_cache instanceof \XCache_Object_Cache ) { // @phpstan-ignore-line
} elseif ( $wp_object_cache instanceof XCache_Object_Cache ) { // @phpstan-ignore-line

$message = 'Xcache';

// Test for WinCache object cache (https://wordpress.org/extend/plugins/wincache-object-cache-backend/).
} elseif ( class_exists( 'WinCache_Object_Cache' ) ) {
$message = 'WinCache';

// Test for APC object cache (https://wordpress.org/extend/plugins/apc/).
} elseif ( class_exists( 'APC_Object_Cache' ) ) {
$message = 'APC';

// Test for WP Redis (https://wordpress.org/plugins/wp-redis/).
} elseif ( isset( $wp_object_cache->redis ) && $wp_object_cache->redis instanceof \Redis ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} elseif ( isset( $wp_object_cache->redis ) && $wp_object_cache->redis instanceof \Redis ) {
} elseif ( isset( $wp_object_cache->redis ) && $wp_object_cache->redis instanceof Redis ) {

$message = 'Redis';

// Test for Redis Object Cache (https://wordpress.org/plugins/redis-cache/).
} elseif ( method_exists( $wp_object_cache, 'redis_instance' ) && method_exists( $wp_object_cache, 'redis_status' ) ) {
$message = 'Redis';

// Test for Object Cache Pro (https://objectcache.pro/).
} elseif ( method_exists( $wp_object_cache, 'config' ) && method_exists( $wp_object_cache, 'connection' ) ) {
$message = 'Redis';

// Test for WP LCache Object cache (https://github.com/lcache/wp-lcache).
} elseif ( isset( $wp_object_cache->lcache ) && $wp_object_cache->lcache instanceof \LCache\Integrated ) { // @phpstan-ignore-line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What specifically is being ignored for PHPStan on this line? The specific error identifier should be used here to prevent ignoring something unintentionally.

Suggested change
} elseif ( isset( $wp_object_cache->lcache ) && $wp_object_cache->lcache instanceof \LCache\Integrated ) { // @phpstan-ignore-line
} elseif ( isset( $wp_object_cache->lcache ) && $wp_object_cache->lcache instanceof \Cache\Integrated ) { // @phpstan-ignore-line

$message = 'WP LCache';

} elseif ( function_exists( 'w3_instance' ) ) {
$config = w3_instance( 'W3_Config' );
$message = 'Unknown';

if ( $config->get_boolean( 'objectcache.enabled' ) ) {
$message = 'W3TC ' . $config->get_string( 'objectcache.engine' );
}
} else {
$message = 'Unknown';
}
} else {
$message = 'Disabled';
}

return $message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Hook callbacks used for Site Health Info.
*
* @package performance-lab
* @since n.e.x.t
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Adds Object Cache module to Site Health Info.
*
* @param array{object_cache: array{label: string,description: string,fields: array<string, array{label: string,value: string}>}} $info Site Health Info.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The object_cache key wouldn't exist yet in the $info being passed in.

*
* @return array{object_cache: array{label: string,description: string,fields: array<string, array{label: string,value: string}>}} Amended Info.
* @since n.e.x.t
Comment on lines +16 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typing can be made a bit more generic.

Suggested change
* @param array{object_cache: array{label: string,description: string,fields: array<string, array{label: string,value: string}>}} $info Site Health Info.
*
* @return array{object_cache: array{label: string,description: string,fields: array<string, array{label: string,value: string}>}} Amended Info.
* @since n.e.x.t
* @since n.e.x.t
*
* @param array<string, array{label: string, description: string, fields: array<string, array{label: string, value: string}>}> $info Site Health Info.
* @return array<string, array{label: string, description: string, fields: array<string, array{label: string, value: string}>}> Amended Info.
*

*/
function perflab_object_cache_supported_info( array $info ): array {
$info['object_cache'] = array(
'label' => __( 'Object Caching', 'performance-lab' ),
'description' => __( 'Shows which features object cache supports and if object caching is in use.', 'performance-lab' ),
'fields' => perflab_object_cache_supported_fields(),
);

return $info;
}

add_filter( 'debug_information', 'perflab_object_cache_supported_info', 10, 1 );
Loading