# cache-manager-fs-hash

> file system store for node cache manager

Latest version **3.0.0** (published 2025-08-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install cache-manager-fs-hash
pnpm add cache-manager-fs-hash
yarn add cache-manager-fs-hash
bun add cache-manager-fs-hash
```

## Health

**Score 33/100 (F)** — status: maintenance-mode.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2025-08-30 |
| First published | 2018-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/cache-manager-fs-hash) |
| Module format | CommonJS |
| Node | >=18.0.0 |
| Dependencies | 1 |
| Unpacked size | 25.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 40 |
| Author | Roland Starke |
| Maintainers | rolandstarke |
| Keywords | cache-manager, storage, filesystem, disk-store, key-value-store |

## Links

- npm: https://www.npmjs.com/package/cache-manager-fs-hash
- Repository: https://github.com/rolandstarke/node-cache-manager-fs-hash
- Homepage: https://github.com/rolandstarke/node-cache-manager-fs-hash#readme
- Issues: https://github.com/rolandstarke/node-cache-manager-fs-hash/issues
- npm.io page: https://npm.io/package/cache-manager-fs-hash

## Dependencies (1)

- [lockfile](https://npm.io/package/lockfile.md) ^1.0.4

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2025-08-30
- 2.0.0 — 2024-04-21
- 1.1.0 — 2024-04-21
- 1.0.0 — 2021-07-10
- 0.0.9 — 2020-06-12
- 0.0.8 — 2020-04-14
- 0.0.7 — 2019-02-01
- 0.0.6 — 2018-09-27
- 0.0.5 — 2018-09-14
- 0.0.4 — 2018-09-14
- 0.0.3 — 2018-02-18
- 0.0.2 — 2018-02-18
- 0.0.1 — 2018-02-18

## README

# Node Cache Manager store for Filesystem

[![Build](https://github.com/rolandstarke/node-cache-manager-fs-hash/actions/workflows/node.js.yml/badge.svg)](https://github.com/rolandstarke/node-cache-manager-fs-hash/actions/workflows/node.js.yml)
[![npm package](https://img.shields.io/npm/v/cache-manager-fs-hash.svg)](https://www.npmjs.com/package/cache-manager-fs-hash)
[![node](https://img.shields.io/node/v/cache-manager-fs-hash.svg)](https://nodejs.org)

Package to cache key-value pairs in JSON files.

## Installation

```sh
npm install cache-manager-fs-hash
```

## Features

* Saves anything that is `JSON.stringify`-able to disk
* Buffers are saved as well (if they reach a certain size they will be stored to separate files)
* Works well with the cluster module

## Usage example

Here is an example that demonstrates how to use the filesystem cache store.

```javascript
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: false, // keys are hashed to generate filenames (default: true)
                 // set to false to use plain keys as filenames
});

(async () => {
    await diskStore.set('key', 'value');
    console.log(await diskStore.get('key')); // "value"

    await diskStore.del('key');
    console.log(await diskStore.get('key')); // undefined

    await diskStore.set('key', 'value', 1000); // with custom TTL
    console.log(await diskStore.ttl('key')); // 999 milliseconds

    // delete stored files
    await diskStore.reset();
})();
```

Here is an example that demonstrates how to use the store with the [node-cache-manager](https://github.com/jaredwray/cache-manager) module.

```javascript
const cacheManager = require('cache-manager');
const { DiskStore } = require('cache-manager-fs-hash');

const diskCache = cacheManager.createCache(new DiskStore({
    path: 'diskcache', // path for cached files
    // ... other options
}));


(async () => {

    await diskCache.set('key', 'value');
    console.log(await diskCache.get('key')); // "value"

    console.log(await getUserCached(5)); // {id: 5, name: '...'}
    console.log(await getUserCached(5)); // {id: 5, name: '...'}

    function getUserCached(userId) {
        return diskCache.wrap(userId, function () {
            return getUser(userId);
        });
    }

    async function getUser(userId) {
        await new Promise(r => setTimeout(r, 100)); // sleep 0.1 seconds
        return {id: userId, name: '...' + Math.random()};
    }

})();
```

## How it works

The library saves each cached item as a separate file under the specified directory. Writes use a `.lock` file to ensure that multiple instances accessing the same cache file do not interfere with each other.

## Tests

```sh
npm test
```

## License

cache-manager-fs-hash is licensed under the MIT license.

---
_Source: https://npm.io/package/cache-manager-fs-hash · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
