Sha*_*oon 20 javascript file-io node.js
我正在使用node.js进行一些文件操作工作,我使用的许多软件包都需要发送"路径",以便他们可以打开文件,做一些工作,等等.
但我正在解析数百万个文件,而不是实际将它们存储在磁盘上,我想将它们存储在内存中.这些文件的内容都在我的数据库中,我讨厌将它们写入磁盘,只是为了对它们进行疯狂的工作.
这样的事情可能吗?
也许在从数据库中获取每个文件后创建一个Bufferusing ,然后使用将文件更改为字符串并根据需要解析/操作它let myBuffer = Buffer.from(fetchedData)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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13079 次 |
| 最近记录: |