创建文件内存映射,函数原型:
function swoole_mmap::open($file, $size = -1, $offset = 0);
file_put_contents
来初始化文件。stream
资源,可使用PHP提供的流式操作函数读写数据fread
、fgets
fwrite
、fputs
fclose
,底层会自动执行fflush
将数据同步到磁盘文件fflush
将内存中的数据写入到磁盘$file = __DIR__.'/data';
$size = 8192;
if (!is_file($file)) {
file_put_contents($file, str_repeat("\0", $size));
}
$fp = swoole\mmap::open($file, 8192);
fwrite($fp, "hello world\n");
fwrite($fp, "hello swoole\n");
fflush($fp);
fclose($fp);