crodas / concurrent-file-writer
一个用于使用 PHP 同时写入文件的微型库
v0.2.2
2017-07-26 13:06 UTC
Requires (Dev)
- crodas/phpunit-compat: ^1.7
- paragonie/random_compat: ^2.0
This package is auto-updated.
Last update: 2024-09-12 19:08:33 UTC
README
在 PHP 中写入文件,尤其是当可能有多进程同时写入时,是一项困难的工作。文件锁定很难正确实现,而且效率低下。
这是一个尽可能少使用文件锁定的微型库,用于并行写入文件。此库旨在改进 crodas/EzUpload
如何安装?
可以使用以下命令使用 composer 安装
composer require crodas/concurrent-file-writer
它如何工作?
当创建一个文件时,会创建一个包含一些元数据的占位符文件。每次 write
调用都会创建一个新的数据块,该数据块存储在一个临时文件夹中。当所有数据块都写入完成后,finalize
函数将锁定文件,并将所有数据块合并成最终的文件,并清理所有临时文件。
使用方法
use ConcurrentFileWriter\ConcurrentFileWriter; $x = new ConcurrentFileWriter('/tmp/file.txt'); // This will return TRUE the first time, FALSE if the file is already create and in process and an exception is the file exists and it seems to be finished. $x->create(); // This calls can happen in parallel (with the same initialization as above). $x->write( $offset, $content ); $x->write( $offset, $content ); // Only one process can finalize the file writing, it will block. // Finalize will return TRUE on success FALSE if another process is already doing it. $x->finalize();
待办事项
- 更多 PHPDocs
- 更多 PHPUnit 以覆盖所有边界情况
- 更多具有真实并行性的 PHPUnit(启动真实的 web 服务器或多个 PHP 进程?)