Transparent cache for promise response for Node.js
Whatever you are caching must be JSON seriazable.
This package is distributed as package in npm repository and can be installed using any node package manager.
yarn add promise-cached
npm install promise-cached --save
This library provides wrapper for promise returning functions.
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! ");
});
});
You can for example use this library to cache http responses.
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);
To ensure code styling is maintained the same, on this project, we rely on editorconfig standard.
You probably can find plugin for your editor (if not included by default) on official website.
yarn test
# or for npm
npm run tests
yarn lint
# or for npm
npm run lint
We are using semver version numbering standard.