40
loading...
This website collects cookies to deliver better user experience
GET
requests on the Strapi API based on query params and model ID. This middleware allows you to either cache your data on your application's memory by default or with Redis, a third-party database. The cache is automatically busted every time a PUT
, POST
, or DELETE
request comes in.npm install --save strapi-middleware-cache
#or
yarn add strapi-middleware-cache
middleware.js
file to your project's configuration. To do this, create a middleware.js
file in your config
folder, which is at the root of your project.middleware.js
file you just created. You can configure environments by following the guide on Strapi's documentation.module.exports = ({ env }) => ({
settings: {
cache: {
enabled: true,
}
}
});
strapi develop
to see that the middleware cache plugin has been set up successfully in your working environment.$ strapi develop
[2021-06-26T06:15:50.394Z] debug [Cache] Mounting LRU cache middleware
[2021-06-26T06:15:50.396Z] debug [Cache] Storage engine: mem
module.exports = ({ env }) => ({
settings: {
cache: {
enabled: true,
models: ['blogs'],## you can add other models
}
}
});
strapi develop
, you’ll notice that caching for the blogs
route has commenced.$ strapi develop
[2021-06-26T20:25:14.744Z] debug [Cache] Mounting LRU cache middleware
[2021-06-26T20:25:14.745Z] debug [Cache] Storage engine: mem
[2021-06-26T20:25:14.752Z] debug [Cache] Caching route /blogs/:id* [maxAge=3600000]
Redis
as the cache engine. Sometimes, you'll want to configure the max number of entries, cache timeout, etc., in your storage engine. settings
in your middleware configuration object. For example, I can configure the type of engine for caching as Redis and set maxAge
and other properties of our models.module.exports = ({ env }) => ({
settings: {
cache: {
enabled: true,
type: 'redis',
maxAge: 2600000,
models: ['blogs'],
}
}
});
type
property describes the type of storage engine for the middleware cache plugin to use. By default, the type
is set to memory which is denoted by mem
.maxAge
tells us the time after which a cache entry will be considered invalid. This time is usually represented in milliseconds. The default maxAge
in the strapi-middleware-cache
is 3600000 milliseconds.cacheTimeout
which specifies the time after which a cache request is timed out.enableEtagSupport
is set to false.[2021-06-26T06:15:50.394Z] debug [Cache] Mounting LRU cache middleware
[2021-06-26T06:15:50.396Z] debug [Cache] Storage engine: mem
log
is set to true
. If you set the property to false
, you won't get an output in your console concerning the middleware.populateContext
is set to false
by default. If you set the property to true
, this setting will inject a cache entry point into the Koa context. It is advantageous, especially for developers who are interested in building with Koa.{
model: 'account',
headers: ['accept-JavaScript']
}
no-cache
, no-store
, public
and private
.memory
and opt for Redis
, they can configure Redis with a config object passed to ioredis. For instance, I can configure other properties and configure Redis sentinel to monitor some of my nodes if there's a failover.module.exports = ({ env }) => ({
settings: {
cache: {
enabled: true,
type: 'redis',
maxAge: 2600000,
max: 400,
cacheTimeout: 400,
enableEtagSupport: true,
logs: true,
populateContext: false,
models: ['blos'],
redisConfig: {
sentinels: [
{ host: '192.168.10.41', port: 26379 },
{ host: '192.168.10.42', port: 26379 },
{ host: '192.168.10.43', port: 26379 },
],
name: 'redis-primary',
}
//or you can connect to redis lab with the command below.
redisConfig: {
host: 'redis-5555.c8.us-east-1-4.ec2.cloud.redislabs.com',
port: 5555,
password: 'secret_password',
},
}
}
});
maxAge
or cacheTimeout
property individually since the models will be accessing different types of resources. To configure model properties individually, but the property of interest into the model property as shown below.module.exports = ({ env }) => ({
settings: {
cache: {
enabled: true,
models: [
{
model: 'blogs',
maxAge: 1000000,
cacheTimeout: 400,
},
{
model: 'posts',
maxAge: 800000,
cacheTimeout: 450,
}
]
}
}
});
maxAge
and cacheTimeout
properties for blogs
and posts
individually. Since we didn't configure other properties, blogs
and posts
will follow the default configuration for other properties. cache
object, which will cause blogs
and posts
to take those configurations instead of the default configuration. For instance, from the example below, our storage engine for blogs
and posts
will be Redis, and we won't have log outputs in our console.module.exports = ({ env }) => ({
settings: {
cache: {
type: 'redis',
logs: false,
enabled: true,
models: [
{
model: 'blogs',
maxAge: 1000000,
cacheTimeout: 400,
},
{
model: 'posts',
maxAge: 800000,
cacheTimeout: 450,
}
]
}
}
});
blog
content type, the collection name is blogs
. What the Strapi-middleware-cache does is pluralize the model name in the configuration. So, if you put your model name as posts
or post
the middleware will cache /posts/*
.about
, this is not beneficial and can lead to an error. Furthermore, to make the middleware skip pluralization for single types, set singleType
to true
for your model.module.exports = ({ env }) => ({
settings: {
cache: {
enabled: true,
models: [
{
model: 'about',
singleType: true,
}
]
}
}
});
PUT
, POST
, or DELETE
request, the cache automatically gets busted. populateContext
configuration to true
. This setting will give you direct access to the cache engine. To do this, we will add a controller directly under our middleware module object. The middleware will extend the Koa Context with an entry point to clear the cache from within controllers.module.exports = ({ env }) => ({
settings: {
cache: {
enabled: true,
populateContext: true,
models: ['blogs']
}
}
});
// controller
module.exports = {
async index(ctx) {
ctx.middleware.cache.store // This will give a direct access to the cache engine
await ctx.middleware.cache.bust({ model: 'blogs', id: '1' }); // Clear cache for this specific record
await ctx.middleware.cache.bust({ model: 'posts' }); // Clear cache for the entire model collection
await ctx.middleware.cache.bust({ model: 'about' }); // Don't pluralize model names of single types
}
};