2

all I got a problem when try to use memcache with php I hava successfully install php and memcached extension. I write a memtest.php to test it and it success. Here is the test.

<?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;
?>

It echo test.

But when I used it in my php web server, I failed. Here is my code: I wrote a 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;
    }

}

And I use php autoload in init.php

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;
    }

}

It showed me that Memcache.php not found, so I modified it to

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;
    }

}

And this time it showed me

Class loader failed to load class Memcache

abviously,The Memcache class can not be found,but why the test php is success?

I wish someone could help me, cause I am really a newer with php.

wanghao qin
  • 145
  • 1
  • 10

1 Answers1

2

You need to install the memcache extension first before you can use the memcache functions of php

please refer to PHP Memcached Installation

Dominick Navarro
  • 752
  • 6
  • 20
  • THKS for answer,but I had already the extension, and I think it was installed correctly, cause I wrote a memtest.php script and it goes OK,and the code I wrote it in my question. – wanghao qin Dec 29 '15 at 07:00