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

前端 未结 10 719
猫巷女王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: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
    }
    

提交回复
热议问题