@strapi-community/provider-rest-cache-redis
v5.1.0
Published
Speed-up HTTP requests with LRU cache
Readme
Stores cache entries in Redis, so every Strapi instance shares one cache and a purge on any of them clears it everywhere.
Use this as soon as you run more than one instance, or whenever the cache should survive a restart.
KeyDB and Valkey speak the same protocol and work with this provider unchanged.
Install
Three packages: the plugin, this provider, and the Redis connection plugin that owns the client.
npm install @strapi-community/plugin-rest-cache @strapi-community/plugin-redis @strapi-community/provider-rest-cache-redisyarn add @strapi-community/plugin-rest-cache @strapi-community/plugin-redis @strapi-community/provider-rest-cache-redispnpm add @strapi-community/plugin-rest-cache @strapi-community/plugin-redis @strapi-community/provider-rest-cache-redisConfigure
// ./config/plugins.js
module.exports = ({ env }) => ({
// The connection is owned by @strapi-community/plugin-redis.
redis: {
config: {
connections: {
default: {
connection: {
host: env("REDIS_HOST", "127.0.0.1"),
port: env.int("REDIS_PORT", 6379),
db: 0,
},
},
},
},
},
"rest-cache": {
config: {
provider: {
name: "redis",
options: {
// Names a connection defined above.
connection: "default",
},
},
strategy: {
// Must match your Redis client's keyPrefix, if it sets one.
keysPrefix: "",
contentTypes: ["api::article.article"],
},
},
},
});// ./config/plugins.ts
export default ({ env }) => ({
redis: {
config: {
connections: {
default: {
connection: {
host: env("REDIS_HOST", "127.0.0.1"),
port: env.int("REDIS_PORT", 6379),
db: 0,
},
},
},
},
},
"rest-cache": {
config: {
provider: {
name: "redis",
options: {
connection: "default",
},
},
strategy: {
keysPrefix: "",
contentTypes: ["api::article.article"],
},
},
},
});Options
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| connection | string | "default" | Which connection from @strapi-community/plugin-redis to use. |
| ttl | number (ms) | — | Store-level default lifetime. The plugin passes an explicit lifetime on every write from strategy.maxAge, so this is only a backstop. |
Any other option is passed through to @keyv/redis.
If your Redis client sets a
keyPrefix, setstrategy.keysPrefixto match. A mismatch means the plugin enumerates none of its own keys, so every purge silently does nothing.
Redis Cluster
Supported. Deletes are issued key by key when clustered, because a multi-key
UNLINK spanning hash slots is rejected outright:
CROSSSLOT Keys in request don't hash to the same slotKey enumeration reads the adapter's own tracking set — a single SMEMBERS —
rather than scanning the keyspace. On a 100k-entry cache the scanning approach
measured roughly 20,000 round trips and 55MB transferred.
KeyDB and Valkey
Both are Redis-protocol compatible; point the connection at them and change nothing else.
connection: {
host: env("KEYDB_HOST", "127.0.0.1"),
port: env.int("KEYDB_PORT", 6379),
}Documentation
License
See the LICENSE file.
