# keolis-cache

> You have to get a redis bdd running somewhere, as this package will connect to it. You can set cache expiration with the environment variable ``KL_CACHE_DURATION``, which take a duration in seconds.

Latest version **1.0.11** (published 2019-05-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install keolis-cache
pnpm add keolis-cache
yarn add keolis-cache
bun add keolis-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.11 |
| Published | 2019-05-17 |
| First published | 2019-05-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 4.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | toche |
| Maintainers | toche |

## Links

- npm: https://www.npmjs.com/package/keolis-cache
- npm.io page: https://npm.io/package/keolis-cache

## Dependencies (3)

- [pino](https://npm.io/package/pino.md) ^5.12.5
- [redis](https://npm.io/package/redis.md) ^2.8.0
- [dotenv](https://npm.io/package/dotenv.md) ^8.0.0

## Recent versions

- 1.0.11 (latest) — 2019-05-17
- 1.0.10 — 2019-05-17
- 1.0.8 — 2019-05-17
- 1.0.7 — 2019-05-17
- 1.0.6 — 2019-05-17
- 1.0.5 — 2019-05-17
- 1.0.4 — 2019-05-17
- 1.0.3 — 2019-05-17
- 1.0.2 — 2019-05-17
- 1.0.1 — 2019-05-17
- 1.0.0 — 2019-05-17

## README

#keolis-cache

###Requirement

You have to get a redis bdd running somewhere, as this package will connect to it.
You can set cache expiration with the environment variable ``KL_CACHE_DURATION``, which take a duration in seconds.

###Example

Instead of this :  
````typescript
router.get('', async ({query}, res) => {
  try {
    const response = await myService(query);
    res.status(response.status).send(response.body);
  } catch (error) {
    logger.error(error);
    res.status(error.status || 500).send(error);
  }
});
````  

Do this :  
````typescript
const HASH = 'YourHashForThisService';

router.get('', async (req, res) => {
  try {
    const response = await getFromCacheOrHttp(HASH, '/myRoute', req, myService);
    res.status(response.status).send(response.body);
  } catch (error) {
    logger.error(error);
    res.status(error.status || 500).send(error);
  }
});
````  
That's it.  
This require to decompose `req` in your service instead of your route... but let's just say i'm sorry for this.

### Latter functionnalities

Random hash on startup

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