Higher Quality, Stronger Performance, Increased Stability, Better Developer Experience, discover everything we've shipped recently!

Strapi plugin logo for Strapi Cache

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.

npm version Strapi Version License: MIT npm


✨ 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, Redis and Valkey caching

🚀 Installation

Install via npm or yarn:

npm install strapi-cache

or

yarn add strapi-cache

Quickstart

// config/plugins.{js,ts}
'strapi-cache': {
  enabled: true,
},

To use Redis or Valkey instead of memory, set provider and redisConfig (required for those providers):

// config/plugins.{js,ts}
'strapi-cache': {
  enabled: true,
  config: {
    provider: 'redis', // or 'valkey'
    redisConfig: env('REDIS_URL', 'redis://127.0.0.1:6379'),
  },
},

See ioredis (Redis) or iovalkey (Valkey) for advanced redisConfig shapes (URL string or client options object).

Full configuration example:

// config/plugins.{js,ts}
'strapi-cache': {
  enabled: true,
  config: {
    debug: false, // Enable debug logs
    max: 1000, // Maximum number of items in the cache (only for memory cache)
    ttl: 1000 * 60 * 60, // Time to live for cache items (1 hour)
    size: 1024 * 1024 * 1024, // Maximum size of the cache (1 GB) (only for memory cache)
    allowStale: false, // Allow stale cache items (only for memory cache)
    cacheableRoutes: ['/api/products', '/api/categories'], // Caches routes which start with these paths (if empty array, all '/api' routes are cached)
    // keyGenerator: (ctx) => `${ctx.request.method}:${ctx.request.url}`, // (Optional) custom cache key for REST + GraphQL requests; receives koa ctx
    // cacheableEntities: ['products', 'categories'], // (Optional) Specify which entities to cache. When set, only these entities will be cached (ignores cacheableRoutes). If not set (undefined), cacheableRoutes logic is used
    excludeRoutes: ['/api/products/private'], // Exclude routes which start with these paths from being cached (takes precedence over cacheableRoutes). **Note:** `excludeRoutes` takes precedence over `cacheableRoutes`.
    provider: 'memory', // Cache provider ('memory', 'redis' or 'valkey')
    redisConfig: env('REDIS_URL', 'redis://localhost:6379'), // Redis/Valkey config: string or object. See https://github.com/redis/ioredis (Redis) or https://github.com/valkey-io/iovalkey (Valkey)
    redisClusterNodes: [], // If provided any cluster node (this list is not empty), initialize cluster client. Each object must have keys 'host' and 'port'
    redisClusterOptions: {}, // Options for ioredis redis cluster client. redisOptions key is taken from redisConfig parameter above if not set here. See https://github.com/redis/ioredis for references
    cacheHeaders: true, // Plugin also stores response headers in the cache (set to false if you don't want to cache headers)
    cacheHeadersDenyList: ['access-control-allow-origin', 'content-encoding'], // Headers to exclude from the cache (must be lowercase, if empty array, no headers are excluded, cacheHeaders must be true)
    cacheHeadersAllowList: ['content-type', 'content-security-policy'], // Headers to include in the cache (must be lowercase, if empty array, all headers are cached, cacheHeaders must be true)
    cacheAuthorizedRequests: false, // Cache requests with authorization headers (set to true if you want to cache authorized requests)
    cacheGetTimeoutInMs: 1000, // Timeout for getting cached data in milliseconds (default is 1 second)
    autoPurgeCache: true, // Automatically purge cache on content CRUD operations
    autoPurgeGraphQL: true, // Automatically purge GraphQL cache on content CRUD operations
    autoPurgeCacheOnStart: true, // Automatically purge cache on Strapi startup
    disableAdminPopups: false, // Disable popups in the admin panel
    disableAdminButtons: false, // Disable the purge cache buttons in the admin panel (list view and edit view)
  },
},

⚙️ Configuration

Possible configuration keys are listed below; omitted keys keep the plugin defaults.

