{"id":15626277,"url":"https://github.com/rileytomasek/tkv-cache","last_synced_at":"2025-08-23T06:03:49.108Z","repository":{"id":170783039,"uuid":"647024921","full_name":"rileytomasek/tkv-cache","owner":"rileytomasek","description":"Minimal Keyv powered caching helper","archived":false,"fork":false,"pushed_at":"2024-04-21T23:37:11.000Z","size":379,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-20T18:12:08.351Z","etag":null,"topics":["cache","keyv","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/rileytomasek.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-29T22:39:36.000Z","updated_at":"2024-04-21T23:37:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"04fced5f-74c7-4800-8a1d-f280013cadab","html_url":"https://github.com/rileytomasek/tkv-cache","commit_stats":null,"previous_names":["rileytomasek/tkv-cache"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/rileytomasek/tkv-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rileytomasek%2Ftkv-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rileytomasek%2Ftkv-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rileytomasek%2Ftkv-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rileytomasek%2Ftkv-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rileytomasek","download_url":"https://codeload.github.com/rileytomasek/tkv-cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rileytomasek%2Ftkv-cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271745659,"owners_count":24813515,"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","status":"online","status_checked_at":"2025-08-23T02:00:09.327Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","keyv","typescript"],"created_at":"2024-10-03T10:11:50.309Z","updated_at":"2025-08-23T06:03:49.085Z","avatar_url":"https://github.com/rileytomasek.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TypeScript KvCache\n\n[![Build Status](https://github.com/rileytomasek/tkv-cache/actions/workflows/main.yml/badge.svg)](https://github.com/rileytomasek/kvcache/actions/workflows/main.yml) [![npm version](https://img.shields.io/npm/v/tkv-cache.svg?color=0c0)](https://www.npmjs.com/package/tkv-cache)\n\nMinimal [Keyv](https://github.com/jaredwray/keyv) powered caching helper with added support for:\n\n- Objects as keys with key ordering and hashing\n- Function to normalize keys to optimize cache hit rate\n- Helper function for wrapping functions with caching\n- Strongly typed set/get with generic types\n\nAll storage is handled by the Keyv instance passed, so you can use the in-memory default, or any of the [storage adapters](https://github.com/jaredwray/keyv#storage-adapters).\n\n## Usage\n\n### Install\n\nInstall `tkv-cache` with your favorite package manager: npm, yarn, pnpm, etc.\n\nYou also need to install `keyv` and any adapters like `@keyv/redis`.\n\n### KvCache\n\nThe `KvCache` class wraps `Keyv` with support for object keys and key normalization.\n\n```ts\nimport Keyv from 'keyv';\nimport { KvCache } from 'tkv-cache';\n\n// Simple usage\nconst cache = new KvCache\u003cstring, string\u003e(new Keyv());\nawait cache.set('key', 'value');\nconst val = await cache.get('key'); // val is: string | null\n\n// Object as the key\ntype ObjKey = { name: string };\nconst cache2 = new KvCache\u003cObjKey, string\u003e(new Keyv());\nawait cache2.set({ name: 'key1' }, 'value');\nconst val2 = await cache2.get({ name: 'key1' }); // val2 is: string | null\n\n// With a custom normalizeKey function\nconst cache3 = new KvCache\u003cstring, string\u003e(\n  new Keyv(),\n  (key) =\u003e key.toUpperCase()\n);\nawait cache3.set('key', 'value');\nconst val3 = await cache3.get('KEY'); // val3 is: string | null\nconsole.log(val3); // 'value' because keys were normalized to 'KEY'\n```\n\n### Easy KvCache Creation\n\nYou can create a `KVCache` instance using the `createKvCache()` function by\npassing in a Keyv options object.\n\n```ts\nconst cache = createKvCache\u003cstring, string\u003e({\n  ttl: 1000,\n});\nawait cache.set('key', 'value');\nconst val = await cache.get('key');\ntype verify = Expect\u003cEqual\u003ctypeof val, string | null\u003e\u003e;\nassert.equal(val, 'value');\n```\n\n### Cachify\n\nThe `cachify` function wraps a function with a `KvCache` and optional key normalization function.\n\n```ts\nimport Keyv from 'keyv';\nimport { cachify } from 'tkv-cache';\n\n// The function to cache. Something slow/expensive like an OpenAI API call.\nconst func = (key: string) =\u003e Promise.resolve(`value for ${key}`);\n\n// The cached function\nconst cachedFunc = cachify({}, func);\n\nconst response = await cachedFunc('key1'); // API call\nconst response2 = await cachedFunc('key1'); // Cache hit\nconst response3 = await cachedFunc('key2'); // API call\n\n// Use key normalization to avoid cache misses\nconst cachedFunc2 = cachify(new Keyv(), func, (key) =\u003e key.toUpperCase());\n\nconst res = await cachedFunc('key1'); // API call\nconst res2 = await cachedFunc('key1'); // Cache hit\nconst res3 = await cachedFunc('KEY1'); // Cache hit (key normalization)\nconst res4 = await cachedFunc('Key1'); // Cache hit (key normalization)\nconst res5 = await cachedFunc('key2'); // API call\n```\n\n### Error Handling\n\n`KvCache` defaults to handling errors by calling `console.error` to prevent caching from breaking execution. You can pass a custom error handler to the `KvCache` constructor if you want to change the behavior.\n\n### Examples\n\nSee the [`KvCache` tests](/test/kv-cache.test.ts) and [`cachify` tests](/test/cachify.test.ts) for more example usage.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frileytomasek%2Ftkv-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frileytomasek%2Ftkv-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frileytomasek%2Ftkv-cache/lists"}