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-hashVerified import paths — ran on the pinned version, not inferred.
Demonstrates direct usage of DiskStore to set, get, delete, and manage cached items with various options.
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.
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.
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.
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`).
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.
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`.
Use the correct named import: `import { DiskStore } from 'cache-manager-fs-hash';` for ESM, or `const { DiskStore } = require('cache-manager-fs-hash');` for CommonJS.Before caching, ensure your object is free of circular references. You might need to serialize it manually or preprocess it to remove the circularity.
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.