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

前端 未结 10 678
猫巷女王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 13:58

    why not use the extension_loaded() function?

    0 讨论(0)
  • 2020-12-23 13:59

    I combined, minified and extended (some more checks) the answers from @Bijay Rungta and @J.C. Inacio

    <?php
    if(!extension_loaded('Memcache'))
    {
        die("Memcache extension is not loaded");
    }
    
    if (!class_exists('Memcache')) 
    {
        die('Memcache class not available');
    }
    
    $memcacheObj = new Memcache;
    if(!$memcacheObj)
    {
        die('Could not create memcache object');
    }
    
    if (!$memcacheObj->connect('localhost')) 
    {
        die('Could not connect to memcache server');
    }
    
    // testdata to store in memcache
    $testData = array(
        'the' => 'cake',
        'is' => 'a lie',
    );
    
    // set data (if not present)
    $aData = $memcacheObj->get('data');
    if (!$aData) 
    {
        if(!$memcacheObj->set('data', $testData, 0, 300))
        {
            die('Memcache could not set the data');
        }
    }
    
    // try to fetch data
    $aData = $memcacheObj->get('data');
    if (!$aData) 
    {
        die('Memcache is not responding with data');
    }
    
    if($aData !== $testData)
    {
        die('Memcache is responding but with wrong data');
    }
    
    die('Memcache is working fine');
    
    0 讨论(0)
  • 2020-12-23 14:01

    The best approach in this case is to use extension_loaded() or function_exists() they are equally as fast.

    You can see evidence here:

    https://github.com/dragoonis/ppi-framework/blob/master/Cache/Memcached.php#L140

    Bear in mind that some PHP extensions such as APC have php.ini settings that can disable them even though the extension may be loaded. Here is an example of how to check against that also:

    https://github.com/dragoonis/ppi-framework/blob/master/Cache/Apc.php#L79

    Hope this helps.

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

    I know this is an old thread, but there's another way that I've found useful for any extension.

    Run

    php -m | grep <module_name>

    In this particular case:

    php -m | grep memcache

    If you want to list all PHP modules then:

    php -m

    Depending on your system you'd get an output similar to this:

    [PHP Modules]
    apc
    bcmath
    bz2
    ... lots of other modules ...
    mbstring
    memcache
     ... and still more modules ...
    zip
    zlib
    
    [Zend Modules]
    

    You can see that memcache is in this list.

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

    You have several options ;)

    $memcache_enabled = class_exists('Memcache');
    $memcache_enabled = extension_loaded('memcache');
    $memcache_enabled = function_exists('memcache_connect');
    
    0 讨论(0)
  • 2020-12-23 14:06

    It may be relevant to see if it's running in PHP via command line as well-

    <path-to-php-binary>php -i | grep memcache
    
    0 讨论(0)
提交回复
热议问题