# cachify-wrapper

> Wraps a function with a caching layer

Latest version **6.0.18** (published 2021-06-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install cachify-wrapper
pnpm add cachify-wrapper
yarn add cachify-wrapper
bun add cachify-wrapper
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.18 |
| Published | 2021-06-14 |
| First published | 2018-08-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 65.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | a.chepugov |
| Keywords | cache, function wrapper, memoize, d.ts, typescript, ts |

## Links

- npm: https://www.npmjs.com/package/cachify-wrapper
- Repository: https://github.com/a-chepugov/cachify-wrapper
- Homepage: https://github.com/a-chepugov/cachify-wrapper#readme
- Issues: https://github.com/a-chepugov/cachify-wrapper/issues
- npm.io page: https://npm.io/package/cachify-wrapper

## 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

- 6.0.18 (latest) — 2021-06-14
- 6.0.17 — 2021-06-14
- 6.0.16 — 2021-06-07
- 6.0.10 — 2021-05-14
- 6.0.4 — 2021-05-13
- 6.0.0 — 2021-05-13
- 5.0.0 — 2020-07-07
- 4.1.0 — 2018-11-09
- 4.0.2 — 2018-11-07
- 4.0.1 — 2018-09-07
- 3.2.4 — 2018-08-28
- 3.2.2 — 2018-08-27
- 3.2.1 — 2018-08-27
- 3.2.0 — 2018-08-27
- 3.1.0 — 2018-08-27
- … 4 more at https://npm.io/package/cachify-wrapper/versions

## README

# cachify-wrapper

Wraps a function with a caching layer

## Usage

### callback

Wraps a function with a caching layer

#### Parameters

*   `fn` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** `callback-last` style function
*   `storage` **[Storage](#storage)\<K, [RecordPacked](#recordpacked)\<V>>** cache storage (optional, default `InMemoryStorageCb`)
*   `options` **[Options](#options)**  (optional, default `{}`)
*   `hasher` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** creates key for KV-storage from `fn` arguments (optional, default `JSON.stringify`)

#### Examples

```javascript
const wrapper = require('cachify-wrapper').default;
class Storage {
	constructor() {
		this.data = new Map();
	}
	get = (key, cb) => cb(null, this.data.get(key))
	set = (key, value, ttl, cb) => {
		this.data.set(key, value);
		if (ttl > 0) setTimeout(() => this.data.delete(key), ttl);
		cb(null, true);
	}
	del = (key, cb) => cb(null, this.data.delete(key))
}
const storage = new Storage();
let count = 0;
const inc = (a, cb) => cb(null, count += a);
const cached = wrapper(inc, storage, {expire: 100});
setTimeout(() => cached(1, (_error, payload) => console.info(payload)), 0); // Invokes request
setTimeout(() => cached(1, (_error, payload) => console.info(payload)), 100); // Takes cached result
setTimeout(() => cached(1, (_error, payload) => console.info(payload)), 200); // Invokes second request

cached.set(2, 'manual value', 1000, () =>
	cached.get(2, (_, result) => {
		console.info(result);
		cached.del(2, () =>
			cached.get(2, (_, result) => console.info(result)))
	}));
```

Returns **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**

### promise

Wraps a function with a caching layer

#### Parameters

*   `fn` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
*   `storage` **[Storage](#storage)\<K, [RecordPacked](#recordpacked)\<V>>?**
*   `options` **[Options](#options)?**
*   `hasher` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?**

#### Examples

```javascript
const wrapperPromise = require('cachify-wrapper').promise;
let count = 0;
const inc = async(a) => count += a;
const cached = wrapperPromise(inc, storage, {expire: 1000});
const p1 = cached(1).then((payload) => console.info(payload)); // Invokes request
const p2 = p1.then(() => cached(1).then((payload) => console.info(payload))); // Takes cached result
```

Returns **function (...any): [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<any>**

### Options

Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)

#### Properties

*   `storage` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?**

    *   `storage.timeout` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** max storage response time before considering it as failed, and invoking `fn`
*   `source` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?**

    *   `source.timeout` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** max `fn` response time before considering it as failed
*   `expire` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** time to consider cached data expired \[in milliseconds]
*   `spread` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** expire time spread (prevents simultaneous deletions saved items from storage)
*   `lock` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** lock timeout (prevents simultaneous concurrent invoke of `fn` at initial period)
*   `stale` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** additional ttl for stale data
*   `ttl` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** forced ttl (TimeToLive) for data (useful if storage is using from multiply services with different expire)
*   `retries` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** number of storage requests passes before `fn` call
*   `error` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** ttl for erroneous state cache (prevents frequent call of `fn`)
*   `verbose` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** verbosity flag

### CacheAbsentError

**Extends Error**

no cache error

### Functions

### Storage

storage interface

#### get

##### Parameters

*   `key` **K**
*   `cb` **CB\<V>**

#### set

##### Parameters

*   `key` **K**
*   `value` **V**
*   `ttl` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
*   `cb` **CB<[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>**

#### del

##### Parameters

*   `key` **K**
*   `cb` **CB<[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>**

### RecordPacked

Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)

#### Properties

*   `e` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?**
*   `v` **V?**
*   `t` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
*   `l` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**

### InMemoryStorageCb

#### get

##### Parameters

*   `key` **K**
*   `cb` **CB\<V>**

#### set

##### Parameters

*   `key` **K**
*   `value` **V**
*   `ttl` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
*   `cb` **CB<[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>**

#### del

##### Parameters

*   `key` **K**
*   `cb` **CB<[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>**

### InMemoryStorageSerializable

**Extends InMemoryStorage**

#### expire

##### Parameters

*   `key` **K**
*   `ttl` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**

#### export

Returns **Iterable\<KRecordTuple\<K, V>>**

#### import

##### Parameters

*   `dump` **Iterable\<KRecordTuple\<K, V>>**

### InMemoryStorageRecord

Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)

#### Properties

*   `value` **V?**
*   `timestamp` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
*   `ttl` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**

### InMemoryStorage

#### Parameters

*   `source` **Iterable\<KRecordTuple\<K, V>>?**

#### get

##### Parameters

*   `key` **K**

#### set

##### Parameters

*   `key` **K**
*   `value` **V**

#### del

##### Parameters

*   `key` **K**

#### has

##### Parameters

*   `key` **K**

#### expire

##### Parameters

*   `key` **K**
*   `ttl` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**

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