{"id":13525928,"url":"https://github.com/fastify/fastify-caching","last_synced_at":"2025-04-14T08:53:58.791Z","repository":{"id":25690935,"uuid":"104666439","full_name":"fastify/fastify-caching","owner":"fastify","description":"A Fastify plugin to facilitate working with cache headers","archived":false,"fork":false,"pushed_at":"2025-03-30T21:23:24.000Z","size":176,"stargazers_count":217,"open_issues_count":0,"forks_count":26,"subscribers_count":17,"default_branch":"main","last_synced_at":"2025-04-07T02:06:39.061Z","etag":null,"topics":["fastify","fastify-plugin"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@fastify/caching","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/fastify.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},"funding":{"github":"fastify","open_collective":"fastify"}},"created_at":"2017-09-24T18:23:28.000Z","updated_at":"2025-04-02T17:56:44.000Z","dependencies_parsed_at":"2023-12-07T17:25:36.251Z","dependency_job_id":"fb572917-4873-4de5-a219-8a9056c5fca2","html_url":"https://github.com/fastify/fastify-caching","commit_stats":{"total_commits":171,"total_committers":34,"mean_commits":5.029411764705882,"dds":0.7543859649122807,"last_synced_commit":"b26b48ab8fabf6dd6879617dd9df42da94e076de"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-caching","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-caching/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-caching/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-caching/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastify","download_url":"https://codeload.github.com/fastify/fastify-caching/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248852109,"owners_count":21171839,"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":["fastify","fastify-plugin"],"created_at":"2024-08-01T06:01:23.596Z","updated_at":"2025-04-14T08:53:58.770Z","avatar_url":"https://github.com/fastify.png","language":"JavaScript","readme":"# @fastify/caching\n\n[![CI](https://github.com/fastify/fastify-caching/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-caching/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/caching.svg?style=flat)](https://www.npmjs.com/package/@fastify/caching)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\n*@fastify/caching* is a plugin for the [Fastify](http://fastify.dev/) framework\nthat provides server-side caching and mechanisms for manipulating HTTP cache headers according to\n[RFC 2616 §14.9](https://tools.ietf.org/html/rfc2616#section-14.9).\n\nThis plugin fully supports Fastify's encapsulation. Therefore, routes that\nshould have differing cache settings should be registered within different\ncontexts.\n\nIn addition to providing header manipulation, the plugin also decorates the\nserver instance with an object that can be used for caching items. **Note:**\nthe default cache should not be used in a \"production\" environment. It is\nan LRU, in-memory cache that is capped at 100,000 items. It is *highly*\nrecommended that a full-featured cache object be supplied, e.g.\n[abstract-cache-redis][acache-redis].\n\n[acache-redis]: https://www.npmjs.com/package/abstract-cache-redis\n\n## Install\n```\nnpm i @fastify/caching\n```\n\n### Compatibility\n| Plugin version | Fastify version |\n| ---------------|-----------------|\n| `\u003e=9.x`        | `^5.x`          |\n| `^8.x`         | `^4.x`          |\n| `\u003e=6.x \u003c8.x`   | `^3.x`          |\n| `\u003e=4.x \u003c6.x`   | `^2.x`          |\n| `\u003e=1.x \u003c4.x`   | `^1.x`          |\n\n\nPlease note that if a Fastify version is out of support, then so are the corresponding versions of this plugin\nin the table above.\nSee [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.\n\n## Example\n\nThis example shows using the plugin to disable client-side caching of all\nroutes.\n\n```js\nconst http = require('node:http')\nconst fastify = require('fastify')()\nconst fastifyCaching = require('@fastify/caching')\n\nfastify.register(\n  fastifyCaching,\n  {privacy: fastifyCaching.privacy.NOCACHE},\n  (err) =\u003e { if (err) throw err }\n)\n\nfastify.get('/', (req, reply) =\u003e {\n  reply.send({hello: 'world'})\n})\n\nfastify.listen({ port: 3000 }, (err) =\u003e {\n  if (err) throw err\n\n  http.get('http://127.0.0.1:3000/', (res) =\u003e {\n    console.log(res.headers['cache-control'])\n  })\n})\n```\n\nThis example shows how to register the plugin such that it only provides\na server-local cache. It will not set any cache control headers on responses.\nIt also shows how to retain a reference to the cache object so that it can\nbe re-used.\n\n```js\nconst IORedis = require('ioredis')\nconst redis = new IORedis({host: '127.0.0.1'})\nconst abcache = require('abstract-cache')({\n  useAwait: false,\n  driver: {\n    name: 'abstract-cache-redis', // must be installed via `npm i`\n    options: {client: redis}\n  }\n})\n\nconst fastify = require('fastify')()\nfastify\n  .register(require('@fastify/redis'), {client: redis})\n  .register(require('@fastify/caching'), {cache: abcache})\n\nfastify.get('/', (req, reply) =\u003e {\n  fastify.cache.set('hello', {hello: 'world'}, 10000, (err) =\u003e {\n    if (err) return reply.send(err)\n    reply.send({hello: 'world'})\n  })\n})\n\nfastify.listen({ port: 3000 }, (err) =\u003e {\n  if (err) throw err\n})\n```\n\n## API\n\n### Options\n\n*@fastify/caching* accepts the options object:\n\n```js\n{\n  privacy: 'value',\n  expiresIn: 300,\n  cache: {get, set},\n  cacheSegment: 'segment-name'\n}\n```\n\n+ `privacy` (Default: `undefined`): can be set to any string that is valid\nfor a *cache-response-directive* as defined by RFC 2616.\n+ `expiresIn` (Default: `undefined`): a value, in seconds, for the *max-age* the\nresource may be cached. When this is set, and `privacy` is not set to `no-cache`,\nthen `', max-age=\u003cvalue\u003e'` will be appended to the `cache-control` header.\n+ `cache` (Default: `abstract-cache.memclient`): an [abstract-cache][acache]\nprotocol-compliant cache object. Note: the plugin requires a cache instance to\nproperly support the ETag mechanism. Therefore, if a falsy value is supplied\nthe default will be used.\n+ `cacheSegment` (Default: `'@fastify/caching'`): segment identifier to use when\ncommunicating with the cache.\n+ `serverExpiresIn` (Default: `undefined`): a value, in seconds, for the length of time the resource is fresh and may be held in a shared cache (e.g. a CDN). Shared caches will ignore max-age when this is specified, though browsers will continue to use max-age. Should be used with expiresIn, not in place of it. When this is set, and `privacy` is set to `public`,  then `', s-maxage=\u003cvalue\u003e'` will be appended to the `cache-control` header.\n\n[acache]: https://www.npmjs.com/package/abstract-cache\n\n### `reply.etag(string, number)`\n\nThis method allows setting of the `etag` header. It accepts any arbitrary\nstring. Be sure to supply a string that is valid for HTTP headers.\n\nIf a tag string is not supplied then [uid-safe][uid-safe] will be used to\ngenerate a tag. This operation will be performed ***synchronously***. It is\nrecommended to always supply a value to this method to avoid this operation.\n\nAll incoming requests to paths affected by this plugin will be inspected for\nthe `if-none-match` header. If the header is present, the value will be used\nto lookup the tag within the cache associated with this plugin. If the tag is\nfound, then the response will end with a 304 status code sent to\nthe client.\n\nTags will be cached according to the second parameter. If the second parameter\nis supplied, and it is an integer, then it will be used as the time to cache\nthe etag for generating 304 responses. The time must be specified in\nmilliseconds. The default lifetime, when the parameter is not specified, is\n`3600000`.\n\n[uid-safe]: https://www.npmjs.com/package/uid-safe\n\n### `reply.expires(date)`\n\nThis method allows setting of the `expires` header. It accepts a regular `Date`\nobject or a string that is a valid date string according to\n[RFC 2616 §14.21][sec14.21].\n\n[sec14.21]: https://tools.ietf.org/html/rfc2616#section-14.21\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","funding_links":["https://github.com/sponsors/fastify","https://opencollective.com/fastify"],"categories":["JavaScript","\u003ch2 align=\"center\"\u003eAwesome Fastify\u003c/h2\u003e"],"sub_categories":["\u003ch2 align=\"center\"\u003eEcosystem\u003c/h2\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-caching","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastify%2Ffastify-caching","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-caching/lists"}