{"id":15797659,"url":"https://github.com/ehacke/transparent-cache","last_synced_at":"2025-08-14T08:06:54.881Z","repository":{"id":44208064,"uuid":"280932041","full_name":"ehacke/transparent-cache","owner":"ehacke","description":"Simple transparent caching for Node. Wrap a function and then call it like normal","archived":false,"fork":false,"pushed_at":"2023-01-07T20:18:19.000Z","size":1579,"stargazers_count":5,"open_issues_count":11,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-08-08T15:49:32.684Z","etag":null,"topics":["caching","redis"],"latest_commit_sha":null,"homepage":"https://asserted.io/posts/transparent-caching-wrapper-for-node","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/ehacke.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":"2020-07-19T19:04:51.000Z","updated_at":"2022-03-01T02:59:19.000Z","dependencies_parsed_at":"2023-02-08T00:31:47.606Z","dependency_job_id":null,"html_url":"https://github.com/ehacke/transparent-cache","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/ehacke/transparent-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehacke%2Ftransparent-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehacke%2Ftransparent-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehacke%2Ftransparent-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehacke%2Ftransparent-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ehacke","download_url":"https://codeload.github.com/ehacke/transparent-cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehacke%2Ftransparent-cache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270385467,"owners_count":24574556,"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-14T02:00:10.309Z","response_time":75,"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":["caching","redis"],"created_at":"2024-10-05T00:10:26.556Z","updated_at":"2025-08-14T08:06:54.821Z","avatar_url":"https://github.com/ehacke.png","language":"TypeScript","readme":"# transparent-cache\n\n![npm](https://img.shields.io/npm/v/@ehacke/transparent-cache)\n![CircleCI](https://img.shields.io/circleci/build/github/ehacke/transparent-cache)\n![GitHub](https://img.shields.io/github/license/ehacke/transparent-cache)\n![Codecov](https://img.shields.io/codecov/c/gh/ehacke/transparent-cache)\n\nSimple transparent caching for Node. Wrap a function and then call it like normal.\n\n## Sponsor \n\n![asserted.io](https://raw.githubusercontent.com/ehacke/transparent-cache/master/images/logo.png)\n\n[asserted.io - Test in Prod](https://asserted.io)\n\n## Features\n\n- The cache is periodically updated in the background without blocking the primary call. So it's always fast.\n- Just wrap any function and it becomes cached\n- Includes both local LRU cache and Redis\n\n## Install\n\nThis has a peer-dependency on [ioredis](https://github.com/luin/ioredis)\n\n```bash\nnpm i -S @ehacke/transparent-cache ioredis\n``` \n\n## Use\n\nIn the most basic case, you can just supply the redis config, and then wrap the function.\n\nThe `redisConfig` option is passed directly to `ioredis` and has the same properties as its [constructor](https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options).\n\n```javascript\nimport { TransparentCache } from '@ehacke/transparent-cache';\n\nconst transparentCache = new TransparentCache({ redisConfig: { host: 'localhost', port: 6379 } });\n\nfunction someSlowAndExpensiveFunction(someCoolArg) {\n  // ... do things you want to cache\n}\n\n// By default, the wrapped version will cache results for 30 sec locally and 10 minutes in Redis\nconst wrappedVersion = transparentCache.wrap(someSlowAndExpensiveFunction);\n\n// No cache yet, call original function. Still slow.\nlet result = await wrappedVersion('foo');\n\n// Immediately call again...\n\n// Returns value from local cache for arg 'foo'\nresult = await wrappedVersion('foo'); \n\n// .... waiting two minutes ....\n\n// Nothing in the local cache, so it goes to Redis\nresult = await wrappedVersion('foo');\n\n// ... waiting 15 minutes ...\n\n// Nothing in local or Redis, so it calls the original function\nresult = await wrappedVersion('foo');\n\n// Clear caches locally and remotely for 'foo'\n// NOTE: This doesn't clear local caches on other nodes\nawait wrappedVersion.delete('foo');\n\n// Hits original again because caches were cleared\nresult = await wrappedVersion('foo');\n```\n\n## API\n\n### Constructor\n\nDuring construction, the default values for each `wrap` call are set.\n\nThe local and remote properties are used as the default for each `wrap` call, but can be overridden.\n\n```typescript\ninterface ConstructorInterface {\n  redis?: Redis;                // Instead of providing the config below, you can provide a constructed IORedis instance\n  redisConfig?: RedisOptions;   // Same as: https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options\n                                // Defaults to: { keyPrefix: 'trans-cache-', enableOfflineQueue: false }\n                                //    This namespaces the keys, and ensures the redis calls fail fast if disconnected\n  local?: {\n    size?: number;              // Defaults to 1000\n    ttlMs?: number;             // Defaults to 30 seconds\n  };\n  remote?: {\n    size?: number;              // Defaults to 10000\n    ttlMs?: number;             // Defaults to 10 minutes\n    commandTimeoutMs?: number;  // Redis command timeout. Defaults to 50 ms\n  };\n}\n```\n\n### Wrap\n\n```typescript\nimport { TransparentCache } from '@ehacke/transparent-cache';\n\nconst transparentCache = new TransparentCache({ redisConfig: { host: 'localhost', port: 6379 } });\n\ntransparentCache.wrap\u003cTypeOfReturn\u003e(\n  functionToWrap,                 // Any function. Promises are awaited\n  overrideConfig: {               // Defaults to constructor values \n    local?: {\n      size?: number;              // Defaults to 1000\n      ttlMs?: number;             // Defaults to 30 seconds\n    };\n    remote?: {\n      size?: number;              // Defaults to 10000\n      ttlMs?: number;             // Defaults to 10 minutes\n      commandTimeoutMs?: number;  // Redis command timeout. Defaults to 50 ms\n    };\n    waitForRefresh?: boolean;     // Normally cache refreshes are done in the background. This forces them to block.\n  }, \n  functionId,                     // Used as part of the cache key, defaults to the function name if present\n  that,                           // Reference to 'this' for the wrapped function, used with .apply()\n)\n```\n\nThe wrapped version of the function also exposes a `.delete()` property that can be called to wipe the cache for a specific set of args.\n\n```typescript\n// Clear caches locally and remotely for 'foo'\n// NOTE: This doesn't clear local caches on other nodes\nawait wrappedVersion.delete('foo');\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehacke%2Ftransparent-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fehacke%2Ftransparent-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehacke%2Ftransparent-cache/lists"}