all我在尝试将memcache与php一起使用时遇到了一个问题我已经成功地安装了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自动加载
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的新手。
您需要先安装memcache扩展,然后才能使用php的memcache函数
请参阅PHP Memcached安装