【发布时间】:2014-09-24 15:13:12
【问题描述】:
我正在使用带有 IRON IO 队列的 flysystem,我正在尝试运行一个数据库查询,该查询将获取约 180 万条记录,同时一次执行 5000 条记录。这是我收到的错误消息,文件大小超过 50 MB:
PHP Fatal error: Allowed memory size of ########## bytes exhausted
以下是我想采取的步骤:
1) 获取数据
2) 将其转换为适当的 CSV 字符串(即implode(',', $dataArray) . "\r\n")
3) 从服务器获取文件(本例为 S3)
4) 读取该文件的内容并将这个新字符串附加到它,然后将该内容重新写入 S3 文件
以下是我所拥有的代码的简要介绍:
public function fire($job, $data)
{
// First set the headers and write the initial file to server
$this->filesystem->write($this->filename, implode(',', $this->setHeaders($parameters)) . "\r\n", [
'visibility' => 'public',
'mimetype' => 'text/csv',
]);
// Loop to get new sets of data
$offset = 0;
while ($this->exportResult) {
$this->exportResult = $this->getData($parameters, $offset);
if ($this->exportResult) {
$this->writeToFile($this->exportResult);
$offset += 5000;
}
}
}
private function writeToFile($contentToBeAdded = '')
{
$content = $this->filesystem->read($this->filename);
// Append new data
$content .= $contentToBeAdded;
$this->filesystem->update($this->filename, $content, [
'visibility' => 'public'
]);
}
我假设这不是最有效的?我要离开这些文档: PHPLeague Flysystem
如果有人能指出我更合适的方向,那就太棒了!
【问题讨论】:
-
有没有办法使用 Flysystem 附加到文件中?否则我看不出你怎么能避免使用这么多内存。
-
@andy 不,它看起来不像。它们具有流功能,但我认为这与您必须读取文件以更新其内容的方式相同。