Manage files in memory and automatically synchronize with disk.
- Javascript
const fs = require('fs')
const { MemoryDisk } = require('memory-disk')
// The real filesystem path to be referenced. (only directory)
const path = './data'
// Whether to get all directory information in the directory
const recursive = true
const memoryDisk = new MemoryDisk(path, recursive)
const testPath = './data/text.txt';
// Save to memory. (After just 5 minutes, it automatically saves to the physical disk.)
memoryDisk.write(testPath, "Hello memory-disk world!")
// Read data. If it is not in memory, it is fetched from the real filesystem.
const data = memoryDisk.read(testPath)
// If there is no .write() or .read() for 5 minutes, it is automatically saved to the real filesystem and deleted from memory.
console.log('data:', data)
console.log('Is real filesystem exists? (before .saveDisk())', fs.existsSync(testPath));
memoryDisk.saveDisk(testPath)
console.log('Is real filesystem exists? (after .saveDisk())', fs.existsSync(testPath));
// Memory clear
memoryDisk.clear()
- Typescript
import fs from 'fs';
import { MemoryDisk } from "memory-disk";
// The real filesystem path to be referenced. (only directory)
const path = './data'
// Whether to get all directory information in the directory
const recursive = true
const memoryDisk = new MemoryDisk(path, recursive)
const testPath = './data/text.txt';
// Save to memory. (After just 5 minutes, it automatically saves to the physical disk.)
memoryDisk.write(testPath, "Hello memory-disk world!")
// Read data. If it is not in memory, it is fetched from the real filesystem.
const data = memoryDisk.read(testPath)
// If there is no .write() or .read() for 5 minutes, it is automatically saved to the real filesystem and deleted from memory.
console.log('data:', data)
console.log('Is real filesystem exists? (before .saveDisk())', fs.existsSync(testPath));
memoryDisk.saveDisk(testPath)
console.log('Is real filesystem exists? (after .saveDisk())', fs.existsSync(testPath));
// Memory clear
memoryDisk.clear()