How to check if memcache or memcached is installed for PHP?

前端 未结 10 681
猫巷女王i
猫巷女王i 2020-12-23 13:47

How do I test if memcache or memcached (for PHP) is installed on my Apache webserver?

Memcache is a caching daemon designed especiall

相关标签:
10条回答
  • 2020-12-23 14:15

    Use this code to not only check if the memcache extension is enabled, but also whether the daemon is running and able to store and retrieve data successfully:

    <?php
    if (class_exists('Memcache')) {
        $server = 'localhost';
        if (!empty($_REQUEST['server'])) {
            $server = $_REQUEST['server'];
        }
        $memcache = new Memcache;
        $isMemcacheAvailable = @$memcache->connect($server);
    
        if ($isMemcacheAvailable) {
            $aData = $memcache->get('data');
            echo '<pre>';
            if ($aData) {
                echo '<h2>Data from Cache:</h2>';
                print_r($aData);
            } else {
                $aData = array(
                    'me' => 'you',
                    'us' => 'them',
                );
                echo '<h2>Fresh Data:</h2>';
                print_r($aData);
                $memcache->set('data', $aData, 0, 300);
            }
            $aData = $memcache->get('data');
            if ($aData) {
                echo '<h3>Memcache seem to be working fine!</h3>';
            } else {
                echo '<h3>Memcache DOES NOT seem to be working!</h3>';
            }
            echo '</pre>';
        }
    }
    if (!$isMemcacheAvailable) {
        echo 'Memcache not available';
    }
    
    ?>
    
    0 讨论(0)
  • 2020-12-23 14:16

    this is my test function that I use to check Memcache on the server

    <?php     
    public function test()
     {
        // memcache test - make sure you have memcache extension installed and the deamon is up and running
        $memcache = new Memcache;
        $memcache->connect('localhost', 11211) or die ("Could not connect");
    
        $version = $memcache->getVersion();
        echo "Server's version: ".$version."<br/>\n";
    
        $tmp_object = new stdClass;
        $tmp_object->str_attr = 'test';
        $tmp_object->int_attr = 123;
    
        $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
        echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
    
        $get_result = $memcache->get('key');
        echo "Data from the cache:<br/>\n";
    
        var_dump($get_result);
     }
    

    if you see something like this

        Server's version: 1.4.5_4_gaa7839e
        Store data in the cache (data will expire in 10 seconds)
        Data from the cache:
        object(stdClass)#3 (2) { ["str_attr"]=> string(4) "test" ["int_attr"]=> int(123) }
    

    it means that everything is okay

    Cheers!

    0 讨论(0)
  • 2020-12-23 14:21

    You can look at phpinfo() or check if any of the functions of memcache is available. Ultimately, check whether the Memcache class exists or not.

    e.g.

    if(class_exists('Memcache')){
      // Memcache is enabled.
    }
    
    0 讨论(0)
  • 2020-12-23 14:24

    Note that all of the class_exists, extensions_loaded, and function_exists only check the link between PHP and the memcache package.

    To actually check whether memcache is installed you must either:

    • know the OS platform and use shell commands to check whether memcache package is installed
    • or test whether memcache connection can be established on the expected port

    EDIT 2: OK, actually here's an easier complete solution:

    if (class_exists('Memcache')) {
        $memcache = new Memcache;
        $isMemcacheAvailable = @$memcache->connect('localhost');
    }
    if ($isMemcacheAvailable) {
        //...
    }
    

    Outdated code below


    EDIT: Actually you must force PHP to throw error on warnings first. Have a look at this SO question answer.

    You can then test the connection via:

    try {
        $memcache->connect('localhost');
    } catch (Exception $e) {
        // well it's not here
    }
    
    0 讨论(0)
提交回复
热议问题