Skip to content

Instantly share code, notes, and snippets.

@timnolte
Last active June 14, 2023 01:54
Show Gist options
  • Save timnolte/d295f24457698f65f00d301074494394 to your computer and use it in GitHub Desktop.
Save timnolte/d295f24457698f65f00d301074494394 to your computer and use it in GitHub Desktop.
WP Redis Site Health
<?php
/**
* Plugin Name: Redis Customizations
* Description: Custom functions, filters and action hooks for additional Redis support.
*
* @package MuPlugins
*/
/**
* Adds Redis Support Information to the WordPress Site Health Info.
*
* @link https://developer.wordpress.org/reference/hooks/debug_information/
*
* @param array $args The debug information to be added to the core information page. This is an associative multi-dimensional array, up to three levels deep. The topmost array holds the sections, keyed by section ID.
*
* @return array
*/
function wp_redis_add_health_check_info( $args ) {
$redis_info = wp_redis_get_info();
$defaults = array(
'redis_version' => 'Unknown',
'redis_mode' => 'Unknown',
'status' => 'Unknown',
'redis_host' => 'Unknown',
'redis_port' => 'Unknown',
'redis_database' => 'Unknown',
'uptime' => 'Unknown',
'used_memory' => 'Unknown',
'key_count' => 'Unknown',
'lifetime_hitrate' => 'Unknown',
'instantaneous_ops' => 'Unknown',
'maxclients' => 'Unknown',
'connected_clients' => 'Unknown',
);
if ( gettype( $redis_info ) === 'array' ) {
$redis_info = array_replace_recursive( $defaults, $redis_info );
}
$args['wp_redis'] = array(
'label' => 'Redis Info',
'fields' => array(
'version' => array(
'label' => 'Server version',
'value' => $redis_info['redis_version'],
),
'mode' => array(
'label' => 'Server mode',
'value' => $redis_info['redis_mode'],
),
'status' => array(
'label' => 'Status',
'value' => $redis_info['status'] . ' to ' . $redis_info['redis_host'] . ':' . $redis_info['redis_port'],
),
'database' => array(
'label' => 'Database',
'value' => $redis_info['redis_database'],
),
'uptime' => array(
'label' => 'Uptime',
'value' => $redis_info['uptime'],
),
'used_memory' => array(
'label' => 'Used memory',
'value' => $redis_info['used_memory'],
),
'key_count' => array(
'label' => 'Keys',
'value' => $redis_info['key_count'],
),
'hit_rate' => array(
'label' => 'Hit Ratio',
'value' => $redis_info['lifetime_hitrate'],
),
'inst_ops' => array(
'label' => 'Ops/Second',
'value' => $redis_info['instantaneous_ops'],
),
'max_clients' => array(
'label' => 'Max clients',
'value' => $redis_info['maxclients'],
),
'connected_clients' => array(
'label' => 'Connected clients',
'value' => $redis_info['connected_clients'],
),
),
);
return $args;
}
if ( defined( 'WP_REDIS_OBJECT_CACHE' ) ) {
add_filter( 'debug_information', 'wp_redis_add_health_check_info' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment