{"id":13472708,"url":"https://github.com/patrixr/strapi-middleware-cache","last_synced_at":"2025-03-26T17:31:06.022Z","repository":{"id":37910562,"uuid":"255584994","full_name":"patrixr/strapi-middleware-cache","owner":"patrixr","description":":electric_plug:  A cache middleware for https://strapi.io","archived":true,"fork":false,"pushed_at":"2023-07-25T04:29:38.000Z","size":4980,"stargazers_count":291,"open_issues_count":25,"forks_count":27,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-18T03:48:09.666Z","etag":null,"topics":["cache","deprecated","hacktoberfest","headless-cms","lru-cache","middleware","no-longer-maintained","plugin","redis","strapi","strapijs"],"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/patrixr.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-04-14T11:00:37.000Z","updated_at":"2025-03-13T19:40:38.000Z","dependencies_parsed_at":"2023-01-31T17:16:25.720Z","dependency_job_id":null,"html_url":"https://github.com/patrixr/strapi-middleware-cache","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrixr%2Fstrapi-middleware-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrixr%2Fstrapi-middleware-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrixr%2Fstrapi-middleware-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrixr%2Fstrapi-middleware-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patrixr","download_url":"https://codeload.github.com/patrixr/strapi-middleware-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245702204,"owners_count":20658562,"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","deprecated","hacktoberfest","headless-cms","lru-cache","middleware","no-longer-maintained","plugin","redis","strapi","strapijs"],"created_at":"2024-07-31T16:00:57.279Z","updated_at":"2025-03-26T17:31:05.637Z","avatar_url":"https://github.com/patrixr.png","language":"JavaScript","funding_links":[],"categories":["Strapi v3","\u003ca name='PluginProviders'\u003e\u003c/a\u003ePlugin \u0026 Providers","📦 Legacy \u0026 Inactive Projects"],"sub_categories":["Plugin \u0026 Providers - v3","\u003ca name='CommunityMiddlewares'\u003e\u003c/a\u003eCommunity Middlewares"],"readme":"# Strapi LRU Caching middleware - Legacy\n\nA cache middleware for the headless CMS strapi.io\n\n## NO LONGER MAINTAINED\n\nTHIS PROJECT IS DEPRECATED\n\n\u003e Since the release of Strapi v4, this project has been parked in favor of the new [Strapi Plugin Rest Cache](https://github.com/strapi-community/strapi-plugin-rest-cache) which was forked from this repository. Maintainers have now moved there, and we recommend you switch to the new and improved plugin.\n\u003e A big thank you to all those that contributed to this middleware, it has served many.\n\n## TOC\n\n**Strapi V4 users:** https://strapi-community.github.io/strapi-plugin-rest-cache/\n\n![](https://github.com/patrixr/strapi-middleware-cache/workflows/Tests/badge.svg)\n\n- [Strapi LRU Caching middleware - Legacy](#strapi-lru-caching-middleware---legacy)\n  - [NO LONGER MAINTAINED](#no-longer-maintained)\n  - [TOC](#toc)\n  - [How it works](#how-it-works)\n  - [Installing](#installing)\n  - [Version 1 compatibility](#version-1-compatibility)\n  - [Requirements](#requirements)\n  - [Setup](#setup)\n  - [Configure models](#configure-models)\n  - [Configure the storage engine](#configure-the-storage-engine)\n    - [Example](#example)\n      - [With Redis Sentinels](#with-redis-sentinels)\n      - [With Redis Cluster](#with-redis-cluster)\n  - [Per-Model Configuration](#per-model-configuration)\n  - [Single types](#single-types)\n  - [Authentication](#authentication)\n  - [Etag support](#etag-support)\n  - [Clearing related cache](#clearing-related-cache)\n  - [Cache entry point](#cache-entry-point)\n    - [Koa Context](#koa-context)\n    - [Strapi Middleware](#strapi-middleware)\n  - [Cache API](#cache-api)\n  - [Admin panel interactions](#admin-panel-interactions)\n\n## How it works\n\nThis middleware caches incoming `GET` requests on the strapi API, based on query params and model ID.\nThe cache is automatically busted everytime a `PUT`, `PATCH`, `POST`, or `DELETE` request comes in.\n\nSupported storage engines\n\n- Memory _(default)_\n- Redis\n\nImportant: Caching must be explicitely enabled **per model**\n\n## Installing\n\nUsing npm\n\n```bash\nnpm install --save strapi-middleware-cache\n```\n\nUsing yarn\n\n```bash\nyarn add strapi-middleware-cache\n```\n\n## Version 1 compatibility\n\n:warning: Important: The middleware has gone through a full rewrite since version 1, and its configuration may not be fully compatible with the old v1. Make sure to (re)read the documentation below on how to use it 👇\n\n## Requirements\n\nSince `2.0.1`:\n\n- strapi `3.4.0`\n- node `14`\n\n_See `1.5.0` for strapi \u003c `3.4.0`_\n\n## Setup\n\nFor Strapi stable versions, add a `middleware.js` file within your config folder\n\ne.g\n\n```bash\ntouch config/middleware.js\n```\n\nTo use different settings per environment, see the [Strapi docs for environments](https://strapi.io/documentation/v3.x/concepts/configurations.html#environments).\n\nYou can parse environment variables for the config here as well if you wish to, please see the [Strapi docs for environment variables](https://strapi.io/documentation/v3.x/concepts/configurations.html#environment-variables).\n\nEnable the cache middleware by adding the following snippet to an empty middleware file or simply add in the settings from the below example:\n\n```javascript\nmodule.exports = ({ env }) =\u003e ({\n  settings: {\n    cache: {\n      enabled: true,\n    },\n  },\n});\n```\n\nStarting the CMS should now log the following\n\n```\n$ strapi develop\n[2021-02-26T07:03:18.981Z] info [cache] Mounting LRU cache middleware\n[2021-02-26T07:03:18.982Z] info [cache] Storage engine: mem\n```\n\n## Configure models\n\nThe middleware will only cache models which have been explicitely enabled.\nAdd a list of models to enable to the module's configuration object.\n\ne.g\n\n```javascript\n// config/middleware.js\nmodule.exports = ({ env }) =\u003e ({\n  settings: {\n    cache: {\n      enabled: true,\n      models: ['review'],\n    },\n  },\n});\n```\n\nStarting the CMS should now log the following\n\n```\n$ strapi develop\n[2021-02-26T07:03:18.981Z] info [cache] Mounting LRU cache middleware\n[2021-02-26T07:03:18.982Z] info [cache] Storage engine: mem\n[2021-02-26T07:03:18.985Z] debug [cache] Register review routes middlewares\n[2021-02-26T07:03:18.986Z] debug [cache] POST /reviews purge\n[2021-02-26T07:03:18.987Z] debug [cache] DELETE /reviews/:id purge\n[2021-02-26T07:03:18.987Z] debug [cache] PUT /reviews/:id purge\n[2021-02-26T07:03:18.987Z] debug [cache] GET /reviews recv maxAge=3600000\n[2021-02-26T07:03:18.988Z] debug [cache] GET /reviews/:id recv maxAge=3600000\n[2021-02-26T07:03:18.988Z] debug [cache] GET /reviews/count recv maxAge=3600000\n```\n\n## Configure the storage engine\n\nThe module's configuration object supports the following properties\n\n### Example\n\n#### With Redis Sentinels\n\n```javascript\n// config/middleware.js\n\n/**\n * @typedef {import('strapi-middleware-cache').UserMiddlewareCacheConfig} UserMiddlewareCacheConfig\n */\n\nmodule.exports = ({ env }) =\u003e ({\n  settings: {\n    /**\n     * @type {UserMiddlewareCacheConfig}\n     */\n    cache: {\n      enabled: true,\n      type: 'redis',\n      models: ['review'],\n      redisConfig: {\n        sentinels: [\n          { host: '192.168.10.41', port: 26379 },\n          { host: '192.168.10.42', port: 26379 },\n          { host: '192.168.10.43', port: 26379 },\n        ],\n        name: 'redis-primary',\n      },\n    },\n  },\n});\n```\n\n#### With Redis Cluster\n\n```javascript\n// config/middleware.js\n\n/**\n * @typedef {import('strapi-middleware-cache').UserMiddlewareCacheConfig} UserMiddlewareCacheConfig\n */\n\nmodule.exports = ({ env }) =\u003e ({\n  settings: {\n    /**\n     * @type {UserMiddlewareCacheConfig}\n     */\n    cache: {\n      enabled: true,\n      type: 'redis',\n      models: ['review'],\n      redisConfig: {\n        cluster: [\n          { host: '192.168.10.41', port: 6379 },\n          { host: '192.168.10.42', port: 6379 },\n          { host: '192.168.10.43', port: 6379 },\n        ],\n      },\n    },\n  },\n});\n```\n\nRunning the CMS will output the following\n\n```\n$ strapi develop\n[2021-02-26T07:03:18.981Z] info [cache] Mounting LRU cache middleware\n[2021-02-26T07:03:18.982Z] info [cache] Storage engine: mem\n[2021-02-26T07:03:18.985Z] debug [cache] Register review routes middlewares\n[2021-02-26T07:03:18.986Z] debug [cache] POST /reviews purge\n[2021-02-26T07:03:18.987Z] debug [cache] DELETE /reviews/:id purge\n[2021-02-26T07:03:18.987Z] debug [cache] PUT /reviews/:id purge\n[2021-02-26T07:03:18.987Z] debug [cache] GET /reviews recv maxAge=3600000\n[2021-02-26T07:03:18.988Z] debug [cache] GET /reviews/:id recv maxAge=3600000\n[2021-02-26T07:03:18.988Z] debug [cache] GET /reviews/count recv maxAge=3600000\n```\n\n## Per-Model Configuration\n\nEach route can hold its own configuration object for more granular control. This can be done simply\nby replacing the model strings in the list by object.\n\nOn which you can set:\n\n- Its own custom `maxAge`\n- Headers to include in the cache-key (e.g the body may differ depending on the language requested)\n\ne.g\n\n```javascript\n// config/middleware.js\n\n/**\n * @typedef {import('strapi-middleware-cache').UserModelCacheConfig} UserModelCacheConfig\n */\n\nmodule.exports = ({ env }) =\u003e ({\n  settings: {\n    cache: {\n      enabled: true,\n      type: 'redis',\n      maxAge: 3600000,\n      models: [\n        /**\n         * @type {UserModelCacheConfig}\n         */\n        {\n          model: 'reviews',\n          headers: ['accept-language']\n          maxAge: 1000000,\n          routes: [\n            '/reviews/:slug',\n            '/reviews/:id/custom-route',\n            { path: '/reviews/:slug', method: 'DELETE' },\n          ]\n        },\n      ]\n    }\n  }\n});\n```\n\nRunning the CMS should now show the following\n\n```\n$ strapi develop\n[2021-02-26T07:03:18.981Z] info [cache] Mounting LRU cache middleware\n[2021-02-26T07:03:18.982Z] info [cache] Storage engine: mem\n[2021-02-26T07:03:18.985Z] debug [cache] Register review routes middlewares\n[2021-02-26T07:03:18.986Z] debug [cache] POST /reviews purge\n[2021-02-26T07:03:18.987Z] debug [cache] DELETE /reviews/:id purge\n[2021-02-26T07:03:18.987Z] debug [cache] PUT /reviews/:id purge\n[2021-02-26T07:03:18.987Z] debug [cache] GET /reviews recv maxAge=1000000 vary=accept-language\n[2021-02-26T07:03:18.988Z] debug [cache] GET /reviews/:id recv maxAge=1000000 vary=accept-language\n[2021-02-26T07:03:18.988Z] debug [cache] GET /reviews/count recv maxAge=1000000 vary=accept-language\n[2021-02-26T07:03:18.990Z] debug [cache] GET /reviews/:slug maxAge=1000000 vary=accept-language\n[2021-02-26T07:03:18.990Z] debug [cache] GET /reviews/:id/custom-route maxAge=1000000 vary=accept-language\n[2021-02-26T07:03:18.990Z] debug [cache] DELETE /reviews/:slug purge\n```\n\n## Single types\n\nBy default, the middleware assumes that the specified models are collections. Meaning that having `'post'` or `'posts'` in your configuration will result in the `/posts/*` being cached. Pluralization is applied in order to match the Strapi generated endpoints.\n\nThat behaviour is however not desired for [single types](https://strapi.io/blog/beta-19-single-types-uid-field) such as `homepage` which should remain singular in the endpoint (`/homepage`)\n\nYou can mark a specific model as being a single type by using the `singleType` boolean field on model configurations\n\ne.g\n\n```javascript\n// config/middleware.js\nmodule.exports = ({ env }) =\u003e ({\n  settings: {\n    cache: {\n      enabled: true,\n      models: [\n        {\n          model: 'footer',\n          singleType: true,\n        },\n      ],\n    },\n  },\n});\n```\n\nRunning the CMS should now show the following\n\n```\n$ strapi develop\n[2021-02-26T07:03:18.981Z] info [cache] Mounting LRU cache middleware\n[2021-02-26T07:03:18.982Z] info [cache] Storage engine: mem\n[2021-02-26T07:03:18.985Z] debug [cache] Register review routes middlewares\n[2021-02-26T07:03:18.986Z] debug [cache] PUT /footer purge\n[2021-02-26T07:03:18.987Z] debug [cache] DELETE /footer purge\n[2021-02-26T07:03:18.987Z] debug [cache] GET /review recv maxAge=3600000\n```\n\n## Authentication\n\nBy default, cache is not looked up if `Authorization` or `Cookie` header are present.\nTo dissable this behaviour add `hitpass: false` to the model cache configuration\n\nYou can customize event further with a function `hitpass: (ctx) =\u003e true` where `ctx` is the koa context of the request. Keep in mind that this function is executed before every `recv` requests.\n\ne.g\n\n```javascript\n// config/middleware.js\nmodule.exports = ({ env }) =\u003e ({\n  settings: {\n    cache: {\n      enabled: true,\n      models: [\n        {\n          model: 'footer',\n          hitpass: false,\n          singleType: true,\n        },\n      ],\n    },\n  },\n});\n```\n\n## Etag support\n\nBy setting the `enableEtagSupport` to `true`, the middleware will automatically create an Etag for each payload it caches.\n\nFurther requests sent with the `If-None-Match` header will be returned a `304 Not Modified` status if the content for that url has not changed.\n\n## Clearing related cache\n\nBy setting the `clearRelatedCache` to `true`, the middleware will inspect the Strapi models before a cache clearing operation to locate models that have relations with the queried model so that their cache is also cleared (this clears the whole cache for the related models). The inspection is performed by looking for direct relations between models and also by doing a deep dive in components, looking for relations to the queried model there too.\n\n## Cache entry point\n\n### Koa Context\n\nBy setting the `withKoaContext` configuration to `true`, the middleware will extend the Koa Context with an entry point which can be used to clear the cache from within controllers\n\n```javascript\n// config/middleware.js\nmodule.exports = ({ env }) =\u003e ({\n  settings: {\n    cache: {\n      enabled: true,\n      withKoaContext: true,\n      models: ['post'],\n    },\n  },\n});\n\n// controller\n\nmodule.exports = {\n  async index(ctx) {\n    ctx.middleware.cache; // A direct access to the Cache API\n  },\n};\n```\n\n**IMPORTANT**: We do not recommend using this unless truly necessary. It is disabled by default as it goes against the non-intrusive/transparent nature of this middleware.\n\n### Strapi Middleware\n\nBy setting the `withStrapiMiddleware` configuration to `true`, the middleware will extend the Strapi middleware object with an entry point which can be used to clear the cache from anywhere (e.g., inside a Model's lifecycle hook where `ctx` is not available).\n\n```javascript\n// config/middleware.js\nmodule.exports = ({ env }) =\u003e ({\n  settings: {\n    cache: {\n      enabled: true,\n      withStrapiMiddleware: true,\n      models: ['post'],\n    },\n  },\n});\n\n// model\n\nmodule.exports = {\n  lifecycles: {\n    async beforeUpdate(params, data) {\n      strapi.middleware.cache; // A direct access to the Cache API\n    },\n  },\n};\n```\n\n**IMPORTANT**: We do not recommend using this unless truly necessary. It is disabled by default as it goes against the non-intrusive/transparent nature of this middleware.\n\n## Cache API\n\n```javascript\n/**\n * @typedef {import('strapi-middleware-cache').CacheStore} CacheStore\n * @typedef {import('strapi-middleware-cache').MiddlewareCacheConfig} MiddlewareCacheConfig\n * @typedef {import('strapi-middleware-cache').ModelCacheConfig} ModelCacheConfig\n * @typedef {import('strapi-middleware-cache').CustomRoute} CustomRoute\n */\n\nconst cache = {\n  /**\n   * @type {CacheStore}\n   */\n  store,\n\n  /**\n   * @type {MiddlewareCacheConfig}\n   */\n  options,\n\n  /**\n   * Clear cache with uri parameters\n   *\n   * @param {ModelCacheConfig} cacheConf\n   * @param {{ [key: string]: string; }=} params\n   */\n  clearCache,\n\n  /**\n   * Get related ModelCacheConfig\n   *\n   * @param {string} model\n   * @param {string=} plugin\n   * @returns {ModelCacheConfig=}\n   */\n  getCacheConfig,\n\n  /**\n   * Get related ModelCacheConfig with an uid\n   *\n   * uid:\n   * - application::sport.sport\n   * - plugins::users-permissions.user\n   *\n   * @param {string} uid\n   * @returns {ModelCacheConfig=}\n   */\n  getCacheConfigByUid,\n\n  /**\n   * Get models uid that is related to a ModelCacheConfig\n   *\n   * @param {ModelCacheConfig} cacheConf The model used to find related caches to purge\n   * @return {string[]} Array of related models uid\n   */\n  getRelatedModelsUid,\n\n  /**\n   * Get regexs to match all ModelCacheConfig keys with given params\n   *\n   * @param {ModelCacheConfig} cacheConf\n   * @param {{ [key: string]: string; }=} params\n   * @param {boolean=} wildcard\n   * @returns {RegExp[]}\n   */\n  getCacheConfRegExp,\n\n  /**\n   * Get regexs to match CustomRoute keys with given params\n   *\n   * @param {CustomRoute} route\n   * @param {{ [key: string]: string; }=} params\n   * @param {boolean=} wildcard\n   * @returns {RegExp[]}\n   */\n  getRouteRegExp,\n};\n```\n\n## Admin panel interactions\n\nThe strapi admin panel uses a separate rest api to apply changes to records, e.g `/content-manager/explorer/application::post.post` the middleware will also watch for write operations on that endpoint and bust the cache accordingly\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrixr%2Fstrapi-middleware-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatrixr%2Fstrapi-middleware-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrixr%2Fstrapi-middleware-cache/lists"}