Bobcares

Strapi-middleware-cache: Setup

by | Jun 13, 2023

Let us learn more on Strapi-middleware-cache and how to set it up with the support of our server management support services at Bobcares.

What is Strapi-middleware-cache?

This middleware caches incoming strapi API GET queries depending on query parameters and model ID.

When a PUT, PATCH, POST, or DELETE request is received, the cache is automatically flushed.

This supports the following engine types:

  • Memory (default)
  • Redis

Caching must be explicitly enabled per model.

Strapi Middleware Cache Installation and Setup

The following requirements are required to utilize the Strapi middleware cache:

  • In the working environment, we should install the appropriate version of Node.js.
  • We should have a fundamental understanding of how Strapi operates.

How to set up Strapi-middleware-cache?

  • First, we’ll install the Strapi middleware cache plugin into the Strapi project by running the command below from the project’s directory in the CLI:

     

  • Add the plugin to the Strapi project once it has been configured locally. Start a Strapi project.

    Add a middleware.js file to the project’s settings if we don’t already have one.

    To accomplish this, add a middleware.js file in the project’s config folder.

  • Enable the middleware cache plugin by pasting the following code into the newly formed middleware.js file.

    We may configure environments by following the guidelines for Strapi.

     module.exports = ({ env }) => ({
    settings: {
    cache: {
    enabled: true,
    }
    }
    });

  • Start the Strapi project by using “strapi develop” to ensure the successful installation of the middleware cache plugin in the working environment.

     $ strapi develop
    [2023-04-26T06:15:50.394Z] debug [Cache] Mounting LRU cache middleware
    [2023-04-26T06:15:50.396Z] debug [Cache] Storage engine: mem

Model Configuration

After configuring the middleware cache plugin with the Strapi, we must specifically specify the model we want to apply to the cache.

Let’s populate the middleware configuration object with models.

module.exports = ({ env }) => ({
settings: {
cache: {
enabled: true,
models: ['blogs'],## you can add other models
}
}
});

We can see that caching for the blogs route has begun if we run the project with strapi develop.

[Need assistance with similar queries? We are here to help]

Conclusion

To sum up we have now seen more on Strapi-middleware-cache with the support of our tech support team.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.