# @wealthbar/data-cache

> Client side data cache

Latest version **1.1.0** (published 2018-06-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @wealthbar/data-cache
pnpm add @wealthbar/data-cache
yarn add @wealthbar/data-cache
bun add @wealthbar/data-cache
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2018-06-26 |
| First published | 2018-06-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Unpacked size | 177.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Cliff Hammerschmidt |
| Maintainers | chrisnicola, pezillionaire, tanglebones, thejf |
| Keywords | datacache |

## Links

- npm: https://www.npmjs.com/package/@wealthbar/data-cache
- Repository: https://gitlab.wealth.bar/wealthbar/caboodle
- npm.io page: https://npm.io/package/@wealthbar/data-cache

## Dependencies (12)

- [npm](https://npm.io/package/npm.md) ^6.1.0
- [nyc](https://npm.io/package/nyc.md) ^10.3.2
- [node](https://npm.io/package/node.md) ^10.4.1
- [mocha](https://npm.io/package/mocha.md) 5
- [sinon](https://npm.io/package/sinon.md) ^6.0.0
- [ts-lint](https://npm.io/package/ts-lint.md) ^4.5.1
- [ts-node](https://npm.io/package/ts-node.md) ^6.1.1
- [typescript](https://npm.io/package/typescript.md) ^2.9.2
- [@types/node](https://npm.io/package/@types/node.md) ^10.3.4
- [@types/mocha](https://npm.io/package/@types/mocha.md) ^5.2.2
- [@types/sinon](https://npm.io/package/@types/sinon.md) ^5.0.1
- [source-map-support](https://npm.io/package/source-map-support.md) ^0.5.6

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

- 1.1.0 (latest) — 2018-06-26
- 1.0.0 — 2018-06-22

## README

# data-cache

Provides a client side in-memory, limited size, data cache for storing results from server calls. It uses 
least-recently accessed (with a maximum) eviction algorithm instead of strict LRU. 

## `dataCacheCtor({maxWeight,maxAccessed}): dataCacheType`

```
function dataCacheCtor(
  {
    maxWeight,
    maxAccessed,
  }: {
    maxWeight?: number,
    maxAccessed?: number,
  } = {}
): dataCacheType
```

Returns a new `dataCache`. `maxWeight` defaults to `1000000` and `maxAccessed` defaults to `8`. "Weight" is used
instead of a specific size measurement; `dataCacheObjectWeight` is provided as a default method for computing the
relative space requirements of an object and it's lookup key. Since we can't assume all objects stored will be
JSON serializable (or that the user wants to take that performance hit) the user is still responsible for providing
the weight of each entry in the cache (including the key size).

## `dataCacheObjectWeight({key,entry}):number`

```
function dataCacheObjectWeight(
  {
    key,
    entry,
  }: {
    key: string,
    entry: object | undefined,
  }
): number
```

Convenience function to compute the weight of a "POJO".

## `dataCacheEvict({dataCache,key}):void`

```
function dataCacheEvict(
  {
    dataCache,
    key,
  }: {
    dataCache: dataCacheType,
    key: string,
  }
):void
```

Evicts `key` from the `dataCache`.

## `dataCacheReap({ dataCache,weight}):void`

```
function dataCacheReap(
  {
    dataCache,
    weight,
  }: {
    dataCache: dataCacheType,
    weight: number,
  }
): void
```

Remove entries from the cache until a new entry of `weight` can be added without exceeding the `maxWeight`.
This will throw if `weight` exceeded `maxWeight` (i.e. the entry can't fit in the cache).

## `dataCachePut({dataCache,key,entry,weight}):void`

```
function dataCachePut(
  {
    dataCache,
    key,
    entry,
    weight,
  }: {
    dataCache: dataCacheType,
    key: string,
    entry: object | undefined,
    weight: number,
  }
): void
```

Adds an `entry` to the `dataCache` under the key `key`, evicting if needed to accommodate its `weight`.

## `dataCacheGet({dataCache,key,peek}): object | undefined`

```
function dataCacheGet(
  {
    dataCache,
    key,
    peek,
  }: {
    dataCache: dataCacheType,
    key: string,
    peek?: boolean,
  }
): object | undefined
```

Retrieves an `entry` from the `dataCache` under the key `key`. If `peek` is `true` the access count is not
incremented. If the `entry` is not in the cache (evicted or never been added) `undefined` is returned.

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