Strapi REST Cache Plugin
The Strapi REST Cache Plugin is a community-maintained solution for accelerating HTTP requests within Strapi applications by implementing a caching layer. It utilizes a provider-based architecture, allowing developers to choose from various storage backends like in-memory, Redis, or file system for caching responses. The plugin supports configurable caching strategies and features automatic invalidation of cached data when related content types are updated, mitigating issues with stale data. While the provided metadata indicates version 4.2.9, the latest significant release is v5.0.0, which introduced breaking changes for Strapi v5 compatibility. The project maintains an active development cadence, with frequent updates addressing bugs and adding features, aiming to improve Strapi API performance. It differentiates itself by its flexible provider system and automatic cache invalidation tied to Strapi content updates.
Common errors
-
Error: Cannot find module 'strapi-plugin-rest-cache'
cause The package name for the plugin has changed in Strapi v5.fixEnsure you are using the correct package name `@strapi-community/plugin-rest-cache` in your `package.json` and in your Strapi plugin configuration file (e.g., `config/plugins.js`). -
TypeError: Cannot read properties of undefined (reading 'config') at module.exports
cause The plugin is enabled but misconfigured, often missing the `provider` or `strategy` objects, or having incorrect syntax.fixReview your `config/plugins.js` file for the `rest-cache` entry. Ensure that `enabled: true` is set and that the `config` object correctly defines a `provider` and `strategy` with valid options, including `name` for the provider. -
Cache is not being purged automatically when content types are updated.
cause This typically happens if the `strategy.contentTypes` array is not correctly configured to include the relevant content types, or if the content types are not properly exposed via the REST API.fixVerify that the `contentTypes` array in your plugin configuration's `strategy` includes the exact API ID of the content types you expect to be purged. E.g., `['api::article.article']`.
Warnings
- breaking The `strapi-plugin-rest-cache` package name changed to `@strapi-community/plugin-rest-cache` in v5.0.0. Additionally, all provider packages (e.g., `strapi-provider-rest-cache-memory`) were renamed to their `@strapi-community` scoped equivalents.
- breaking The Couchbase Provider was entirely removed in v5.0.0 due to lack of maintenance and testing resources.
- gotcha The README explicitly states the package is currently under 'ALPHA' status, indicating potential instability, API changes without major version bumps, or incomplete features. While v5.0.0 is released, this warning from the README for prior versions implies ongoing development and refinement.
- gotcha This plugin is specifically for Strapi v4.x.x and v5.x.x. It is not compatible with Strapi v3.x. The recommended alternative for Strapi v3.x is `strapi-middleware-cache`.
Install
-
npm install strapi-plugin-rest-cache -
yarn add strapi-plugin-rest-cache -
pnpm add strapi-plugin-rest-cache
Imports
- plugins
import { RestCachePlugin } from 'strapi-plugin-rest-cache';module.exports = { 'rest-cache': { enabled: true, config: { /* ... */ } } }; - RestCacheConfig
// No direct import for configuration. Configuration is defined in config/plugins.js
- RestCacheProvider
import { MemoryProvider } from '@strapi-community/plugin-rest-cache';const memoryProvider = require('@strapi-community/provider-rest-cache-memory');
Quickstart
module.exports = {
// ... other plugins
'rest-cache': {
enabled: true,
config: {
provider: {
name: 'memory',
options: {
max: 32767, // max number of items in cache
maxAge: 3600, // cache expiration time in seconds
},
},
strategy: {
contentTypes: [
// List of content types to cache
'api::article.article',
'api::category.category',
],
maxAge: 3600, // global maxAge for content types
hitpass: false, // If true, cache will not be used if query params are present
},
},
},
// ...
};