{"id":19405206,"url":"https://github.com/outofsyncstudios/request-utils-response-cache","last_synced_at":"2025-02-25T00:45:52.887Z","repository":{"id":32953147,"uuid":"123977232","full_name":"OutOfSyncStudios/request-utils-response-cache","owner":"OutOfSyncStudios","description":"A basic, ExpressJS response cache using Redis or MemoryCache","archived":false,"fork":false,"pushed_at":"2022-12-30T18:56:37.000Z","size":556,"stargazers_count":0,"open_issues_count":15,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-09T07:48:43.800Z","etag":null,"topics":["cache","caching","http","nodejs","npm","open-source"],"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/OutOfSyncStudios.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":"2018-03-05T20:51:12.000Z","updated_at":"2021-07-04T04:20:45.000Z","dependencies_parsed_at":"2023-01-14T22:48:49.146Z","dependency_job_id":null,"html_url":"https://github.com/OutOfSyncStudios/request-utils-response-cache","commit_stats":null,"previous_names":["mediaxpost/request-utils-response-cache"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OutOfSyncStudios%2Frequest-utils-response-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OutOfSyncStudios%2Frequest-utils-response-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OutOfSyncStudios%2Frequest-utils-response-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OutOfSyncStudios%2Frequest-utils-response-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OutOfSyncStudios","download_url":"https://codeload.github.com/OutOfSyncStudios/request-utils-response-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240582044,"owners_count":19824145,"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","caching","http","nodejs","npm","open-source"],"created_at":"2024-11-10T11:37:26.385Z","updated_at":"2025-02-25T00:45:52.482Z","avatar_url":"https://github.com/OutOfSyncStudios.png","language":"JavaScript","readme":"# request-utils-response-cache\n\n[![NPM](https://nodei.co/npm/@outofsync/request-utils-response-cache.png?downloads=true)](https://nodei.co/npm/@outofsync/request-utils-response-cache/)\n\n![Version](http://img.shields.io/npm/v/@outofsync/request-utils-response-cache.svg)\n![Downloads](http://img.shields.io/npm/dt/@outofsync/request-utils-response-cache.svg)\n[![Build Status](https://travis-ci.org/OutOfSyncStudios/request-utils-response-cache.svg)](https://travis-ci.org/OutOfSyncStudios/request-utils-response-cache)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/1ec9acec98c943d4871bf14c9b5ec9c1)](https://www.codacy.com/manual/OutOfSyncStudios/request-utils-response-cache?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=OutOfSyncStudios/request-utils-response-cache\u0026amp;utm_campaign=Badge_Grade)\n[![Codacy Coverage Badge](https://api.codacy.com/project/badge/Coverage/1ec9acec98c943d4871bf14c9b5ec9c1)](https://www.codacy.com/manual/OutOfSyncStudios/request-utils-response-cache?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=OutOfSyncStudios/request-utils-response-cache\u0026amp;utm_campaign=Badge_Coverage)\n[![Dependencies](https://david-dm.org/OutOfSyncStudios/request-utils-response-cache/status.svg)](https://david-dm.org/OutOfSyncStudios/request-utils-response-cache)\n\n`request-utils-response-cache` is an inline, response caching mechanism for [ExpressJS](https://www.npmjs.com/package/express) and [request-utils](https://www.npmjs.com/package/@outofsync/request-utils) which uses a connected [ObjectKeyCache](https://www.npmjs.com/package/@outofsync/object-key-cache) or [Redis](https://www.npmjs.com/package/redis).\n\nResponse Caching is highly recommended for any client facing Express application or APIs that are build on top of Express that may be under even the most modest of loads.\n\nA response's `res.locals` data are cached based on the following HTTP Request criteria:\n\n * HTTP Method (optional)\n * Request URL\n * HTTP Headers (optional)\n * HTTP Query Params (optional)\n * HTTP Form Body Params\n * Express Parameter value (`req.params`)\n\n Any `etag`, `if-match`, `if-none-match`, `if-modified-since`, or `if-unmodified-since` headers are stripped from the request before checking against the cache for a matching request. Additionally, if the header `cache-control` set to `no-cache` is passed in the request, then the cache checking is skipped.\n\nIf two requests are made with the same criteria, then the second request will be served from cache. By default, responses are cached on a 5-minute fixed window based on the timestamp of the initial cached response. After the timeframe has elapsed, the response is fully handled and the results can be cached again.\n\nIf there are any unexpected errors during the cache retrieval process, then the process fails silently and the request is handled as if it were not cached.\n\nIf additional manipulation of the request is desired then it is possible to provide an `onCacheMiss(req, res)` and `onCacheHit(req, res, data)` to the configuration of the Response Cache\n\n# [Installation](#installation)\n\u003ca name=\"installation\"\u003e\u003c/a\u003e\n\n```shell\nnpm install @outofsync/request-utils-response-cache\n```\n\n# [Usage](#usage)\n\u003ca name=\"usage\"\u003e\u003c/a\u003e\n\n```js\nconst ResponseCache = require('@outofsync/request-utils-response-cache');\nlet responseCache = new ResponseCache('responses', {\n  expire: 300000, // Five minutes\n  onCacheHit: ((req, res, data) =\u003e {\n    res.set('Content-Type', 'application/json');\n  })\n});\n\nfunction sendResponse(req, res, next) {\n  if (!res.headersSent) {\n    res.set('Content-Type', 'application/json');\n    for (const header in res.locals.headers) {\n      if (res.locals.headers.hasOwnProperty(header)) {\n        res.header(header, res.locals.headers[header]);\n      }\n    }\n    res.status(res.locals.status);\n    res.json(__.omit(res.locals.body, ['cacheExpiration']));\n  }\n  next();\n}\n\n// Later within the expressJS request stack\n// Before other processing, check cache\napp.use(responseCache.handler);\n\n// Do other processing\n// app.use...\n\n// After other processing\napp.use(responseCache.store); // This only stores when the req.needsCache is set\n\n// Process the res.locals and send response\napp.use(sendResponse);\n```\n\n\u003ca name=\"api\"\u003e\u003c/a\u003e\n# [API Reference](#api)\n\n## constructor(cacheNamespace [, config] [, cache] [, log])\nCreate a new ResponseCache with the passed `cacheNamespace`, [`config`](#config-object), [`cache`](#cache-object), and [`log`](#logging-object).  A `cacheNamespace` is required to scope the Response Cache to scope other values which may be in use within the cache.\n\n## handler(req, res, next)\nAn ExpressJS handler to check the current request against cache. If the cache\nexists, then it is retrieved and placed in `res.locals` and sets `req.usedCache`\nto true. If the cache does not exist and the request should be cached, then this\nsets the `req.needsCache` to true. This should occur early in the ExpressJS stack.\n\n```js\n  app.use(responseCache.handler);\n```\n\n## store(req, res, next)\nAn ExpressJS handler to store the current request when the `handler` indicates that\nthe current request is not cached by the `req.needsCache`. This should occur just\nbefore the response is sent in the ExpressJS stack.\n\n```js\n  app.use(responseCache.handler);\n```\n\n\u003ca name=\"appendix\"\u003e\u003c/a\u003e\n# [Appendix](#appendix)\n\n\u003ca name=\"config-object\"\u003e\u003c/a\u003e\n## [Configuration Object](#config-object)\n\nThe configuration parameter expects and object that contains the following (with defaults provided below):\n```js\n{\n  expire: 300000 // every 5 minute window (in mSec)\n  ignoreHeaders: false,\n  ignoreMethod: false,\n  ignoreQuery: false,\n  onCacheHit: (req, res, data) =\u003e {\n\n  },\n  onCacheMiss: (req, res) =\u003e {\n\n  }\n}\n```\n\n|parameter|type|description|\n|---------|----|-----------|\n|**`expire`**|Integer|Number of milliseconds for the fixed window for the initial cache.|\n|**`onCacheHit`**|Function(req, res, data) or `null`|A function accepting a HTTPRequest, a HTTPResponse, object data. When a request hits the cache then this function is called so additional data processing can occur.|\n|**`onCacheMiss`**|Function(req, res) or `null`|A function accepting a HTTPRequest, a HTTPResponse. When a request misses the cache then this function is called so additional data processing can occur.|\n|**`ignoreHeaders`**|Boolean|Skips the request headers when calculating the cache key|\n|**`ignoreMethod`**|Boolean|Skips the request method when calculating the cache key|\n|**`ignoreQuery`**|Boolean|Skips the query parameters when calculating the cache key|\n\n\u003ca name=\"cache-object\"\u003e\u003c/a\u003e\n## [Cache Object](#cache-object)\nThe Cache object can be a active and [promisified Redis](https://www.npmjs.com/package/redis#promises) connect, or an active [ObjectKeyCache](https://www.npmjs.com/package/@outofsync/object-key-cache). If no value is set, then the response cache will create an internal Object Key Cache and use it.\n\n\u003ca name=\"logging-object\"\u003e\u003c/a\u003e\n## [Logging Object](#logging-object)\nThe Logging object is an instance of any logging library, such as [Winston](https://www.npmjs.com/package/winston) or [Bunyan](https://www.npmjs.com/package/bunyan), which support the `.error(...)`, `.info(...)`, `.debug(...)`, and `.log(...)` methods. If this is not provided, then any debug or error messages are sent to `/dev/null` through the use of [`LogStub`](https://www.npmjs.com/package/logstub).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foutofsyncstudios%2Frequest-utils-response-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foutofsyncstudios%2Frequest-utils-response-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foutofsyncstudios%2Frequest-utils-response-cache/lists"}