PHP:Pthread + Memcache问题

nullException

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();
}
乔·沃特金斯

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

以下任何一个代码示例都不错:

<?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();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHP Memcache使用“ true”获取值的问题

来自分类Dev

在Ubuntu Server上安装PHP pthread的问题

来自分类Dev

pthread RWlock问题

来自分类Dev

链表和pthread的问题

来自分类Dev

找不到php Memcache类别

来自分类Dev

PHP Memcache延长数据时间

来自分类Dev

使Memcache容器自动在Docker中启动的问题

来自分类Dev

pthread的C ++ 11链接问题

来自分类Dev

php-memcache中的memcache.dbpath究竟设置了什么?

来自分类Dev

关于 pthread_join() 和 pthread_detach() 的问题

来自分类Dev

PHP会话锁定并使用Memcache存储会话

来自分类Dev

PHP Memcache无法写入会话数据

来自分类Dev

memcache php存储超过1MB

来自分类Dev

无法使用终端卸载php-memcache

来自分类Dev

pthread调度和输出问题

来自分类Dev

php pthread,嵌入同步作品

来自分类Dev

PHP Memcached是否完全向后兼容Memcache?

来自分类Dev

如何在PHP中使用memcached(而非memcache)

来自分类Dev

为什么PHP Sessions Memcached可以工作,但Memcache不能工作?

来自分类Dev

在CentOS上使用PHP 7的Memcache扩展无法安装

来自分类Dev

如何在Windows上安装PHP 7扩展“ memcache”

来自分类Dev

PHP的Memcache扩展-XAMPP Windows无法正常工作

来自分类Dev

为什么PHP Sessions Memcached可以工作,但Memcache不能工作?

来自分类Dev

如何在PHP中连接到Memcache服务器?

来自分类Dev

PHP - memcache 随机失败 - 无法分配请求的地址 (99)

来自分类Dev

PHP pthread似乎不是多线程的

来自分类Dev

在pthread和curl之间进行PHP测试

来自分类Dev

带pthread的PHP中的Worker和Pool

来自分类Dev

在C中使用pthread调用php函数