• Resolved Joe

    (@joewa1980)


    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.
    

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi @joewa1980,

    Works like a charm. Thank you.

    Now that we have the stats displayed in our dashboard, is there any way you can provide a code snippet to hide the memcached stats located in the plugins section? Details

    Again, thank you!

    Thread Starter Joe

    (@joewa1980)

    Hi @generosus,

    I’m pleased you’ve found it helpful! In case they change the plugin behaviour it might just be easiest to use the uBlock Origin Chrome extension to hide that particular element for now.

    Joe

    Hey @joewa1980,

    Thanks for that suggestion. Unfortunately, as you know, that would only work for the browser using that extension. So, after a bit of testing, we came up with the following solution:

    Affected File: object-cache-4-everyone.php

    Line 277: Replace '<br/><br/>' . with '<br/>';

    Lines 278 to 281: Comment out each line with //

    Details: https://prnt.sc/p53AZOWSi1MV

    Results: Before: https://prnt.sc/s2e_PALAy2bn After: https://prnt.sc/VNygkpF4V35a

    The above will revert back to its original configuration next time the plugin is updated. So, hoping the plugin developer will incorporate what we shared in this topic into his next convenient plugin revision. It would look much cleaner.

    Cheers!

    Hey @joewa1980,

    One more thought: Is there any way you can add the plugin’s “Flush cache” feature to your widget or WP’s Admin Toolbar?

    Thank you!

    Thread Starter Joe

    (@joewa1980)

    Hi @generosus,

    A flush cache button has been added to the dashboard widget – enjoy! This snippet is independent of the Object Cache 4 Everyone plugin, it directly references your Memcached server.

    Hiding their plugin listing display elements would be something you need to discuss with @fpuenteonline.

    Now for the code:

    // 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']) . "<br /><br />";

    // Nonce field for security
    $nonce = wp_create_nonce('flush_memcached_cache');

    // Flush Cache Button
    echo '<form method="post" action="">';
    echo '<input type="hidden" name="flush_cache_nonce" value="' . $nonce . '" />';
    echo '<input type="submit" name="flush_cache" value="Flush Cache" class="button button-primary" />';
    echo '</form>';
    }

    // Handle the form submission to flush the cache
    add_action('admin_init', 'handle_flush_memcached_cache');

    function handle_flush_memcached_cache() {
    if (isset($_POST['flush_cache']) && check_admin_referer('flush_memcached_cache', 'flush_cache_nonce')) {
    $memcached = new Memcached();
    $memcached->addServer('127.0.0.1', 11211);
    $memcached->flush();
    add_action('admin_notices', 'flush_memcached_cache_notice');
    }
    }

    function flush_memcached_cache_notice() {
    echo '<div class="notice notice-success is-dismissible">';
    echo '<p>Memcached cache has been flushed successfully.</p>';
    echo '</div>';
    }

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

    Hi @joewa1980,

    Amazing! Thank you.

    With a few tweaks, the new Memcached widget looks perfect.

    Again, thank you. Let’s hope the developer can incorporate your work into his plugin.

    Cheers!

    Hi @joewa1980,

    One final challenge 🙂

    Is there any way to query the host server to find out what Memcached version is being used? If so, can you possibly update your widget to show that information? (e.g., “Server Memcached Version: 1.6.28″)

    This article might help you.

    Thank you!

    Thread Starter Joe

    (@joewa1980)

    Yes, sure, that’s below. This version also checks if a Memcached server is running. If not, it will show a not running message.

    // 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) || !isset($stats[$server])) {
    echo "Unable to fetch Memcached stats. Please ensure that the Memcached server is running on " . esc_html($server) . ".";
    return;
    }

    // Assuming $stats is not empty, extract the server stats
    $stats = $stats[$server];
    $version = $memcached->getVersion();
    $version = $version[$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>Memcached Server Version:</strong> " . esc_html($version) . "<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']) . "<br /><br />";

    // Nonce field for security
    $nonce = wp_create_nonce('flush_memcached_cache');

    // Flush Cache Button
    echo '<form method="post" action="">';
    echo '<input type="hidden" name="flush_cache_nonce" value="' . $nonce . '" />';
    echo '<input type="submit" name="flush_cache" value="Flush Cache" class="button button-primary" />';
    echo '</form>';
    }

    // Handle the form submission to flush the cache
    add_action('admin_init', 'handle_flush_memcached_cache');

    function handle_flush_memcached_cache() {
    if (isset($_POST['flush_cache']) && check_admin_referer('flush_memcached_cache', 'flush_cache_nonce')) {
    $memcached = new Memcached();
    $memcached->addServer('127.0.0.1', 11211);
    $memcached->flush();
    add_action('admin_notices', 'flush_memcached_cache_notice');
    }
    }

    function flush_memcached_cache_notice() {
    echo '<div class="notice notice-success is-dismissible">';
    echo '<p>Memcached cache has been flushed successfully.</p>';
    echo '</div>';
    }

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

    You’re a genius! Looks fabulous. Thank you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.