{"id":15043542,"url":"https://github.com/tomerh2001/cache-manager-function","last_synced_at":"2025-08-21T10:32:38.093Z","repository":{"id":255543753,"uuid":"852374553","full_name":"tomerh2001/cache-manager-function","owner":"tomerh2001","description":"Cache functions dynamically based on their arguments using cache-manager.","archived":false,"fork":false,"pushed_at":"2024-12-08T21:39:46.000Z","size":691,"stargazers_count":1,"open_issues_count":13,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-08T22:27:49.647Z","etag":null,"topics":["cache","cache-control","cache-manager","decorators","function-cache","functions","nodejs","redis","typescript"],"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/tomerh2001.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-04T17:41:06.000Z","updated_at":"2024-11-28T10:07:53.000Z","dependencies_parsed_at":"2024-10-23T02:25:23.871Z","dependency_job_id":"e4c6ef33-8ede-47b4-82e3-597b50f7805d","html_url":"https://github.com/tomerh2001/cache-manager-function","commit_stats":{"total_commits":56,"total_committers":4,"mean_commits":14.0,"dds":0.3035714285714286,"last_synced_commit":"96c43ac2d0dc206e8005221cc92ba0e6805c6b75"},"previous_names":["tomerh2001/cache-manager-function"],"tags_count":11,"template":false,"template_full_name":"tomerh2001/semantic-release-repo-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomerh2001%2Fcache-manager-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomerh2001%2Fcache-manager-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomerh2001%2Fcache-manager-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomerh2001%2Fcache-manager-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomerh2001","download_url":"https://codeload.github.com/tomerh2001/cache-manager-function/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230507447,"owners_count":18237005,"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","cache-control","cache-manager","decorators","function-cache","functions","nodejs","redis","typescript"],"created_at":"2024-09-24T20:49:14.640Z","updated_at":"2024-12-19T22:26:10.381Z","avatar_url":"https://github.com/tomerh2001.png","language":"TypeScript","readme":"# cache-manager-function\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![XO code style](https://shields.io/badge/code_style-5ed9c7?logo=xo\u0026labelColor=gray)](https://github.com/xojs/xo)\n[![Snyk Security](../../actions/workflows/snyk-security.yml/badge.svg)](../../actions/workflows/snyk-security.yml)\n[![CodeQL](../../actions/workflows/codeql.yml/badge.svg)](../../actions/workflows/codeql.yml)\n[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/tomerh2001/semantic-release-repo-template/badge)](https://securityscorecards.dev/viewer/?uri=github.com/tomerh2001/semantic-release-repo-template)\n\nCache functions dynamically based on their arguments using [cache-manager](https://github.com/jaredwray/cacheable).\n\n## Installation\n\nInstall the package using npm:\n\n```bash\nnpm install cache-manager-function\n```\n\nor with Yarn:\n\n```bash\nyarn add cache-manager-function\n```\n\n## Usage\n\n### 1. Initialize\n\nCall `getOrInitializeCache` to manually initialize the cache.\n\n```typescript\nconst cache = await getOrInitializeCache\u003cRedisStore\u003e({\n  store: await redisStore({\n    host: 'localhost',\n    port: 6379,\n  }),\n});\n```\n\nAlternatively, you can initialize the cache implicitly by providing the store directly to `cachedFunction`.\n\n```typescript\nconst multiply = (x, y) =\u003e x * y;\n\nconst cachedMultiply = cachedFunction(multiply, {\n  store: await redisStore({\n    host: 'localhost',\n    port: 6379,\n  }),\n});\n\n// Initializes the cache and caches the result\nawait cachedMultiply(2, 3); \n```\n\n### 2. Caching with `cachedFunction`\n\nThe `selector` option specifies which arguments should be used to generate the cache key.\n\n```typescript\ntype Person = { name: string; lastname: string };\n\nconst greet = (person: Person) =\u003e `Hello, ${person.name} ${person.lastname}!`;\n\n// Caches based on `person.name` and `person.lastname`\nconst cachedGreet = cachedFunction(greet, {\n  selector: ['0.name', '0.lastname']\n});\n\nawait cachedGreet({ person: { name: `John`, lastname: `Doe` } }); // Caches the result based on name=John and lastname=Doe\nawait cachedGreet({ person: { name: `Jane`, lastname: `Doe` } }); // Caches the result based on name=Jane and lastname=Doe\n```\n\n### 3. Using the `CacheOptions` decorator\n\n`CacheOptions` receives the exact same options that `cachedFunction` receives. It’s an alternative way to define the cache behavior directly on the function.\n\n```typescript\nimport { CacheOptions, cachedFunction } from `cache-manager-function`;\n\nclass UserService {\n  @CacheOptions(['0'], 300) // Specifies to cache based on the first argument (id), with a TTL of 300ms\n  async getUser(id: number) {\n    return `User with ID: ${id}`;\n  }\n}\n\nconst service = new UserService();\nconst cachedFetchUser = cachedFunction(service.getUser);\n\nawait userService.getUser(1); // Executes and caches based on the `id` argument\nawait userService.getUser(1); // Returns the cached result\n```\n\n## API\n\n### getOrInitializeCache\n\nInitializes or retrieves the cache.\n\n- **Parameters:**\n  - `options` (Optional): Configuration options to initialize the cache.\n\n- **Returns:** A promise that resolves to the cache instance.\n\n### cachedFunction\n\nCaches the result of a function based on its arguments.\n\n- **Parameters:**\n  - `function_`: The function to cache.\n  - `options` (Optional): Configuration options for caching.\n\n- **Returns:** A cached function that returns a promise with the result.\n\n### CacheOptions\n\nDecorator to cache function results based on selected arguments.\n\n- **Parameters:**\n  - `selector`: Paths of the arguments to include in the cache key.\n  - `ttl` (Optional): Time-to-live for the cached result.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomerh2001%2Fcache-manager-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomerh2001%2Fcache-manager-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomerh2001%2Fcache-manager-function/lists"}