PHP:Pthread+;内存缓存故障

PHP:Pthread+;内存缓存故障,php,pthreads,memcached,pthreads-win32,Php,Pthreads,Memcached,Pthreads Win32,Memcache在pthread线程中似乎不起作用 我得到这个警告: Warning: Memcache::get(): No servers added to memcache connection in test.php on line 15 class Test extends Thread { protected $memcache; function __construct() { $this->memcache = New Mem

Memcache在pthread线程中似乎不起作用

我得到这个警告:

Warning: Memcache::get(): No servers added to memcache connection in test.php on line 15



   class Test extends Thread {

    protected $memcache;

    function __construct() {
        $this->memcache = New Memcache;
        $this->memcache->connect('localhost',11211 ) or die("Could not connect");
    }

    public function run() {
        $this->memcache->set('test', '125', MEMCACHE_COMPRESSED, 50);
        $val = $this->memcache->get('test');p
        echo "Value $val.";
        sleep(2);
    }

}

$threads = [];
for ($t = 0; $t < 5; $t++) {
    $threads[$t] = new Test();
    $threads[$t]->start();
}

for ($t = 0; $t < 5; $t++) {
    $threads[$t]->join();
}
警告:Memcache::get():第15行的test.php中没有向Memcache连接添加服务器
类测试扩展了线程{
受保护的$memcache;
函数_u构造(){
$this->memcache=新建memcache;
$this->memcache->connect('localhost',11211)或die(“无法连接”);
}
公共功能运行(){
$this->memcache->set('test','125',memcache\u COMPRESSED,50);
$val=$this->memcache->get('test');p
回声“价值$val.”;
睡眠(2);
}
}
$threads=[];
对于($t=0;$t<5;$t++){
$threads[$t]=新测试();
$threads[$t]->start();
}
对于($t=0;$t<5;$t++){
$threads[$t]->join();
}

由于memcache对象不准备在线程之间共享,因此必须为每个线程创建到memcached的连接,还必须确保不要将memcached连接写入线程对象上下文

以下任一代码示例都很好:

<?php
class Test extends Thread {

    public function run() {
        $memcache = new Memcache;

        if (!$memcache->connect('127.0.0.1',11211 ))
            throw new Exception("Could not connect");

        $memcache->set('test', '125', MEMCACHE_COMPRESSED, 50);
        $val = $memcache->get('test');
        echo "Value $val.\n";
    }

}

$threads = [];
for ($t = 0; $t < 5; $t++) {
    $threads[$t] = new Test();
    $threads[$t]->start();
}

for ($t = 0; $t < 5; $t++) {
    $threads[$t]->join();
}

<?php
class Test extends Thread {
    protected static $memcache;

    public function run() {
        self::$memcache = new Memcache;

        if (!self::$memcache->connect('127.0.0.1',11211 ))
            throw new Exception("Could not connect");

        self::$memcache->set('test', '125', MEMCACHE_COMPRESSED, 50);
        $val = self::$memcache->get('test');
        echo "Value $val.\n";
    }

}

$threads = [];
for ($t = 0; $t < 5; $t++) {
    $threads[$t] = new Test();
    $threads[$t]->start();
}

for ($t = 0; $t < 5; $t++) {
    $threads[$t]->join();
}