javascript 我可以使用 Node.js 在“内存”中存储文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23831947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:38:41  来源:igfitidea点击:

Can I store a file in "memory" with Node.js?

javascriptnode.jsfile-io

提问by Shamoon

I'm doing some file manipulation work with node.js and many of the packages that I use require a "path" to be sent so that they can open the file, do some work, and so on.

我正在用 node.js 做一些文件操作工作,我使用的许多包都需要发送一个“路径”,以便他们可以打开文件,做一些工作,等等。

But I'm parsing millions of files and rather than actually store them on disk, I'd like to store them in memory. The contents of the files are all in my database and I'd hate to write them to disk, just to do my insanely awesome work on them.

但我正在解析数百万个文件,而不是实际将它们存储在磁盘上,我想将它们存储在内存中。文件的内容都在我的数据库中,我不想将它们写入磁盘,只是为了对它们进行疯狂的工作。

So is such a thing possible?

那么这样的事情可能吗?

采纳答案by CMP

If you are using a Linux system then you can merely create your own tmpfs (RAM) via the following nodejs code. This creates a tmpfs at /mnt/myramdisk as per the node code. The directory /mnt/myramdisk must be manually created beforehand obviously via mkdir /mnt/myramdisk.

如果您使用的是 Linux 系统,那么您只需通过以下 nodejs 代码创建自己的 tmpfs (RAM)。这会根据节点代码在 /mnt/myramdisk 处创建一个 tmpfs。目录 /mnt/myramdisk 显然必须事先通过 mkdir /mnt/myramdisk 手动创建。

var MountPoint='/mnt/myramdisk';
var TextFile='/MyTextFileInRAM.txt';
var RAM_Size='512m';

const fs = require('fs');

const { exec } = require('child_process');
exec("awk '{print }' /proc/mounts | grep "+MountPoint, (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    //console.log(err);
    console.log(MountPoint+' is Not Mounted yet. Im mounting it now:\n');
    NotMountedYetSoMountIt();
    return;
  }
  // the *entire* stdout and stderr (buffered)
  if(stdout)
    {
        console.log(MountPoint+' is Already Mounted');
        TextToWriteToFileOnTMPFS();
    } 
});

function NotMountedYetSoMountIt()
{
    const { exec } = require('child_process');
    exec('df -h && echo && mount -t tmpfs -o size='+RAM_Size+' tmpfs '+MountPoint+' && echo && df -h', (err, stdout, stderr) => {
      if (err) {
        // node couldn't execute the command
        return;
      }
    // the *entire* stdout and stderr (buffered)
    console.log(`stdout: ${stdout}`);
    TextToWriteToFileOnTMPFS();
    console.log(`stderr: ${stderr}`);
    });
}

function TextToWriteToFileOnTMPFS()
{

    let TextToWrite = 'Hello\n' + 
                      'world @'+CurrentTime();

    fs.writeFile(MountPoint+TextFile, TextToWrite, (err) => {
        // throws an error, you could also catch it here
        if (err) throw err;
        // success case, the file was saved
        console.log('saved!');
    });
}

function addZero(i) {
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}
function CurrentTime()
{
  var d = new Date();
  var h = addZero(d.getHours());
  var m = addZero(d.getMinutes());
  var s = addZero(d.getSeconds());
  return h + ":" + m + ":" + s;
}

Output:

输出:

root@box:/daemons#node tmpfs_test.js && cat /mnt/myramdisk/MyTextFileInRAM.txt
/mnt/myramdisk is Not Mounted yet. Im mounting it now:

stdout: Filesystem      Size  Used Avail Use% Mounted on
udev            7.8G     0  7.8G   0% /dev
tmpfs           1.6G  1.4M  1.6G   1% /run
/dev/sda2       938G  436G  454G  49% /
tmpfs           7.8G  449M  7.4G   6% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/loop1       90M   90M     0 100% /snap/core/8213
/dev/sda1       511M  6.1M  505M   2% /boot/efi
/dev/loop2       90M   90M     0 100% /snap/core/8268


Filesystem      Size  Used Avail Use% Mounted on
udev            7.8G     0  7.8G   0% /dev
tmpfs           1.6G  1.4M  1.6G   1% /run
/dev/sda2       938G  436G  454G  49% /
tmpfs           7.8G  449M  7.4G   6% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/loop1       90M   90M     0 100% /snap/core/8213
/dev/sda1       511M  6.1M  505M   2% /boot/efi
/dev/loop2       90M   90M     0 100% /snap/core/8268
tmpfs           512M     0  512M   0% /mnt/myramdisk

stderr:
saved!
Hello
world @23:09:15



root@box:/daemons# node tmpfs_test.js && cat /mnt/myramdisk/MyTextFileInRAM.txt
/mnt/myramdisk is Already Mounted
saved!
Hello
world @23:09:19

回答by user3210416

It looks like it's possible look at this article how to do it creating writable memory stream

看起来可以看这篇文章如何创建可写的内存流

回答by Vad

You can use memfs, which is an in-memory filesystem for Node.js.

您可以使用memfs,它是 Node.js 的内存文件系统。

回答by samvv

Because I have not found a single library that suited my needs, I have created diskette. It is a tiny library that tries to efficiently store data such as buffers and strings and make it streamable. I use it in my own projects and so far it works like a charm.

因为我还没有找到一个适合我需要的库,所以我创建了diskette。它是一个小型库,试图有效地存储缓冲区和字符串等数据并使其可流式传输。我在我自己的项目中使用它,到目前为止它就像一个魅力。

Please let me know if you found a bug or if there's anything more that can be added to it.

如果您发现了一个错误,或者是否还有其他可以添加的内容,请告诉我。

回答by omarwaleed

Maybe create a Bufferusing let myBuffer = Buffer.from(fetchedData)after fetching each file from the database then using myBuffer.toString()to change the file to a string and parse/manipulate it however you like

也许在从数据库中获取每个文件之后创建一个Bufferusinglet myBuffer = Buffer.from(fetchedData)然后使用myBuffer.toString()将文件更改为字符串并根据您的喜好解析/操作它