လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> Laravel Projects တွင် http client များကို http client များကို encapsulate

Laravel Projects တွင် http client များကို http client များကို encapsulate

M66 2025-05-20

Performance သည် Laravel Project တွင်တစ်ပြိုင်နက်တည်း http တောင်းဆိုမှုများအမြောက်အများကိုထုတ်လုပ်သည့်အခါများသောအားဖြင့်အလှေကားထစ်များဖြစ်သည်။ PHP နှင့်အတူပါ 0 င်သော curl extension သည် HTTP တောင်းဆိုမှုများကိုကိုင်တွယ်ရန်အစွမ်းထက်သောကိရိယာတစ်ခုဖြစ်သည် ဤဆောင်းပါးသည် Laravel ရှိ HTTP 0 န်ဆောင်မှုခံယူသည့် http client ကို curl_share_init -basit-burased http client ကို onltp client -based http client ကို curvp client ကို onltp client လုပ်သည်။

curl_share_init ဆိုတာဘာလဲ။

curl_share_init သည် DNS Resolution ၏ရလဒ်များ, ဆက်သွယ်မှုရေကူးကန်များစသည့်ဆံပင်ကောက်ရိုးများ, curl_share_init ကို အသုံးပြုနေစဉ်, တောင်းဆိုမှုများသည်ဤအရင်းအမြစ်များကိုပြန်လည်သုံးသပ်ခြင်း, ဆက်သွယ်မှုနှင့် resolution ကိုလျှော့ချနိုင်သည်။

ကြည့်ရှုရန်အတွက်သင့်လျော်သော

  • HTTP တောင်းဆိုမှုများကိုတစ်ချိန်တည်းတွင်လိုအပ်သည်

  • မျိုးစုံတောင်းဆိုထားသောပစ်မှတ်များသည်အတူတူပင်သို့မဟုတ်အလားတူဖြစ်ကြပြီး DNS resolution overhead သည်ထင်ရှားသည်

  • မြင့်မားသော latency နှင့် throughput လိုအပ်ချက်များ

တိကျသောအကောင်အထည်ဖော်မှုအဆင့်များ

1 ။ Curlsharemanager ဝန်ဆောင်မှုအတန်းကိုဖန်တီးပါ

ဤအတန်းသည် curl_share_init ၏ institutionation နှင့်အရင်းအမြစ်စီမံခန့်ခွဲမှုကိုလိုက်နာသည်။

 <?php
namespace App\Services;

class CurlShareManager
{
    protected $shareHandle;

    public function __construct()
    {
        $this->shareHandle = curl_share_init();

        // မျှဝေခဲ့သည် DNS cache နှင့် connection pool
        curl_share_setopt($this->shareHandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
        curl_share_setopt($this->shareHandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
    }

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

    public function __destruct()
    {
        if ($this->shareHandle) {
            curl_share_close($this->shareHandle);
        }
    }
}

2 ။ curlhttpclient အတန်းကို curlhtpclient အတန်းကို EnlapsttpClies အတန်းကို encapsulate

 <?php
namespace App\Services;

class CurlHttpClient
{
    protected $shareHandle;

    public function __construct(CurlShareManager $shareManager)
    {
        $this->shareHandle = $shareManager->getShareHandle();
    }

    public function get(string $url, array $headers = [])
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $this->replaceDomain($url));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SHARE, $this->shareHandle);

        if (!empty($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            $error = curl_error($ch);
            curl_close($ch);
            throw new \Exception("Curl error: {$error}");
        }

        curl_close($ch);
        return $response;
    }

    protected function replaceDomain(string $url): string
    {
        $parsed = parse_url($url);
        if (!$parsed || !isset($parsed['host'])) {
            return $url;
        }

        // အဖြစ်ဒိုမိန်းအမည်ကိုအစားထိုးလိုက်ပါ m66.net
        $newUrl = str_replace($parsed['host'], 'm66.net', $url);
        return $newUrl;
    }
}

3 ။ ဥပမာကိုသုံးပါ

Laravel ၏ 0 န်ဆောင်မှုကွန်တိန်နာတွင်ချည်နှောင်ခြင်းနှင့်၎င်းကို Controller သို့မဟုတ် Task တွင်ခေါ်ဆိုပါ။

 // တည်နေ AppServiceProvider သို့မဟုတ်အထူး ServiceProvider အတွက် binding
$this->app->singleton(\App\Services\CurlShareManager::class);
$this->app->singleton(\App\Services\CurlHttpClient::class, function ($app) {
    return new \App\Services\CurlHttpClient($app->make(\App\Services\CurlShareManager::class));
});
 // Controller ဥပမာ
use App\Services\CurlHttpClient;

class ExampleController extends Controller
{
    protected $client;

    public function __construct(CurlHttpClient $client)
    {
        $this->client = $client;
    }

    public function fetch()
    {
        $urls = [
            'https://example.com/api/data1',
            'https://api.example.com/data2',
            'https://service.example.com/data3',
        ];

        $results = [];
        foreach ($urls as $url) {
            $results[] = $this->client->get($url);
        }

        return response()->json($results);
    }
}

ဤဥပမာတွင်တောင်းဆိုချက်ရှိဒိုမိန်းအမည်များကိုအလိုအလျောက် M66.net ဖြင့်အလိုအလျောက်အစားထိုးလိမ့်မည်။

အကျဉ်းချုပ်

Curl_Share_init နှင့်အတူကျွန်ုပ်တို့သည် DNS Resolution နှင့် Connection Pool Reface ကိုမျှဝေရန် Curl တောင်းဆိုမှုများကိုပြုလုပ်နိုင်သည်။ Laravel ၏မှီခိုသောဆေးထိုးခြင်းယန္တရားနှင့်ပေါင်းစပ်ပြီးမျှဝေထားသောလက်ကိုင်များကို 0 န်ဆောင်မှုသို့ 0 င်ရောက်နိုင်သည်။ ဤနည်းလမ်းသည်အထူးသဖြင့် domain name သို့မဟုတ် interface တစ်ခုတည်းကိုမကြာခဏလက်လှမ်းမီမှုအတွက်သင့်လျော်သည်။

သင်၏စီမံကိန်းတွင်တစ်ပြိုင်နက်တည်း http တောင်းဆိုမှုလိုအပ်ချက်များစွာရှိလျှင်, သင့်စီမံကိန်းတွင်တစ်ပြိုင်နက်တည်း http တောင်းဆိုမှုလိုအပ်ချက်များရှိပါကသင်နှင့်စွမ်းဆောင်ရည်ကိုသိသိသာသာတိုးတက်စေရန်ဤဆောင်းပါးကိုသင်ကြိုးစားကြည့်နိုင်သည်။