Registry / storage / persistent-cache

persistent-cache

JSON →
library1.1.2jsnpmunverified

A simple Node.js module to persistently store/cache arbitrary data to disk with optional memory mirroring for performance. Version 1.1.2 (latest) has low release activity with no recent updates. It provides both synchronous and asynchronous APIs for get, put, delete, and keys operations. Data is stored as files on disk, and cache entries can have a time-to-live duration. It is lightweight with no dependencies, but offers basic functionality compared to more feature-rich caching libraries like node-cache or lru-cache. Suitable for simple persistent storage needs, but has known issues with path handling and race conditions on concurrent access.

npm install persistent-cache
INSTALL
IMPORT
SIG · PERSISTENT-CACHE
P
persistent-cache
storagejavascriptv1.1.2
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.

persistentCache
import persistentCache from 'persistent-cache'
const persistentCache = require('persistent-cache')
The default export is a factory function that creates a cache instance. In CommonJS, use require('persistent-cache'). The library does not ship TypeScript types.
Cache
const cache = persistentCache(options)
const cache = new persistentCache(options)
persistentCache is a factory function, not a constructor. Do not use 'new'.
cache.put
cache.put('key', data, cb)
cache.put('key', data)
If using async put, a callback is expected. Omitting it may cause unexpected behavior or errors.
cache.get
cache.get('key', (err, data) => {})
const data = cache.get('key')
Async get takes a callback; sync counterpart is cache.getSync(key). Mixing them up is common.
cache.keys
cache.keys(cb)
const keys = cache.keys()
Similar to get, keys is async; use keysSync for synchronous retrieval.

Demonstrates basic usage: create cache with options, put/get data asynchronously and synchronously, list keys, delete entries, and unlink the entire cache.

import persistentCache from 'persistent-cache'; const cache = persistentCache({ name: 'mycache', duration: 1000 * 60 * 60 }); // Asynchronous put cache.put('key1', { hello: 'world' }, (err) => { if (err) throw err; console.log('Data stored'); // Asynchronous get cache.get('key1', (err, data) => { if (err) throw err; console.log('Retrieved:', data); }); }); // Synchronous put cache.putSync('key2', 'some string'); // Synchronous get const val = cache.getSync('key2'); console.log('Sync retrieved:', val); // Get all keys cache.keys((err, keys) => { if (err) throw err; console.log('All keys:', keys); }); // Delete a key cache.delete('key1', (err) => { if (err) throw err; console.log('Deleted key1'); }); // Remove entire cache storage cache.unlink((err) => { if (err) throw err; console.log('Cache unlinked'); });
Debug
Known issues
gotchaCache storage is file-based; concurrent access from multiple Node processes can cause race conditions or corrupted data.
fix
Use a caching library designed for concurrency (e.g., redis, lru-cache with disk persistence) or implement file locking.
affects: <=1.1.2
gotchaThe default base directory is the main module's directory (process.cwd()), which may not be writable in production or packaged apps.
fix
Always specify an explicit base option pointing to a writable path.
affects: <=1.1.2
deprecatedThe constructor pattern 'new persistentCache(...)' is incorrectly used in some examples, but it is a factory function.
fix
Call persistentCache(options) without the 'new' keyword.
affects: >=1.0.0
gotchaThe callback for async methods (put, get, etc.) may not always be called if an error occurs before the operation starts.
fix
Wrap calls in try-catch or use promise wrappers. Consider using synchronous methods for simplicity.
affects: <=1.1.2
gotchaThe module does not create the base directory if it does not exist; it will throw an error.
fix
Ensure the base directory exists before creating a cache instance.
affects: <=1.1.2
Errors
Common errors & fixes
Error: ENOENT: no such file or directory, open '.../cache/someKey'
The cache directory or file does not exist. This can happen if the base directory is not correctly set or the cache is unlinked.
fix
Verify the base and name options, ensure the directory is writable, and recreate the cache if necessary.
TypeError: cache.put is not a function
The cache was not created correctly. Common mistake: calling 'new persistentCache()' instead of 'persistentCache()'.
fix
Create the cache using persistentCache(options) without the 'new' keyword.
RangeError: Maximum call stack size exceeded
Infinite recursion due to circular references in data passed to put. The library uses JSON.stringify internally.
fix
Ensure data passed to put is serializable (no circular references, no functions).
Error: Cannot find module 'persistent-cache'
The package is not installed. This can happen when using the package without installing it first.
fix
Run 'npm install persistent-cache' in your project directory.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
persistent-cache — npm install persistent-cache · libregistry