イベント駆動型プログラミングは、手続き的コーディングに慣れているPHP開発者にとってユニークな課題です。 PHPの手続き上の性質では、イベントはしばしば単純な機能呼び出しに要約され、固有の非同期挙動はありません。 すべてのコード実行はブロックされています ただし、JavaScriptのような言語は、イベントループが中心コンポーネントとしての可能性を示しています。 この洞察により、開発者はイベントループと非同期機能をPHP HTTPサーバーに統合するようになりました。この記事では、ICICLEライブラリを活用する高性能PHP HTTPサーバーの構築と、最適化された静的ファイルのサービングのためにApacheと統合することを示しています。 サンプルコードは、
https://www.php.cn/link/ac2ef27277777777777777777777777d80c1
を取り付けることから始めます 単純なdicicle httpサーバーの例:
Leaguerouteを使用した高度なルーティング
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*) http://%{SERVER_NAME}:9001%{REQUEST_URI} [P]
より堅牢なルーティングについては、leagueroute:mod_rewrite
を統合します
拡張:
aサンプル
:composer require icicleio/http
// server.php require __DIR__ . "/vendor/autoload.php"; use Icicle\Http\Message\RequestInterface; use Icicle\Http\Message\Response; use Icicle\Http\Server\Server; use Icicle\Loop; use Icicle\Socket\Client\ClientInterface; $server = new Server(function (RequestInterface $request, ClientInterface $client) { $response = (new Response(200))->withHeader("Content-Type", "text/plain"); yield $response->getBody()->end("hello world"); yield $response; }); $server->listen(9001); Loop\run();
複雑なビューの場合は、leagueplates:
を使用しますテンプレートを実装します(
composer require league/route
の例のスニペット、および更新されたserver.php
は簡潔に省略されていますが、元の例の構造に従ってください)。
// server.php // ... (previous imports) ... use League\Route\Http\Exception\MethodNotAllowedException; use League\Route\Http\Exception\NotFoundException; use League\Route\RouteCollection; use League\Route\Strategy\UriStrategy; // ... (Server creation) ... $router = new RouteCollection(); $router->setStrategy(new UriStrategy()); require __DIR__ . "/routes.php"; $dispatcher = $router->getDispatcher(); try { $result = $dispatcher->dispatch($request->getMethod(), $request->getRequestTarget()); $status = 200; $content = $result->getContent(); } catch (NotFoundException $e) { $status = 404; $content = "not found"; } catch (MethodNotAllowedException $e) { $status = 405; $content = "method not allowed"; } // ... (Response creation and sending) ...
routes.php
元の記事には、かなりの数の同時リクエストを処理するサーバーの機能を示すパフォーマンスベンチマークが含まれています。 これらのベンチマークは、実行された特定のハードウェアと条件のコンテキストで考慮する必要があります。 重要なポイントは、Icicleの非同期モデルで高性能の可能性です。 この記事は、実験とコミュニティの議論を奨励することで締めくくります。 ICICLE著者が提供する更新されたベンチマークも含まれています。 FAQセクションでは、サーバー開発にiticleを使用することのさまざまな側面をさらに明確にします。
以上がIcicleで数分で超高速PHPサーバーを構築しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。