Strapi Cache
A LRU-Cache plugin for strapi v5
🧠 strapi-cache
A powerful LRU-Cache plugin for Strapi v5
Boost your API performance with automatic in-memory or Redis caching for REST and GraphQL requests.
✨ Features
- ⚡️ Cache REST API responses
- 🔮 Cache GraphQL queries
- ♻️ LRU (Least Recently Used) caching strategy
- 🔧 Simple integration with Strapi config
- 📦 Lightweight with zero overhead
- 🗄️ Supports in-memory and Redis caching
🚀 Installation
Install via npm or yarn:
npm install strapi-cache
or
yarn add strapi-cache
⚙️ Configuration
In your Strapi project, navigate to config/plugins.js
and add the following configuration:
1// config/plugins.{js,ts}
2'strapi-cache': {
3 enabled: true,
4 config: {
5 debug: false, // Enable debug logs
6 max: 1000, // Maximum number of items in the cache (only for memory cache)
7 ttl: 1000 * 60 * 60, // Time to live for cache items (1 hour)
8 size: 1024 * 1024 * 1024, // Maximum size of the cache (1 GB) (only for memory cache)
9 allowStale: false, // Allow stale cache items (only for memory cache)
10 cacheableRoutes: ['/api/products', '/api/categories'], // Caches routes which start with these paths (if empty array, all '/api' routes are cached)
11 provider: 'memory', // Cache provider ('memory' or 'redis')
12 redisConfig: env('REDIS_URL', 'redis://localhost:6379'), // Redis config takes either a string or an object see https://ioredis.readthedocs.io/en/stable/README for references to what object is available, the object or string is passed directly to ioredis client (if using Redis)
13 cacheHeaders: true, // Plugin also stores response headers in the cache (set to false if you don't want to cache headers)
14 cacheAuthorizedRequests: false, // Cache requests with authorization headers (set to true if you want to cache authorized requests)
15 },
16},
🔍 Routes
The plugin creates three new routes
POST /strapi-cache/purge-cache
(purges the whole cache)POST /strapi-cache/purge-cache/:key
(purges cache entries with have the key in the cache key)GET /strapi-cache/cacheable-routes
(returns the cacheable routes defined in the config)
All of these routes are protected by the policies admin::isAuthenticatedAdmin
and plugin::strapi-cache.purge-cache
. The plugin::strapi-cache.purge-cache
policy can be managed in the plugin's permissions section under the settings.
🗂️ How It Works
- Storage: The plugin keeps cached data in memory or Redis, depending on the configuration.
- Packages: Uses lru-cache for in-memory cache. Uses ioredis for Redis caching.
- Automatic Invalidation: Cache is cleared automatically when content is updated, deleted, or created. (GraphQL cache clears on any content update.)
no-cache
Header Support: Respects theno-cache
header, letting you skip the cache by settingCache-Control: no-cache
in your request.- Default Cached Requests: By default, caches all GET requests to
/api
and POST requests to/graphql
. You can customize which content types to cache in the config (only for GET requests).
🔮 Planned Features
- Cache Invalidation: Automatically invalidate cache on content updates, deletions, or creations.
- GraphQL Caching: Cache GraphQL queries.
- Purge Cache Button: Add a UI option in the Strapi admin panel to manually purge the cache for content-types.
- Purge Whole Cache Button: Add a UI option in the Strapi admin settings panel to purge the whole cache.
- Route/Content-Type Specific Caching: Allow users to define which routes should be cached based.
- Switchable Cache Providers: Explore support for other caching providers like Redis for distributed caching.
🛑 Problems
If you encounter any issues, please feel free to open an issue on the GitHub repo.
🛠️ Troubleshooting
If you encounter an error like:
1Access to fetch at 'http://your-backend.com' from origin 'http://your-origin.com' has been blocked by CORS policy:
2Request header field cache-control is not allowed by Access-Control-Allow-Headers in preflight response.
You might need to adjust your CORS middlware settings in Strapi:
1// config/middlewares.{js,ts}
2'strapi::cors';
with:
1// config/middlewares.{js,ts}
2{
3 name: "strapi::cors",
4 config: {
5 headers: ["Content-Type", "Authorization", "Cache-Control"], // Add 'Cache-Control' to the allowed headers
6 },
7},
🛠️ Contributing
Contributions are welcome! If you have suggestions or improvements, please open an issue or submit a pull request.
Install now
npm install strapi-cache
Create your own plugin
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.