{"id":14989633,"url":"https://github.com/afrith/koa-context-cache","last_synced_at":"2026-01-31T23:08:06.052Z","repository":{"id":35111213,"uuid":"208007590","full_name":"afrith/koa-context-cache","owner":"afrith","description":"Koa middleware that caches a field in the context object","archived":false,"fork":false,"pushed_at":"2023-04-18T17:07:22.000Z","size":602,"stargazers_count":0,"open_issues_count":15,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-26T16:43:50.692Z","etag":null,"topics":["cache","context","koa","koa2","koajs","middleware"],"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/afrith.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":"2019-09-12T08:59:24.000Z","updated_at":"2023-04-18T17:07:27.000Z","dependencies_parsed_at":"2024-09-15T16:05:19.044Z","dependency_job_id":"53ba2495-e95d-425b-aef1-00dff3539f4c","html_url":"https://github.com/afrith/koa-context-cache","commit_stats":{"total_commits":4,"total_committers":2,"mean_commits":2.0,"dds":0.25,"last_synced_commit":"ae8283b1dc41541764e4ced0035f39eac8dce1e3"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afrith%2Fkoa-context-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afrith%2Fkoa-context-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afrith%2Fkoa-context-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afrith%2Fkoa-context-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/afrith","download_url":"https://codeload.github.com/afrith/koa-context-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244875046,"owners_count":20524591,"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","context","koa","koa2","koajs","middleware"],"created_at":"2024-09-24T14:18:40.823Z","updated_at":"2026-01-31T23:08:01.013Z","avatar_url":"https://github.com/afrith.png","language":"JavaScript","readme":"# koa-context-cache\n\nMiddleware for [koa](https://koajs.com/) that caches values applied to the context state by other middleware.\n\n## Example use case\n\nYou have an API endpoint and want to control access to that endpoint based on the `Authorization` header. To implement thhis, you have a middleware `ensureAuth` which checks the header and stores the result in `ctx.state`, something like the following.\n\n```js\nconst ensureAuth = async (ctx, next) =\u003e {\n  const authToken = ctx.headers.authorization\n  if (!authToken) return ctx.throw(401) // Unauthorized\n  ctx.state.userinfo = await getUserFromToken(authToken)\n  if (!ctx.state.userinfo.isAuthorized) return ctx.throw(403) // Forbidden\n  await next()\n}\n\nrouter.get('/protected-endpoint', ensureAuth, async ctx =\u003e {\n  // ...\n})\n```\n\nHowever, that `getUserFromToken` function is slow or expensive - it has to make a request to another service, or to a database. Clients might be hitting the API frequently, so you want to cache the value of `ctx.state.userinfo` for a short period for each token.\n\n\u003e Sidenote: in this contrived example you could just wrap a cache around `getUserFromToken`. But let's say `ensureAuth` is actually from a third-party library and you don't want to hack around in its internals.\n\nThis package allows you to wrap `ensureAuth` in a cache in the following way:\n\n```js\nimport koaContextCache from 'koa-context-cache'\n\nconst cachedAuth = koaContextCache({\n  middleware: ensureAuth,\n  getKeyFromContext: ctx =\u003e ctx.headers.Authorization,\n  contextPropName: 'userinfo',\n  ttl: 60 // seconds\n})\n\nrouter.get('/protected-endpoint', cachedAuth, async ctx =\u003e {\n  // ...\n})\n```\n\nWhen the first request comes in with a particular `Authorization` header, the `ensureAuth` middleware will be applied and when the request completes the resulting value of `ctx.state.userinfo` will be stored in a cache.\n\nAny subsequent requests (within the TTL) that present the same `Authorization` header will have the cached value added to the context as `ctx.state.userinfo` without the `ensureAuth` middleware running.\n\n## API\n\nThe module exports a function which returns a middleware. The function accepts an `options` object with the following properties. See the example code above.\n\n* `getKeyFromContext`: a function that derives the cache key from the context. It will be passed the Koa context and should return a String.\n* `middleware`: middleware which will be applied if there is no cached value for the given key.\n* `contextPropName`: the name of the property of `ctx.state` which is to be cached; i.e. what is cached is the value of `ctx.state[contextPropName]`.\n* `ttl`: time-to-live of the cache, in seconds.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafrith%2Fkoa-context-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fafrith%2Fkoa-context-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafrith%2Fkoa-context-cache/lists"}