如何在 Laravel 中使用分块传输编码?
How can I use chunked transfer encoding in Laravel?
美好的一天。我想知道我是否可以在 API 响应中使用分块传输编码(进一步的 CTE)?我在数据库中有大量数据,我需要在一个请求中将其传输给客户端。我已经阅读了很多关于 CTE 机制的文章,但不幸的是我找不到如何实现它。
有一件重要的事情要提:没有分页。它应该是一个自治系统,returns 数据返回到客户端端点,而不是网页。
正如我提到的,数据存储在数据库中。唯一的问题是如何将数据分成段(块)并在一个 API 响应(一个接一个)中发送。
谢谢。
我想您可能对查询生成器上的块方法感兴趣。
Chunking Results
If you need to work with thousands of database records, consider using
the chunk method. This method retrieves a small chunk of the results
at a time and feeds each chunk into a Closure for processing. This
method is very useful for writing Artisan commands that process
thousands of records. For example, let's work with the entire users
table in chunks of 100 records at a time:
$response = new \Symfony\Component\HttpFoundation\StreamedResponse(function() {
$handle = fopen('php://output', 'w');
DB::table('users')->orderBy('id')->chunk(100, function ($users) use($handle) {
foreach ($users as $user) {
fputs($handle, json_encode($user));
}
});
fclose($handle);
});
return $response;
进一步阅读:https://laravel.com/docs/master/queries#chunking-results
分块传输编码
据我所知 Laravel does this by default 如果您 return 回复 JSON。
更新:
我的解决方案不必要地错综复杂。我已经删除了 StreamedResponse
因为真的不需要它。请参阅下面更新的示例。
$json_response = collect();
DB::table('users')->orderBy('id')->chunk(100, function ($users) use($json_response) {
foreach ($users as $user) {
$json_response->push($user);
}
});
return $json_response->toJson();
在我的具体案例中,我使用以下代码(示例)解决了我的问题:
use Symfony\Component\HttpFoundation\StreamedResponse;
$response = new StreamedResponse();
$response->setCallback(function () {
var_dump('Hello World');
flush();
sleep(2);
var_dump('Hello World');
flush();
});
$response->send();
美好的一天。我想知道我是否可以在 API 响应中使用分块传输编码(进一步的 CTE)?我在数据库中有大量数据,我需要在一个请求中将其传输给客户端。我已经阅读了很多关于 CTE 机制的文章,但不幸的是我找不到如何实现它。
有一件重要的事情要提:没有分页。它应该是一个自治系统,returns 数据返回到客户端端点,而不是网页。
正如我提到的,数据存储在数据库中。唯一的问题是如何将数据分成段(块)并在一个 API 响应(一个接一个)中发送。
谢谢。
我想您可能对查询生成器上的块方法感兴趣。
Chunking Results
If you need to work with thousands of database records, consider using the chunk method. This method retrieves a small chunk of the results at a time and feeds each chunk into a Closure for processing. This method is very useful for writing Artisan commands that process thousands of records. For example, let's work with the entire users table in chunks of 100 records at a time:
$response = new \Symfony\Component\HttpFoundation\StreamedResponse(function() {
$handle = fopen('php://output', 'w');
DB::table('users')->orderBy('id')->chunk(100, function ($users) use($handle) {
foreach ($users as $user) {
fputs($handle, json_encode($user));
}
});
fclose($handle);
});
return $response;
进一步阅读:https://laravel.com/docs/master/queries#chunking-results
分块传输编码
据我所知 Laravel does this by default 如果您 return 回复 JSON。
更新:
我的解决方案不必要地错综复杂。我已经删除了 StreamedResponse
因为真的不需要它。请参阅下面更新的示例。
$json_response = collect();
DB::table('users')->orderBy('id')->chunk(100, function ($users) use($json_response) {
foreach ($users as $user) {
$json_response->push($user);
}
});
return $json_response->toJson();
在我的具体案例中,我使用以下代码(示例)解决了我的问题:
use Symfony\Component\HttpFoundation\StreamedResponse;
$response = new StreamedResponse();
$response->setCallback(function () {
var_dump('Hello World');
flush();
sleep(2);
var_dump('Hello World');
flush();
});
$response->send();