{"id":17706122,"url":"https://github.com/fernandomoraes/nodejs-remote-promise-cacheable","last_synced_at":"2025-03-13T09:33:01.125Z","repository":{"id":49416907,"uuid":"375887134","full_name":"fernandomoraes/nodejs-remote-promise-cacheable","owner":"fernandomoraes","description":"Remote promise cacheable for Nodejs","archived":false,"fork":false,"pushed_at":"2021-06-18T13:17:36.000Z","size":349,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-23T13:43:10.036Z","etag":null,"topics":["cache","lock","nodejs","redis","requests","semaphore"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fernandomoraes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-11T02:54:06.000Z","updated_at":"2024-02-21T12:13:31.000Z","dependencies_parsed_at":"2022-08-31T19:20:10.505Z","dependency_job_id":null,"html_url":"https://github.com/fernandomoraes/nodejs-remote-promise-cacheable","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fernandomoraes%2Fnodejs-remote-promise-cacheable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fernandomoraes%2Fnodejs-remote-promise-cacheable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fernandomoraes%2Fnodejs-remote-promise-cacheable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fernandomoraes%2Fnodejs-remote-promise-cacheable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fernandomoraes","download_url":"https://codeload.github.com/fernandomoraes/nodejs-remote-promise-cacheable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221359423,"owners_count":16804797,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","lock","nodejs","redis","requests","semaphore"],"created_at":"2024-10-24T23:06:56.684Z","updated_at":"2024-10-24T23:06:57.333Z","avatar_url":"https://github.com/fernandomoraes.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![codecov](https://codecov.io/gh/fernandomoraes/nodejs-remote-promise-cacheable/branch/master/graph/badge.svg?token=31HHCRCKYV)](https://codecov.io/gh/fernandomoraes/nodejs-remote-promise-cacheable)\n![lint and test](https://github.com/fernandomoraes/nodejs-remote-promise-cacheable/actions/workflows/push.yml/badge.svg)\n![release](https://github.com/fernandomoraes/nodejs-remote-promise-cacheable/actions/workflows/release.yml/badge.svg)\n\n# nodejs remote promise cacheable\n\nA lightweight nodejs library that allows you to avoid duplicated processing (http calls, database, processing, etc) in a distributed environment. The library is based on Redis lock and pub/sub operations to work.\n\nThe library tries to bring the best of the distributed world: **high throughput** and **low latency**, while keeping all operations **highly resilient**.\n\nThe main objective here is to avoid **unnecessary** work process. To achieve good throughput, latency and high availability numbers, we had to compromise on consistency, so if something goes wrong, the flow will continue and the target will be processed. If you have hard consistency requirements in your business, this library is not for you.\n\n## Using\n\n### Installation\n\n**yarn**\n\n```.sh\nyarn add @moraes/remote-promise-cacheable\n```\n\n**npm**\n\n```.sh\nnpm install --save @moraes/remote-promise-cacheable\n```\n\n### Code\n\n```.js\nimport Redis from 'ioredis';\nimport { PromiseCacheable } from '@moraes/remote-promise-cacheable';\n\nconst cacheable = new PromiseCacheable(() =\u003e new Redis());\n\nconst callOptions = {\n    key: 'my-slow-process-identifier',\n    cacheTimeout: 10 * 60 * 1000,\n    waitTimeout: 10 * 1000,\n};\n\n//simple sync operation\nconst result = await cacheable.call(callOptions, () =\u003e {\n    //my slow process\n    return 'result value';\n});\n\n//a more real use case, using async operation\nconst result = await cacheable.call(callOptions, ()=\u003e {\n    return axios.get('https://google.com.br')\n        .then(response =\u003e response.data);\n});\n\n//release redis connection\ncacheable.close();\n```\n\n### Properties\n\nFor each call, the following properties must be used:\n\n-   **key** - the process identifier\n-   **cacheTimeout** - total time in ms that the result will be on cache\n-   **waitTimeout** - total time in ms that will be waiting while another call is finishing\n\n### Logging\n\nFor logging, you must configure a [winston](https://www.npmjs.com/package/winston) logger instance, example:\n\n```.js\nconst cacheable = new PromiseCacheable(() =\u003e new Redis(), {\n    logger: winston.createLogger({\n        level: 'info',\n        transports: [new winston.transports.Console()]\n    })\n});\n```\n\n## Best practices\n\n### Redis timeout\n\nConfigure Redis timeout for a resilient communication, the value should be low to quickly recover from a problem, and high to support a spike. Remember: timeout can be both a remedy and a poison.\n\n### Wait timeout\n\nConfigure this value to be something close to the processing time of the real operation, this will prevent you of waiting a long time before knowing there was a problem.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffernandomoraes%2Fnodejs-remote-promise-cacheable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffernandomoraes%2Fnodejs-remote-promise-cacheable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffernandomoraes%2Fnodejs-remote-promise-cacheable/lists"}