簡體   English   中英

我可以使用 Node.js 在“內存”中存儲文件嗎?

[英]Can I store a file in “memory” with Node.js?

我正在用 node.js 做一些文件操作工作,我使用的許多包都需要發送一個“路徑”,以便他們可以打開文件,做一些工作,等等。

但我正在解析數百萬個文件,而不是實際將它們存儲在磁盤上,我想將它們存儲在內存中。 文件的內容都在我的數據庫中,我不想將它們寫入磁盤,只是為了對它們進行瘋狂的工作。

那么這樣的事情可能嗎?

看起來可以看這篇文章如何創建可寫的內存流

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

因為我還沒有找到一個適合我需要的庫,所以我創建了diskette 它是一個小型庫,試圖有效地存儲緩沖區和字符串等數據並使其可流式傳輸。 我在我自己的項目中使用它,到目前為止它就像一個魅力。

如果您發現了一個錯誤,或者是否還有其他可以添加的內容,請告訴我。

也許在從數據庫中獲取每個文件之后使用let myBuffer = Buffer.from(fetchedData)創建一個Buffer然后使用myBuffer.toString()將文件更改為字符串並根據需要解析/操作它

如果您使用的是 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 $2}' /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;
}

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM