{"id":15361552,"url":"https://github.com/jonathanong/redis-cache-fn","last_synced_at":"2025-04-15T08:41:40.308Z","repository":{"id":12693050,"uuid":"72496099","full_name":"jonathanong/redis-cache-fn","owner":"jonathanong","description":"cache a function via redis","archived":false,"fork":false,"pushed_at":"2023-01-07T06:49:58.000Z","size":682,"stargazers_count":5,"open_issues_count":5,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-15T02:12:02.059Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonathanong.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-01T02:21:02.000Z","updated_at":"2023-03-04T06:17:58.000Z","dependencies_parsed_at":"2023-01-13T17:05:20.330Z","dependency_job_id":null,"html_url":"https://github.com/jonathanong/redis-cache-fn","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanong%2Fredis-cache-fn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanong%2Fredis-cache-fn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanong%2Fredis-cache-fn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanong%2Fredis-cache-fn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonathanong","download_url":"https://codeload.github.com/jonathanong/redis-cache-fn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249038887,"owners_count":21202803,"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":[],"created_at":"2024-10-01T12:55:36.417Z","updated_at":"2025-04-15T08:41:40.291Z","avatar_url":"https://github.com/jonathanong.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# redis-cache-fn\n\n[![Node.js CI](https://github.com/jonathanong/redis-cache-fn/actions/workflows/node.js.yml/badge.svg?branch=master\u0026event=push)](https://github.com/jonathanong/redis-cache-fn/actions/workflows/node.js.yml)\n[![codecov](https://codecov.io/gh/jonathanong/redis-cache-fn/branch/master/graph/badge.svg?token=Xd4tvpcLGe)](https://codecov.io/gh/jonathanong/redis-cache-fn)\n\nCaches your functions via Redis.\nFeatures:\n\n- Uses Redis caching, expiration, and pub/sub.\n- Concurrency locking - if the function is being run elsewhere with the same arguments, it will wait for the result of that function instead of executing again.\n- Caching - caches results for as long as you want. If you set `ttl=0`, then you're just this library for concurrency locking, which is completely fine. However, please keep in mind that locking mechanism in this module is not robust - it is more suited towards caching.\n- Only tested with [ioredis](https://github.com/luin/ioredis)\n\nUse Cases:\n\n- Race conditions\n- API calls with rate limits\n- Expensive database calls\n- Expensive function calls\n\nDifferences from [redis-cache-decorator](https://github.com/jonathanong/redis-cache-decorator):\n\n- Uses a class system so that it's more maintainable\n- A slightly different API\n- Doesn't bother throwing when functions timeout. Use another library instead.\n- Removes stream support\n\n## API\n\n### const Cache = require('redis-cache-fn')(options)\n\n- `client \u003crequired\u003e` - a `ioredis` client for GET/SET/PUBLISH, etc.\n- `subscriber \u003crequired\u003e` - a `ioredis` client for `PSUBSCRIBE`\n- `namespace = ''` - a prefix for all the events\n- `encoding = 'json'` - how data is encoded between redis and node.js.\n  Supported values are:\n  - `json` - the default\n  - `string`\n  - `buffer`\n- `ttl = '30s'` - the TTL expiration in seconds.\n- `timeout = 0` - how long to wait for the function to execute before executing the function again. By default, there is no timeout.\n  - Ex. if it the current function has waited 30s for the function to complete elsewhere, it will say \"F IT\" and run the function itself.\n- `pollInterval = '1s'` - how often to poll for new values.\n- `precache = 3/4` - when the age hits this threshold, execute the function again to so that the cache remains fresh.\n- `onError = err =\u003e console.error(err.stack)` - an error handler for redis network errors.\n\n### Cache = Cache.extend(options\\\u003cObject\\\u003e)\n\nSubclasses the cache, extending its options.\nAll options are overwritten except:\n\n- `namespace` - the namespace is simply concatenated with `:`s.\n\n### fn = Cache.wrap(function\\\u003cFunction\\\u003e)\n\nDecorates the function so that it hits the cache first.\n\n### { Cache } = fn\n\nThe function's Cache constructor.\n\n### promise = fn()\n\nReturns a promise.\n\n### promise.cache\n\nThe cache instance of this function call.\n\n### const hash = Cache.createHash(args)\n\n### const cache = new Cache(args)\n\n### value = await cache.set(value)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathanong%2Fredis-cache-fn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonathanong%2Fredis-cache-fn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathanong%2Fredis-cache-fn/lists"}