Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP HyperF -> Bertindih dan Konkurensi

PHP HyperF -> Bertindih dan Konkurensi

王林
王林asal
2024-07-17 03:01:201049semak imbas

PHP HyperF -> Bertindih dan Konkurensi Bertindih dan Konkurensi" />

  • PHP: 8.3.7
  • HyperF PHP: 3.1.23

HyperF - Projek

Amat penting bahawa proses HyperF tidak menimpa data pembolehubah yang dikongsi. Ini boleh berlaku dengan lebihan beban.

Cipta - Projek

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

Pasang - Pemerhati

composer require hyperf/watcher --dev

Pasang - UUID

composer require ramsey/uuid

Pelayan - Mula

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

HyperF - APP

APP - Penghala

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');

laluan: /project/config/routes.php

APP - Pengawal - Bertindih

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

}

laluan: /project/app/Controller/ControllerOverlapping.php

APP - Pengawal - Kawalan

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

}

laluan: /project/app/Controller/ControllerConcurrency.php

Laksanakan

GET - Concurrency

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'

GET - Kawalan

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

Atas ialah kandungan terperinci PHP HyperF -> Bertindih dan Konkurensi. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn