# promise-cached

> Transparent cache for promise response for Node.js

Latest version **0.0.4** (published 2020-02-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-cached
pnpm add promise-cached
yarn add promise-cached
bun add promise-cached
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2020-02-18 |
| First published | 2020-01-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 19.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | nemanjan00 |
| Maintainers | nemanjan00 |
| Keywords | cache, redis, promise |

## Links

- npm: https://www.npmjs.com/package/promise-cached
- Repository: https://github.com/nemanjan00/promise-cached
- Homepage: https://github.com/nemanjan00/promise-cached#readme
- Issues: https://github.com/nemanjan00/promise-cached/issues
- npm.io page: https://npm.io/package/promise-cached

## Dependencies (5)

- [husky](https://npm.io/package/husky.md) ^4.0.6
- [redis](https://npm.io/package/redis.md) ^2.8.0
- [eslint](https://npm.io/package/eslint.md) ^6.8.0
- [implement-js](https://npm.io/package/implement-js.md) ^0.0.27
- [node-cache-redis](https://npm.io/package/node-cache-redis.md) ^2.14.0

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

- 0.0.4 (latest) — 2020-02-18
- 0.0.3 — 2020-01-10
- 0.0.2 — 2020-01-10

## README

# promise-cached [![See on Github](https://github.com/themes/tactile/images/octocat-icon.png)](https://github.com/nemanjan00/promise-cached#promise-cached-)

[![Build Status](https://travis-ci.org/nemanjan00/promise-cached.svg?branch=master)](https://travis-ci.org/nemanjan00/promise-cached)

Transparent cache for promise response for Node.js

## Table of contents

<!-- vim-markdown-toc GFM -->

* [Constraints](#constraints)
* [Installation](#installation)
	* [Yarn](#yarn)
	* [NPM](#npm)
* [Usage](#usage)
	* [Basic usage](#basic-usage)
	* [Speeding up existing functions](#speeding-up-existing-functions)
* [Contribution guide](#contribution-guide)
	* [Editorconfig](#editorconfig)
	* [Running tests](#running-tests)
	* [Running linter](#running-linter)
	* [Version numbering](#version-numbering)
* [Authors](#authors)

<!-- vim-markdown-toc -->

## Constraints

Whatever you are caching must be JSON seriazable.

## Installation

This package is distributed as package in [npm](https://www.npmjs.com/) repository and can be installed using any node package manager. 

### Yarn

```
yarn add promise-cached
```

### NPM

```bash
npm install promise-cached --save
```

## Usage

This library provides wrapper for promise returning functions.

### Basic usage

```javascript
const wrapper = require("promise-cached");

// This is a very slow function that returns same response, every time, for same params
const sleepAndReturn = (message) => {
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve(message);
		}, 1000);
	});
};

const options = {
	ttl: 10 * 1000 // Cache lasts for 10 seconds
};

// This function acts just like one above, exept it is very fast after the first time
const cachedFunction = wrapper("sleepAndReturn", sleepAndReturn, options);

cachedFunction("💪").then(() => {
	console.log("This took 1s");

	cachedFunction("💪").then(() => {
		console.log("This was instant! ");
	});
});
```

### Speeding up existing functions

You can for example use this library to cache http responses.

```javascript
const wrapper = require("promise-cached");

const options = {
	ttl: 10 * 1000 // Cache lasts for 10 seconds
};

const axios = require("axios");

const originalGet = axios.get;

axios.get = wrapper("axios.get", originalGet, options);
```

## Contribution guide

### Editorconfig

To ensure code styling is maintained the same, on this project, we rely on [editorconfig](https://editorconfig.org/) standard.

You probably can find plugin for your editor (if not included by default) on [official website](https://editorconfig.org/). 

### Running tests

``` bash
yarn test

# or for npm

npm run tests
```

### Running linter

```bash
yarn lint

# or for npm

npm run lint
```

### Version numbering

We are using [semver](https://semver.org/) version numbering standard. 

## Authors

* [nemanjan00](https://github.com/nemanjan00)

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