tc-cache
v1.0.12
Published
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.
Downloads
2
Readme
#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
.