# ya-cache

> Yet another cache

Latest version **1.0.1** (published 2015-06-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install ya-cache
pnpm add ya-cache
yarn add ya-cache
bun add ya-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.0.1 |
| Published | 2015-06-15 |
| First published | 2015-06-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Spencer Alger |
| Maintainers | spalger |
| Keywords | cache, fs, lock |

## Links

- npm: https://www.npmjs.com/package/ya-cache
- Repository: https://github.com/spalger/ya-cache
- Homepage: https://github.com/spalger/ya-cache#readme
- Issues: https://github.com/spalger/ya-cache/issues
- npm.io page: https://npm.io/package/ya-cache

## Dependencies (5)

- [debug](https://npm.io/package/debug.md) ^2.2.0
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [promify](https://npm.io/package/promify.md) ^1.0.1
- [lockfile](https://npm.io/package/lockfile.md) ^1.0.1
- [babel-runtime](https://npm.io/package/babel-runtime.md) ^5.5.8

## 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.0.1 (latest) — 2015-06-15
- 1.0.0 — 2015-06-15
- 1.0.0-alpha.2 — 2015-06-15
- 1.0.0-alpha.1 — 2015-06-14
- 1.0.0-alpha — 2015-06-14

## README

# yet another cache

Why the heck does the world need another caching module? Well, it probably doesn't but while working on [http-asset](http://github.com/spalger/http-asset) part of me was feeling crazy, part of me was feeling ambitious, and the rest of me was too lazy to sift through all the caching modules on NPM to find the one that best fit the bill.

So, I offer you ya-cache.

## features

 - super simple api
 - file system backed
 - cache is just JSON in a file
 - file system locking ¯\\&#95;(ツ)&#95;/¯

## example

```js
let Cache = require('ya-cache')
let thingCache = new Cache(join(__dirname, '__cache__'))

const HOUR = 1000 * 60 * 60

let [expires, thing] = thingCache.gets('expire', 'thing')
if (expires && expires < Date.now()) {
  thing = await calculateComplexThingy()
  await thingCache.sets({
    expire: Date.now() + 24 * HOUR
    thing: thing
  })
}

doSomethingWithThing(thing)

```

## api

---
#### `new Cache(path, lockFileOptions = {})`

Constructs a cache object, provides the api to each cache


---
#### `cache.get([key]) -> Promise`

Get the value of a key, returns a promise. If the key argument is undefined then all keys and their values are returned in an object.

```js
cache.get('key').then(function (val) {
  console.log('value for key is', val)
})
```

---
#### `cache.set(key, val) -> Promise`

Set the value of a key, returns a promise.

```js
cache.set('key', 'value').then(function () {
  console.log('value for %s is now set to %s', 'key', 'value')
})
```

---
#### `cache.gets(...keys) -> Promise`

Get one or more keys, returns a promise that resolves to an array of values.

```js
cache.gets('key1', 'key2', 'key3').then(function (vals) {
  vals.forEach(function (val, i) {
    console.log('key%i has value %s', i + 1, val)
  })
})
```

---
#### `cache.sets({ key: value ... }) -> Promise`

Set one of more keys, returns a promise that resolves to an object of the saved values.

```js
cache.sets({
  key1: 'foo'
  key2: 'bar'
  key3: 'baz'
}).then(function (vals) {
  console.log('saved vals', vals)
})
```

---
#### `cache.clear([key]) -> Promise`

Clear a key in the cache. If the key is undefined then all keys are cleared

```js
cache.set('foo', 'bar')
cache.clear('foo')
cache.get('foo').then(function (val) {
  console.log('foo equals "%s"', val) // foo equals "undefined"
})
```

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