KeyDescriptionPossible values
debugLog cache decisions and operations to the server consoletrue or false (default: false)
providerWhere entries are stored'memory', 'redis', or 'valkey' (default: 'memory')
redisConfigRedis/Valkey connection: URL string or client options passed to ioredis/iovalkeyString or object; required when provider is 'redis' or 'valkey'. Default: value of REDIS_URL from the environment
redisClusterNodesSeed nodes for Redis cluster mode; non-empty list switches to a cluster clientArray of { host: string, port: number } (default: [])
redisClusterOptionsOptions for the cluster client (e.g. scaleReads); redisOptions often come from redisConfigObject (default: {})
redisScanDeleteCountCOUNT hint for SCAN when purging keys (Redis/Valkey)Positive number (default: 100)
maxMaximum number of entries (in-memory provider only)Positive integer (default: 1000)
ttlTime-to-live for each entry, in millisecondsNon-negative number (default: 3600000, i.e. 1 hour)
sizeApproximate max total size in bytes (in-memory provider only)Positive integer (default: 10485760, i.e. 10 MB)
allowStaleWhether stale entries may be returned (in-memory provider only)true or false (default: false)
cacheableRoutesOnly URLs starting with one of these paths are cached; if empty, every URL under the REST API prefix matchesArray of path prefix strings (default: [] meaning “all API routes”)
keyGeneratorCustom function to build REST cache keys; receives Koa ctx; when omitted, default key is ${method}:${url}; for graphql, if set, the ctx gets additional fields: rootFields and operationName which may be useful for invalidation logicFunction (ctx) => string (default: unset)
cacheableEntitiesIf non-empty, only these API “entity” segments are cached; when set, this drives eligibility instead of cacheableRoutesArray of strings (e.g. collection/table names), or omit / leave empty to use cacheableRoutes
excludeRoutesURLs starting with any of these prefixes are never cached; evaluated before cacheableRoutes / entitiesArray of path prefix strings (default: [])
cacheHeadersStore and replay response headers with the bodytrue or false (default: true)
cacheHeadersDenyListHeader names (lowercase) to strip when cacheHeaders is trueArray of strings (default: [])
cacheHeadersAllowListIf non-empty, only these header names (lowercase) are stored; if empty, all headers are stored (subject to deny list)Array of strings (default: [])
cacheAuthorizedRequestsWhether to cache requests that include an Authorization headertrue or false (default: false)
cacheGetTimeoutInMsMax time to wait for a cache read before treating it as a missMilliseconds (default: 1000)
autoPurgeCacheInvalidate relevant REST cache entries after content create/update/deletetrue or false (default: true)
autoPurgeGraphQLInvalidate GraphQL cache after content create/update/deletetrue or false (default: false if omitted; set true to enable)
autoPurgeCacheOnStartClear the cache when Strapi startstrue or false (default: true)
disableAdminPopupsTurn off admin UI notifications for cache actionstrue or false (default: false)
disableAdminButtonsHide manual purge controls in the admin (list and edit views)true or false (default: false)

🔍 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 that have the key in the cache key, expects JSON body with key field)
  • GET /strapi-cache/cacheable-routes (returns the cacheable routes defined in the config)
  • GET /strapi-cache/config (returns the current plugin 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, Redis or Valkey, depending on the configuration.
  • Packages: Uses lru-cache for in-memory cache. Uses ioredis for Redis and iovalkey for Valkey caching.
  • Automatic Invalidation: When autoPurgeCache is enabled (default), relevant REST cache entries are invalidated on content create, update, or delete. When autoPurgeGraphQL is enabled, GraphQL cache is invalidated the same way (it is off unless you set it in config).
  • no-cache Header Support: Respects the no-cache header, letting you skip the cache by setting Cache-Control: no-cache in your request.
  • Default Cached Requests: By default, caches all GET requests to /api (or whatever prefix you defined) and POST requests to /graphql or predefined graphql route from graphql plugin config. You can customize which routes or entities to cache using cacheableRoutes or cacheableEntities config options.

🔮 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.

If you have any feature requests or suggestions, please open a dedicated issue.

🛑 Problems

If you encounter any issues, please feel free to open an issue on the GitHub repo.

🛠️ 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

STATS

49 GitHub stars4763 weekly downloads

Last updated

4 days ago

Strapi Version

5.0.0 and above

Author

github profile image for TupiC
TupiC

Useful links

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.