# service-cache

> Proxy for caching service responses

Latest version **0.0.2** (published 2020-06-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install service-cache
pnpm add service-cache
yarn add service-cache
bun add service-cache
```

## 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.2 |
| Published | 2020-06-13 |
| First published | 2020-06-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 3.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | nemanjan00 |
| Maintainers | nemanjan00 |

## Links

- npm: https://www.npmjs.com/package/service-cache
- Repository: git@github.com:nemanjan00/service-cache
- npm.io page: https://npm.io/package/service-cache

## Dependencies (1)

- [promise-cached](https://npm.io/package/promise-cached.md) ^0.0.4

## Recent versions

- 0.0.2 (latest) — 2020-06-13
- 0.0.1 — 2020-06-13

## README

# service-cache

Proxy for caching service responses

## Table of contents

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

* [Installation](#installation)
* [Usage](#usage)
	* [Simple example](#simple-example)
	* [Full example](#full-example)

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

## Installation

```bash
npm install service-cache --save
```

## Usage

### Simple example

```javascript
const wrapper = require("service-cache");

const originalService = {
	something: () => {
		return doSomething();
	}
};

const service = wrapper(originalService);

// service is transparent proxy to originalService,
// unless you try to run function with Cached suffix

service.somethingCached(); // This will return response from cache,
// if function was already run

// keep in mind that if you run function multiple times before there is
// response in cache, it will be run multiple times...

// you can use queue-promised to make sure you run it just once

```

### Full example

```javascript
const wrapper = require("service-cache");

const service = wrapper({
	sleep: (message) => {
		return new Promise(resolve => {
			setTimeout(() => {
				resolve(message);
			}, 1000);
		});
	}
});

const start = Date.now();

const array = Array(10).fill(true);

// Chain 10 promises that wait for 1s and then return, with cache enabled
array.reduce((last) => last.then(() => service.sleepCached("cached")), Promise.resolve()).then(() => {
	console.log(`Cached is done after ${(Date.now() - start)/1000}s`);
});

// Chain 10 promises that wait for 1s and then return, with cache disabled
array.reduce((last) => last.then(() => service.sleep("normal")), Promise.resolve()).then(() => {
	console.log(`Normal is done after ${(Date.now() - start)/1000}s`);
});
```

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