Registry / storage / read-cache

read-cache

JSON →
library1.0.0jsnpmunverified

Minimalistic library (v1.0.0, stable) that reads a file into memory and caches its contents, invalidating the cache when the file's modification time changes. Returns promises (async) and offers sync variants. No dependencies, ESM/CJS compatible. Key differentiator: automatic cache invalidation via stat() on every read, avoiding stale data without explicit TTL or watchers. Suitable for small config files or one-shot reads where performance is not critical.

npm install read-cache
INSTALL
IMPORT
SIG · READ-CACHE
R
read-cache
storagejavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

readCache
import readCache from 'read-cache'
const readCache = require('read-cache')
Package uses CommonJS but also works with ESM default import in bundlers like Webpack, Rollup, or TypeScript with esModuleInterop.
readCache.sync
import readCache from 'read-cache'; readCache.sync('file.txt')
import { sync } from 'read-cache'
sync is a method on the default export, not a named export.
readCache.clear
import readCache from 'read-cache'; readCache.clear()
import { clear } from 'read-cache'
clear is a method on the default export, not a named export.

Shows async file read with caching, retrieving cached content, and clearing the cache.

import readCache from 'read-cache'; async function main() { const content = await readCache('example.txt', 'utf8'); console.log('File content:', content); // Cached version (fast) const cached = readCache.get('example.txt'); console.log('Cached:', cached); // Clear cache readCache.clear(); } main().catch(console.error);
Debug
Known issues
gotchaCache is not persistent across process restarts. It only caches in memory for the lifetime of the Node.js process.
fix
Use a persistent caching mechanism (e.g., redis, fs) if cross-process caching is needed.
affects: >=0.0.0
gotchaFile stat check on every read adds overhead; for high-frequency reads, consider using fs.watch or manual caching.
fix
If reading many files rapidly, use a dedicated cache with change detection (e.g., chokidar).
affects: >=0.0.0
gotchaEncoding parameter defaults to null (Buffer) if not specified; be explicit with encoding to get a string.
fix
Always pass 'utf8' (or desired encoding) when expecting string output.
affects: >=0.0.0
gotchareadCache.sync throws synchronously if file does not exist; wrap in try-catch.
fix
Use try-catch: try { const data = readCache.sync('path'); } catch (err) { ... }
affects: >=0.0.0
gotchaThe cache never expires except on file modification via mtime; if mtime does not change, stale data may be served.
fix
Ensure file mtime updates on content change, or call clear() after writes.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: readCache.sync is not a function
Importing sync as a named import instead of accessing it as a method on the default export.
fix
Use: import readCache from 'read-cache'; readCache.sync('file')
Cannot find module 'read-cache'
Package not installed or not in node_modules
fix
Run: npm install read-cache
TypeError: readCache(...).then is not a function
readCache.sync returns a value, not a promise; .then used on sync call.
fix
Use readCache (async) or readCache.sync for synchronous retrieval.
Error: ENOENT: no such file or directory
File does not exist at given path
fix
Check path or use try-catch with readCache.sync.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
30 hits · last 30 days
node
28
Amazon
1
Resources
read-cache — npm install read-cache · libregistry