DEV Community

Cover image for PHP HyperF + MariaDB -> Async / Parallel
Thiago Silva
Thiago Silva

Posted on • Updated on

PHP HyperF + MariaDB -> Async / Parallel

  • PHP: 8.3.7
  • PHP HyperF: 3.1.23
  • MariaDB: 11.3.2

HyperF - Project

System test for executing database queries in parallel or asynchronously.

Create - Project

composer create-project hyperf/hyperf-skeleton "project"
Enter fullscreen mode Exit fullscreen mode

Install - Watcher

composer require hyperf/watcher --dev
Enter fullscreen mode Exit fullscreen mode

Install - DB Library

composer require hyperf/database
composer require hyperf/db-connection
Enter fullscreen mode Exit fullscreen mode

Server - Start

cd project ;
php bin/hyperf.php server:watch ;
Enter fullscreen mode Exit fullscreen mode

MariaDB - Database

The data model is in meta tables, just for example.

Database - Project

CREATE DATABASE `project`;
Enter fullscreen mode Exit fullscreen mode

Table - Family

CREATE TABLE `family` (
    `id` tinyint(3) unsigned NOT NULL,
    `name` varchar(40) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

Table - Pets

CREATE TABLE `pet` (
    `id` tinyint(3) unsigned NOT NULL,
    `name` varchar(40) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

Table - Car

CREATE TABLE `car` (
    `id` tinyint(3) unsigned NOT NULL,
    `name` varchar(40) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

Table - Family Setting

CREATE TABLE `family_setting` (
    `id_family` tinyint(3) unsigned NOT NULL,
    `type` varchar(10) DEFAULT NULL,
    `key` tinyint(3) unsigned NOT NULL,
    KEY (`id_family`),
    KEY (`type`),
    KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

Populate - ALL

INSERT INTO `family` (`id`, `name`) VALUES(1, 'Family 1');
INSERT INTO `family` (`id`, `name`) VALUES(2, 'Family 2');
INSERT INTO `family` (`id`, `name`) VALUES(3, 'Family 3');

INSERT INTO `pet` (`id`, `name`) VALUES(1, 'Pet 1');
INSERT INTO `pet` (`id`, `name`) VALUES(2, 'Pet 2');
INSERT INTO `pet` (`id`, `name`) VALUES(3, 'Pet 3');

INSERT INTO `car` (`id`, `name`) VALUES(1, 'Car 1');
INSERT INTO `car` (`id`, `name`) VALUES(2, 'Car 2');
INSERT INTO `car` (`id`, `name`) VALUES(3, 'Car 3');

INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(1, 'pet', 1);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(1, 'pet', 2);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(1, 'car', 3);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(2, 'car', 1);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(2, 'car', 2);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(3, 'pet', 3);
Enter fullscreen mode Exit fullscreen mode

HyperF - APP

Database Config - MariaDB

DB_DRIVER=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=master
Enter fullscreen mode Exit fullscreen mode

path: /project/.env

APP - Router

Router::addRoute(['GET', 'POST'], '/family', 'App\Controller\ControllerFamily@data');
Router::addRoute(['GET', 'POST'], '/family/mount', 'App\Controller\ControllerFamily@mount');
Enter fullscreen mode Exit fullscreen mode

path: /project/config/routes.php

APP - Model - Family

namespace App\Model;

use Hyperf\DbConnection\Db;

class ModelFamily extends Model
{
    static public function data()
    {
        $family=Db::select('SELECT * FROM `family`;');
        return $family;
    }

    static public function mount()
    {
        $parallel=new \Hyperf\Coroutine\Parallel();

        $parallel->add(function(){
            $data=Db::select('SELECT * FROM `family`;');
            return $data;
        });

        $parallel->add(function(){
            $data=Db::select('SELECT * FROM `pet`;');
            sleep(2); // important add time test
            return $data;
        });

        $parallel->add(function(){
            $data=Db::select('SELECT * FROM `car`;');
            sleep(1); // important add time test
            return $data;
        });

        $parallel->add(function(){
            $data=Db::select('SELECT * FROM `family_setting`;');
            return $data;
        });

        $result=$parallel->wait();
        $result=[
            'family'=>$result[0],
            'pet'=>$result[1],
            'car'=>$result[2],
            'family_setting'=>$result[3],
        ];

        return $result;
    }
}
Enter fullscreen mode Exit fullscreen mode

path: /project/app/Model/ModelFamily.php

APP - Controller - Family

namespace App\Controller;

use \App\Model\ModelFamily;

class ControllerFamily
{
    public function data()
    {
        $family=ModelFamily::data();
        return $family;
    }
    public function mount()
    {
        $data=ModelFamily::mount();
        return $data;
    }
}
Enter fullscreen mode Exit fullscreen mode

path: /project/app/Controller/ControllerFamily.php

Execute

GET - Family

curl "http://127.0.0.1:9501/family" | jq .
Enter fullscreen mode Exit fullscreen mode
[
    {
        "id": 1,
        "name": "Family 1"
    },
    {
        "id": 2,
        "name": "Family 2"
    },
    {
        "id": 3,
        "name": "Family 3"
    }
]
Enter fullscreen mode Exit fullscreen mode

GET - Mount

curl "http://127.0.0.1:9501/family/mount" | jq .
Enter fullscreen mode Exit fullscreen mode
{
    "family": [
        {
            "id": 1,
            "name": "Family 1"
        },
        ...
    ],
    "pet": [
        {
            "id": 1,
            "name": "Pet 1"
        },
        ...
    ],
    "car": [
        {
            "id": 1,
            "name": "Car 1"
        },
        ...
    ],
    "family_setting": [
        {
            "id_family": 1,
            "type": "pet",
            "key": 1
        },
        {
            "id_family": 1,
            "type": "pet",
            "key": 2
        },
        ...
    ]
}
Enter fullscreen mode Exit fullscreen mode

https://github.com/thiagoeti/php-hyperf-mariadb-async-parrallel

Top comments (0)