What is read-cache?
The `read-cache` npm package is designed to read and cache the contents of files in memory. This can significantly improve performance for applications that need to read the same files multiple times, as it avoids unnecessary disk I/O after the initial read. The cache is automatically updated if the file changes, ensuring that the application always has access to the latest version of the file contents.
What are read-cache's main functionalities?
Reading and caching file contents
This feature allows you to read the contents of a file and cache it for future reads. The function returns a promise that resolves with the file's contents.
const readCache = require('read-cache');
readCache('path/to/file.txt').then(contents => {
console.log(contents);
});
Synchronously reading and caching file contents
Similar to the asynchronous version, this feature allows for synchronous reading and caching of file contents, useful in scenarios where asynchronous operations are not possible or desired.
const readCache = require('read-cache');
const contents = readCache.sync('path/to/file.txt');
console.log(contents);
Other packages similar to read-cache
node-cache
While `node-cache` is a general-purpose in-memory cache module for Node.js and does not specifically target file content caching, it can be used to manually implement a similar functionality by reading file contents and storing them in the cache. Compared to `read-cache`, `node-cache` offers more flexibility in what can be cached but requires more setup for caching file contents.
memory-cache
This package provides a simple in-memory cache similar to `node-cache`. It can be used to cache any JavaScript object, including file contents. Like `node-cache`, it offers more general caching capabilities but lacks the file-specific optimizations and automatic updates provided by `read-cache`.
read-cache
Reads and caches the entire contents of a file until it is modified.
Install
$ npm i read-cache
Usage
var readCache = require('read-cache');
readCache('foo.js').then(function (contents) {
console.log(contents);
});
API
readCache(path[, encoding])
Returns a promise that resolves with the file's contents.
readCache.sync(path[, encoding])
Returns the content of the file.
readCache.get(path[, encoding])
Returns the content of cached file or null.
readCache.clear()
Clears the contents of the cache.
License
MIT © Bogdan Chadkin