{"id":13779142,"url":"https://github.com/koajs/cash","last_synced_at":"2025-05-15T23:07:07.899Z","repository":{"id":17573744,"uuid":"20377175","full_name":"koajs/cash","owner":"koajs","description":"HTTP response caching for Koa. Supports Redis, in-memory store, and more!","archived":false,"fork":false,"pushed_at":"2025-02-13T00:14:26.000Z","size":382,"stargazers_count":160,"open_issues_count":2,"forks_count":20,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-07T02:31:55.350Z","etag":null,"topics":["amazon","aws","cache","caching","cdn","content","delivery","javascript","koa","memory","network","node","redis","s3","session","sessions","space","static","storage","store"],"latest_commit_sha":null,"homepage":"","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/koajs.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,"zenodo":null}},"created_at":"2014-06-01T12:42:11.000Z","updated_at":"2025-04-27T11:37:10.000Z","dependencies_parsed_at":"2025-04-12T17:38:15.921Z","dependency_job_id":"32551657-a258-44ca-b56a-d065d5803351","html_url":"https://github.com/koajs/cash","commit_stats":{"total_commits":106,"total_committers":14,"mean_commits":7.571428571428571,"dds":0.7547169811320755,"last_synced_commit":"a847126d290ced01d131797c660ab7f00faa4bb8"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fcash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fcash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fcash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fcash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/cash/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254436948,"owners_count":22070947,"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":["amazon","aws","cache","caching","cdn","content","delivery","javascript","koa","memory","network","node","redis","s3","session","sessions","space","static","storage","store"],"created_at":"2024-08-03T18:01:01.604Z","updated_at":"2025-05-15T23:07:02.357Z","avatar_url":"https://github.com/koajs.png","language":"JavaScript","readme":"# koa-cash\n\n[![build status](https://github.com/koajs/cash/actions/workflows/ci.yml/badge.svg)](https://github.com/koajs/cash/actions/workflows/ci.yml)\n[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)\n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)\n[![license](https://img.shields.io/github/license/koajs/cash.svg)](LICENSE)\n[![npm downloads](https://img.shields.io/npm/dt/koa-cash.svg)](https://npm.im/koa-cash)\n\n\u003e HTTP response caching for Koa.  Supports Redis, in-memory store, and more!\n\n\n## Table of Contents\n\n* [Features](#features)\n* [Install](#install)\n* [Usage](#usage)\n* [API](#api)\n  * [app.use(koaCash(options))](#appusekoacashoptions)\n  * [Max age (optional)](#max-age-optional)\n  * [CashClear](#cashclear)\n* [Notes](#notes)\n* [Contributors](#contributors)\n* [License](#license)\n* [Links](#links)\n\n\n## Features\n\nCaches the response based on any arbitrary store you'd like.\n\n* Handles JSON and stream bodies\n* Handles gzip compression negotiation (if `options.compression` is set to `true` as of v4.0.0)\n* Handles 304 responses\n\n:tada: **Pairs great with [@ladjs/koa-cache-responses](https://github.com/ladjs/koa-cache-responses)** :tada:\n\n\n## Install\n\n[NPM](https://www.npmjs.com/)\n\n```sh\nnpm install koa-cash\n```\n\n\n## Usage\n\n```js\nimport LRU from 'lru-cache';\nimport koaCash from 'koa-cash';\n\n// ...\nconst cache = new LRU();\napp.use(koaCash({\n  get: (key) =\u003e {\n    return cache.get(key);\n  },\n  set(key, value) {\n    return cache.set(key, value);\n  },\n}))\n\napp.use(async ctx =\u003e {\n  // this response is already cashed if `true` is returned,\n  // so this middleware will automatically serve this response from cache\n  if (await ctx.cashed()) return;\n\n  // set the response body here,\n  // and the upstream middleware will automatically cache it\n  ctx.body = 'hello world!';\n});\n```\n\n\n## API\n\n### app.use(koaCash(options))\n\nOptions are:\n\n#### `maxAge`\n\nDefault max age (in milliseconds) for the cache if not set via `await ctx.cashed(maxAge)`.\n\n#### `threshold`\n\nMinimum byte size to compress response bodies. Default `1kb`.\n\n#### `compression`\n\nIf a truthy value is passed, then compression will be enabled.  This value is `false` by default.\n\n#### `setCachedHeader`\n\nIf a truthy value is passed, then `X-Cached-Response` header will be set as `HIT` when response is served from the cache.  This value is `false` by default.\n\n#### `methods`\n\nIf an object is passed, then add extra HTTP method caching. This value is empty by default. But `GET` and `HEAD` are enabled.\n\nEg: `{ POST: true }`\n\n#### `hash()`\n\nA hashing function. By default, it's:\n\n```js\nfunction hash(ctx) {\n return ctx.response.url; // same as ctx.url\n}\n```\n\n`ctx` is the Koa context and is also passed as an argument. By default, it caches based on the URL.\n\n#### `get()`\n\nGet a value from a store. Must return a Promise, which returns the cache's value, if any.\n\n```js\nfunction get(key, maxAge) {\n  return Promise;\n}\n```\n\nNote that all the `maxAge` stuff must be handled by you. This module makes no opinion about it.\n\n#### `set()`\n\nSet a value to a store. Must return a Promise.\n\n```js\nfunction set(key, value, maxAge) {\n  return Promise;\n}\n```\n\nNote: `maxAge` is set by `.cash = { maxAge }`. If it's not set, then `maxAge` will be `0`, which you should then ignore.\n\n#### Example\n\nUsing a library like [lru-cache](https://github.com/isaacs/node-lru-cache), though this would not quite work since it doesn't allow per-key expiration times.\n\n```js\nconst koaCash = require('koa-cash');\nconst LRU = require('lru-cache');\n\nconst cache = new LRU({\n  maxAge: 30000 // global max age\n})\n\napp.use(koaCash({\n  get (key, maxAge) {\n    return cache.get(key)\n  },\n  set (key, value) {\n    cache.set(key, value)\n  }\n}))\n```\n\nSee [@ladjs/koa-cache-responses](https://github.com/ladjs/koa-cache-responses) test folder more examples (e.g. Redis with `ioredis`).\n\n### Max age (optional)\n\n```js\nconst cached = await ctx.cashed(maxAge) // maxAge is passed to your caching strategy\n```\n\nThis is how you enable a route to be cached. If you don't call `await ctx.cashed()`, then this route will not be cached nor will it attempt to serve the request from the cache.\n\n`maxAge` is the max age passed to `get()`.\n\nIf `cached` is `true`, then the current request has been served from cache and **you should early `return`**. Otherwise, continue setting `ctx.body=` and this will cache the response.\n\n### CashClear\n\n```js\nctx.cashClear('/')\n```\n\nThis is a special method available on the ctx that you can use to clear the cache for a specific key.\n\n\n## Notes\n\n* Only `GET` and `HEAD` requests are cached. (Unless overridden)\n* Only `200` responses are cached. Don't set `304` status codes on these routes - this middleware will handle it for you\n* The underlying store should be able to handle `Date` objects as well as `Buffer` objects. Otherwise, you may have to serialize/deserialize yourself.\n\n\n## Contributors\n\n| Name             | Website                   |\n| ---------------- | ------------------------- |\n| **Jonathan Ong** | \u003chttp://jongleberry.com\u003e  |\n| **Nick Baugh**   | \u003chttp://niftylettuce.com\u003e |\n\n\n## License\n\n[MIT](LICENSE) © [Jonathan Ong](http://jongleberry.com)\n\n\n## Links\n\n* [NPM](https://www.npmjs.com/)\n","funding_links":[],"categories":["Middleware","JavaScript","仓库"],"sub_categories":["中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fcash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Fcash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fcash/lists"}