Registry / database / cache-manager-fs-hash

cache-manager-fs-hash

JSON →
library3.0.0jsnpmunverified

cache-manager-fs-hash is a file system store for the `node-cache-manager` library, designed to persist key-value pairs to disk. The current stable version is 3.0.0, supporting Node.js versions 18 and above. While no explicit release cadence is stated, the project appears actively maintained. Its key differentiators include the ability to save any `JSON.stringify`-able data, efficient handling of `Buffer` objects (storing larger ones in separate files), and compatibility with Node.js's cluster module for multi-process environments. It employs `.lock` files to prevent race conditions during concurrent file access, ensuring data integrity. This makes it a suitable choice for applications requiring persistent, local caching without relying on external databases.

npm install cache-manager-fs-hash
INSTALL
IMPORT
SIG · CACHE-MANAGER-FS-H
C
cache-manager-fs-hash
databasejavascriptv3.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

DiskStore
import { DiskStore } from 'cache-manager-fs-hash';
const { DiskStore } = require('cache-manager-fs-hash');
Preferred ESM named import for modern Node.js applications (>=18.0.0).
DiskStore
const { DiskStore } = require('cache-manager-fs-hash');
import { DiskStore } from 'cache-manager-fs-hash';
CommonJS named import, suitable for older Node.js or CJS modules.
DiskStore
import type { DiskStore } from 'cache-manager-fs-hash';
import { DiskStore } from 'cache-manager-fs-hash';
TypeScript users should use a type import when only referencing the type, not the runtime value.

Demonstrates direct usage of DiskStore to set, get, delete, and manage cached items with various options.

const { DiskStore } = require('cache-manager-fs-hash'); const diskStore = new DiskStore({ path: 'diskcache', // path for cached files (default: cache) ttl: 60 * 60 * 1000, // time to live in milliseconds // (default: never expires) zip: true, // zip files to save disk space (default: false) hash: true // keys are hashed to generate filenames (default: true) }); (async () => { await diskStore.set('key', 'value'); console.log(await diskStore.get('key')); await diskStore.set('objectKey', { data: 'complex value', timestamp: Date.now() }); console.log(await diskStore.get('objectKey')); await diskStore.del('key'); console.log(await diskStore.get('key')); await diskStore.set('key', 'value', 1000); // with custom TTL console.log(await diskStore.ttl('key')); // Clean up all stored files await diskStore.reset(); console.log('Cache reset.'); })();
Debug
Known issues
gotchaBy default, `ttl` is set to `0` (never expires). This can lead to uncontrolled disk space usage over time if not explicitly configured or managed.
fix
Always set a `ttl` option when initializing `DiskStore` (e.g., `ttl: 60 * 60 * 1000` for 1 hour) or use `cache-manager`'s wrapper functions with explicit TTLs.
affects: >=1.0.0
gotchaSetting `hash: false` will use the raw cache keys as filenames. This can expose sensitive data in filenames, lead to invalid filenames if keys contain special characters, and potentially create conflicts if keys are not unique and valid for the filesystem.
fix
Keep `hash: true` (the default) unless you have a very specific reason not to, and ensure your keys are safe for filenames if disabling hashing. Hashing helps obscure keys and ensures valid filenames.
affects: >=1.0.0
gotchaThe `path` option for the cache directory must have appropriate write/read permissions for the Node.js process. Incorrect permissions will result in `EACCES` or similar errors.
fix
Ensure the specified cache directory exists and the Node.js process has full read/write/execute permissions for it. You might need to `sudo mkdir` and `sudo chown` the directory.
affects: >=1.0.0
breakingThe `engines` field in `package.json` now requires Node.js version `18.0.0` or higher, which might break deployments on older Node.js runtimes. There were no explicit breaking changes noted in the provided documentation for the internal API of v3 compared to v2.
fix
Upgrade your Node.js environment to version 18.0.0 or higher. If you need to support older Node.js versions, consider using an earlier major version of `cache-manager-fs-hash` (e.g., `2.x`).
affects: >=3.0.0
gotchaThe library internally uses `JSON.stringify` to store data. If you attempt to cache objects containing circular references or non-JSON-serializable types (e.g., functions, Symbols, complex class instances without custom `toJSON` methods), they will either fail to store or be stripped of their non-serializable parts.
fix
Ensure that any data you intend to cache is fully JSON-serializable. For complex objects, implement a `toJSON()` method on your class or preprocess the object to remove/serialize non-standard types.
affects: >=1.0.0
Errors
Common errors & fixes
Error: EACCES: permission denied, open '/path/to/diskcache/some_file.lock'
The Node.js process lacks write permissions for the specified cache directory or its parent.
fix
Grant appropriate read/write/execute permissions to the Node.js user for the cache directory (`path` option). For example, `sudo chown -R nodeuser:nodegroup /path/to/diskcache`.
TypeError: DiskStore is not a constructor
The `DiskStore` class was imported incorrectly, often due to trying to use a default import instead of a named import, or a CommonJS `require` in an ESM context.
fix
Use the correct named import: `import { DiskStore } from 'cache-manager-fs-hash';` for ESM, or `const { DiskStore } = require('cache-manager-fs-hash');` for CommonJS.
TypeError: Converting circular structure to JSON
You attempted to cache an object that contains circular references, which `JSON.stringify` cannot handle.
fix
Before caching, ensure your object is free of circular references. You might need to serialize it manually or preprocess it to remove the circularity.
Error: ENAMETOOLONG: name too long, open '...'
This error can occur if `hash: false` is used, and a cache key results in a filename path that exceeds the operating system's maximum path length limit.
fix
Enable hashing by setting `hash: true` (the default) to generate shorter, fixed-length filenames from your keys. Alternatively, ensure your cache keys are concise if `hash: false` is necessary.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
cache-manageroptionalCommonly used in conjunction with this package for a unified caching interface, although not a direct runtime dependency of the store itself.
Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources