首页  >  文章  >  后端开发  >  PHP HyperF -> 重叠和并发

PHP HyperF -> 重叠和并发

王林
王林原创
2024-07-17 03:01:201049浏览

PHP HyperF -> 重叠和并发 重叠和并发" />

  • PHP:8.3.7
  • PHP HyperF:3.1.23

HyperF - 项目

HyperF 进程不要覆盖共享变量数据非常重要。过载时可能会发生这种情况。

创建 - 项目

composer create-project hyperf/hyperf-skeleton "project"

安装-观察者

composer require hyperf/watcher --dev

安装-UUID

composer require ramsey/uuid

服务器 - 启动

cd project ;
php bin/hyperf.php server:watch ;

HyperF-APP

APP-路由器

Router::addRoute(['GET', 'POST'], '/overlapping/data', 'App\Controller\ControllerOverlapping@data');
Router::addRoute(['GET', 'POST'], '/overlapping/value-a', 'App\Controller\ControllerOverlapping@value_a');
Router::addRoute(['GET', 'POST'], '/overlapping/value-b', 'App\Controller\ControllerOverlapping@value_b');
Router::addRoute(['GET', 'POST'], '/overlapping/value-c', 'App\Controller\ControllerOverlapping@value_c');

Router::addRoute(['GET', 'POST'], '/concurrency/data', 'App\Controller\ControllerConcurrency@data');
Router::addRoute(['GET', 'POST'], '/concurrency/value-a', 'App\Controller\ControllerConcurrency@value_a');
Router::addRoute(['GET', 'POST'], '/concurrency/value-b', 'App\Controller\ControllerConcurrency@value_b');
Router::addRoute(['GET', 'POST'], '/concurrency/value-c', 'App\Controller\ControllerConcurrency@value_c');

路径:/project/config/routes.php

APP - 控制器 - 重叠

namespace App\Controller;

class ControllerOverlapping
{
    public $data;

    public function __construct()
    {
        $this->data='';
    }

    public function data()
    {
        return $this->data;
    }

    public function value_a()
    {
        $this->data='Value A';
        return $this->data;
    }

    public function value_b()
    {
        $this->data='Value B';
        return $this->data;
    }

    public function value_C()
    {
        $this->data='Value C';
        return $this->data;
    }

}

路径:/project/app/Controller/ControllerOverlapping.php

APP-控制器-控制

namespace App\Controller;

use Ramsey\Uuid\Uuid;

class ControllerConcurrency
{
    public $data;

    public function __construct()
    {
        $this->data=array();
    }

    public function start()
    {
        $key=Uuid::uuid4()->toString();
        return $key;
    }

    public function end($key)
    {
        $data=$this->data[$key];
        unset($this->data[$key]);
        return $data;
    }

    public function data()
    {
        return $this->data;
    }

    public function value_a()
    {
        $key=$this->start();
        $this->data[$key]='Value A';
        $data=$this->end($key);
        return $data;
    }

    public function value_b()
    {
        $key=Uuid::uuid4()->toString();
        $this->data[$key]='Value B';
        $data=$this->end($key);
        return $data;
    }

    public function value_C()
    {
        $key=Uuid::uuid4()->toString();
        $this->data[$key]='Value C';
        $data=$this->end($key);
        return $data;
    }

}

路径:/project/app/Controller/ControllerConcurrency.php

执行

GET——并发

curl "http://127.0.0.1:9501/overlapping/data"
Result: ''

curl "http://127.0.0.1:9501/overlapping/value-a"
Result: 'Value A'

curl "http://127.0.0.1:9501/overlapping/data"
Result: 'Value A'

curl "http://127.0.0.1:9501/overlapping/value-b"
Result: 'Value B'

curl "http://127.0.0.1:9501/overlapping/data"
Result: 'Value B'

获取-控制

curl "http://127.0.0.1:9501/concurrency/data"
Result: ''

curl "http://127.0.0.1:9501/concurrency/value-a"
Result: 'Value A'

curl "http://127.0.0.1:9501/concurrency/data"
Result: ''

curl "http://127.0.0.1:9501/concurrency/value-b"
Result: 'Value B'

curl "http://127.0.0.1:9501/concurrency/data"
Result: ''

curl "http://127.0.0.1:9501/concurrency/value-a" && \
curl "http://127.0.0.1:9501/concurrency/value-a" && \
curl "http://127.0.0.1:9501/concurrency/value-c" ;
Result: 'Value A', 'Value B', 'Value C'

curl "http://127.0.0.1:9501/concurrency/data"
Result: ''

https://github.com/thiagoeti/php-hyperf-overlapping-concurrency

以上是PHP HyperF -> 重叠和并发的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn