# @amphibian/cache

> simple object based caching

Latest version **2.1.3** (published 2018-09-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install @amphibian/cache
pnpm add @amphibian/cache
yarn add @amphibian/cache
bun add @amphibian/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 | 2.1.3 |
| Published | 2018-09-20 |
| First published | 2017-01-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 426 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Thomas Lindstrom |

## Links

- npm: https://www.npmjs.com/package/@amphibian/cache
- Repository: https://gitlab.com/thomaslindstr_m/cache
- Homepage: https://gitlab.com/thomaslindstr_m/cache#README
- Issues: https://gitlab.com/thomaslindstr_m/cache/issues
- npm.io page: https://npm.io/package/@amphibian/cache

## Dependencies (9)

- [@amphibian/errors](https://npm.io/package/@amphibian/errors.md) ^2.0.0
- [@amphibian/for-own](https://npm.io/package/@amphibian/for-own.md) ^1.0.14
- [@amphibian/is-number](https://npm.io/package/@amphibian/is-number.md) ^1.0.1
- [@amphibian/is-object](https://npm.io/package/@amphibian/is-object.md) ^1.0.5
- [@amphibian/is-string](https://npm.io/package/@amphibian/is-string.md) ^1.0.2
- [@amphibian/is-boolean](https://npm.io/package/@amphibian/is-boolean.md) ^1.0.5
- [@amphibian/object-keys](https://npm.io/package/@amphibian/object-keys.md) ^1.0.2
- [@amphibian/iterate-up-array](https://npm.io/package/@amphibian/iterate-up-array.md) ^1.0.1
- [@amphibian/object-has-property](https://npm.io/package/@amphibian/object-has-property.md) ^1.0.1

## Recent versions

- 2.1.3 (latest) — 2018-09-20
- 2.1.2 — 2018-08-02
- 2.1.1 — 2018-08-02
- 2.1.0 — 2018-08-02
- 2.0.0 — 2018-06-22
- 1.4.1 — 2017-11-04
- 1.4.0 — 2017-11-04
- 1.3.1 — 2017-09-25
- 1.3.0 — 2017-06-16
- 1.2.5 — 2017-04-29
- 1.2.4 — 2017-01-16
- 1.2.3 — 2017-01-16
- 1.2.2 — 2017-01-12
- 1.2.1 — 2017-01-12
- 1.2.0 — 2017-01-10
- … 2 more at https://npm.io/package/@amphibian/cache/versions

## README

# cache

simple object based caching

```
npm install @amphibian/cache
```

```javascript
var createCache = require('@amphibian/cache');

var userCache = createCache();
userCache.set('user-123', {id: '123', name: 'Some Name'});
console.log(userCache.get('user-123')); // > {id: '123', name: 'Some Name'}

// Invalidate a single cache key
userCache.invalidate('user-123');

// Invalidate the entire cache
userCache.invalidate();

// Get the entire cache object
console.log(userCache.contents()); /* > {
    'user-123': {
        tag: null,
        value: {
            id: '123',
            name: 'Some Name'
        }
    }
*/
```

## Cache lifetime

Data prone to change should always have a set lifetime to avoid having stale data lying around. Set the `Number` of milliseconds as `lifetime` in the `options` object.

```javascript
userCache.set('user-123', {id: '123', name: 'Some Name'}, {
    lifetime: 300 * 1000 // 5 minutes
});

// Assert cache freshness
userCache.fresh('user-123'); // > true
```

## Tagging cache

Setting tags for the cache can be useful, for example, when the data returned is an error. This way you can keep handling the error like you normally would. Set the tag as a `String` `tag` in the `options` object.

```javascript
userCache.set('user-123', {error: 'No user found'}, {
    tag: 'error'
});

userCache.tag('user-123'); // > 'error'
```

## Open a cache

```javascript
var user123Cache = userCache.open('user-123');
user123Cache.set({id: '123', name: 'Some New Name'});
```

## Cache options

When creating a cache you can give an `options` `Object` as the first argument.

```javascript
var userCache = createCache();
```

### `options.enabled` _(`Boolean`)_

Enable or disable the cache. Useful during tests.

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