Top tip: display Memcached server data in admin dashboard

[ad_1]

To display your Memcached server stats in your WordPress dashboard, you can add this code as a snippets using functions.php or Code Snippets plugin.

Adjust 127.0.0.1:11211 to reflect your actual server.

Enjoy!

// Hook into the WordPress dashboard setup action to add our widget
add_action('wp_dashboard_setup', 'add_memcached_stats_dashboard_widget');

function add_memcached_stats_dashboard_widget() {
    wp_add_dashboard_widget('memcached_stats_dashboard_widget', 'Memcached Stats', 'display_memcached_stats_dashboard_widget');
}

function display_memcached_stats_dashboard_widget() {
    // Attempt to connect to your Memcached server
    $memcached = new Memcached();
    $memcached->addServer('127.0.0.1', 11211);
    $stats = $memcached->getStats();
    $server="127.0.0.1:11211";

    // Check if we got stats back
    if (empty($stats)) {
        echo "Unable to fetch Memcached stats.";
        return;
    }

    // Assuming $stats is not empty, extract the server stats
    $stats = $stats[$server];

    // Calculate Cache Hit Ratio
    $hitRatio = ($stats['get_hits'] / ($stats['get_hits'] + $stats['get_misses'])) * 100;

    // Calculate Uptime
    $uptime = gmdate("j \D H \H i \M s \S", $stats['uptime']);

    // Display the stats
    echo "<strong>Memcached Server running:</strong> " . esc_html($server) . "<br />";
    echo "<strong>Cache Hit Ratio:</strong> " . number_format($hitRatio, 2) . "%<br />";
    echo "<strong>Uptime:</strong> " . esc_html($uptime) . "<br />";
    echo "<strong>Current Unique Items / Total Items:</strong> " . number_format($stats['curr_items']) . " / " . number_format($stats['total_items']);
}

// Ensure this PHP code is placed in your theme's functions.php file or a custom plugin.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer