php Memcache类无法找到

时间:2015-12-29 02:21:32

标签: php memcached

所有 尝试在php中使用memcache时遇到问题 我hava成功安装了php和memcached扩展。 我写了一个memtest.php来测试它并且它成功了。这是测试。

<?php
$memcache = new Memcache;  
$memcache->connect('xxxxxx', 11211) or die ("Could not connect");
$memcache->set('key', 'test'); 
$get_value = $memcache->get('key'); 
echo $get_value;
?>

回声测试。

但是当我在我的php web服务器中使用它时,我失败了。 这是我的代码: 我写了一个memcacheTool.php

<?php
/**
 * memcache operate tool
 *
 * @author      wanghao
 * @date        2015-12-25  
 */
class memcacheTool {

    //var $memcache;
    //static $memcacheD = new Nemcached;

    private $cacheServer = URL_OF_MEMCACHED;
    private $cachePort = 11211;
    private $memcache;
    private static $_instance;

    private function __construct()
    {
        if (class_exists("Memcache"))
        {
            $this -> memcache = new Memcache();
        }

        $res = $this -> memcache -> connect($cacheServer, $cachePort);

        if( false == $res)
        {
            $this -> memcache = null;
        }
    }

    public static function getInstance()
    {
        if (is_null( self::$_instance))
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * set operation
     *
     * @param string    $key        
     * @param string    $value  
     */
    static function set($key, $value)
    {
        //error_log("memcache: set ".$key"value ".$value);
        if ( !trim($key) ) 
        {
            return false;
        }
        $memcache -> add($key,$value,MEMCACHE_COMPRESSED,0);

    }
    /**
      * get operation
      *
      * @param string   $key
      */
    static function get($key)
    {

        if ( !trim($key) ) {
            return null;
        }

        if (is_null($memcache -> get($key))){
            return null;
        } 
        $memValue = $memcache -> get($key);
        return $memValue;
    }

}

我在init.php中使用php autoload

function __autoload($object)
{
    require("". $object . ".php");

    if ( !class_exists($object) )
    {
        error_log("<IMPORTANT>Class loader failed to load class " . $object);
        include_once("app/http.php");
        include_once("app/poster.php");
        include_once("app/handle.php");
        include_once("app/loader.php");
        include_once("app/casserver.php");
        return;
    }

}

它向我显示找不到Memcache.php,所以我将其修改为

function __autoload($object)
{
    if ($object != "Memcache")
    {
        require("". $object . ".php");
    }
    if ( !class_exists($object) )
    {
        error_log("<IMPORTANT>Class loader failed to load class " . $object);
        include_once("app/http.php");
        include_once("app/poster.php");
        include_once("app/handle.php");
        include_once("app/loader.php");
        include_once("app/casserver.php");
        return;
    }

}

这次它告诉我

  

类加载器无法加载类Memcache

显然,无法找到Memcache类,但为什么测试php成功了?

我希望有人可以帮助我,因为我真的是一个更新的PHP。

1 个答案:

答案 0 :(得分:2)

在使用php

的memcache函数之前,需要先安装memcache扩展

请参考 PHP Memcached Installation

相关问题