tc-cache

1.0.12 • Public • Published

tc-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 :

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 :

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

Side infos

tc-cache also use pino as a logger. You can set it's level by adding an environment variable called LEVEL.

Readme

Keywords

none

Package Sidebar

Install

npm i tc-cache

Weekly Downloads

3

Version

1.0.12

License

ISC

Unpacked Size

4.42 kB

Total Files

6

Last publish

Collaborators

  • toche