strapi-community / strapi-plugin-redis

Plugin used to centralize management of Redis connections in Strapi
MIT License
41 stars 5 forks source link

Could not initialize REST Cache provider "redis". The package "strapi-plugin-redis" is required. #18

Closed neural-loop closed 9 months ago

neural-loop commented 1 year ago

I haven't been able to figure this one out.

4|beta-api | [2023-06-20 17:55:48.221] debug: ⛔ Server wasn't able to start properly. 4|beta-api | [2023-06-20 17:55:48.225] error: Could not initialize REST Cache provider "redis". The package "strapi-plugin-redis" is required. 4|beta-api | Error: Could not initialize REST Cache provider "redis". The package "strapi-plugin-redis" is required.

 "dependencies": {
    "@strapi/plugin-documentation": "4.10.7",
    "@strapi/plugin-i18n": "4.10.7",
    "@strapi/plugin-users-permissions": "4.10.7",
    "@strapi/provider-email-nodemailer": "4.10.7",
    "@strapi/strapi": "4.10.7",
    "better-sqlite3": "8.4.0",
    "pg": "8.8.0",
    "pg-connection-string": "2.6.0",
    "strapi-plugin-ezforms": "0.1.2",
    "strapi-plugin-populate-deep": "2.0.0",
    "strapi-plugin-redis": "^1.0.1",
    "strapi-plugin-request-id": "1.1.0",
    "strapi-plugin-rest-cache": "^4.2.7",
    "strapi-plugin-sitemap": "^2.0.10",
    "strapi-plugin-slugify": "^2.3.2",
    "strapi-plugin-wysiwyg-react-md-editor": "4.3.0",
    "strapi-provider-rest-cache-memory": "4.2.7",
    "strapi-provider-rest-cache-redis": "^4.2.7"
  },
  redis: {
    config: {
      connections: {
        default: {
          connection: {
            host: '127.0.0.1',
            port: 6379,
            db: 0,
          },
          settings: {
            debug: false,
          },
        },
      },
    },
  },
  "rest-cache": {
    config: {
      provider: {
        name: "redis",
        options: {
          max: 32767,
          connection: "default",
        },
      },
      strategy: {
        enableEtagSupport: true,
        logs: true,
        clearRelatedCache: true,
        maxAge: 3600000,
        contentTypes: [
          "api::model.model",
        ],
      },
    },
  }
0|Redis    |  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
0|Redis    |  |    `-._   `._    /     _.-'    |     PID: 3116
0|Redis    |   `-._    `-._  `-./  _.-'    _.-'                                   
0|Redis    |  |`-._`-._    `-.__.-'    _.-'_.-'|                                  
0|Redis    |  |    `-._`-._        _.-'_.-'    |           http://redis.io        
0|Redis    |   `-._    `-._`-.__.-'_.-'    _.-'                                   
0|Redis    |  |`-._`-._    `-.__.-'    _.-'_.-'|                                  
0|Redis    |  |    `-._`-._        _.-'_.-'    |                                  
0|Redis    |   `-._    `-._`-.__.-'_.-'    _.-'                                   
0|Redis    |       `-._    `-.__.-'    _.-'                                       
0|Redis    |           `-._        _.-'                                           
0|Redis    |               `-.__.-'                                               
0|Redis    | 
0|Redis    | 3116:M 12 Jun 13:33:08.867 # Server started, Redis version 3.2.12
0|Redis    | 3116:M 12 Jun 13:33:08.867 * The server is now ready to accept connections on port 6379
derrickmehaffy commented 1 year ago

You need to install the `strapi-plugin-redis package I believe, can you give a copy of your package.json?

neural-loop commented 1 year ago

Thanks for responding - it's there in the first block of text under dependencies

"strapi-plugin-redis": "^1.0.1",
{
  "name": "sitename",
  "private": true,
  "version": "0.1.0",
  "description": "A Strapi application",
  "scripts": {
    "develop": "strapi develop",
    "start": "strapi start",
    "build": "strapi build",
    "strapi": "strapi"
  },
  "dependencies": {
    "@strapi/plugin-documentation": "4.10.7",
    "@strapi/plugin-i18n": "4.10.7",
    "@strapi/plugin-users-permissions": "4.10.7",
    "@strapi/provider-email-nodemailer": "4.10.7",
    "@strapi/strapi": "4.10.7",
    "better-sqlite3": "8.4.0",
    "pg": "8.8.0",
    "pg-connection-string": "2.6.0",
    "strapi-plugin-ezforms": "0.1.2",
    "strapi-plugin-populate-deep": "2.0.0",
    "strapi-plugin-redis": "^1.0.1",
    "strapi-plugin-request-id": "1.1.0",
    "strapi-plugin-rest-cache": "^4.2.7",
    "strapi-plugin-sitemap": "^2.0.10",
    "strapi-plugin-slugify": "^2.3.2",
    "strapi-plugin-wysiwyg-react-md-editor": "4.3.0",
    "strapi-provider-rest-cache-memory": "4.2.7",
    "strapi-provider-rest-cache-redis": "^4.2.7"
  },
  "strapi": {
    "uuid": ""
  },
  "engines": {
    "node": ">=14.19.1 <=18.x.x",
    "npm": ">= 8.0.0"
  },
  "license": "MIT"
}

I've had this error in all 4.x versions. Never could figure it out.

SalahAdDin commented 11 months ago

Is this plugin still maintained?

derrickmehaffy commented 11 months ago

Is this plugin still maintained?

Yes it is in my free time but I've not been able to reproduce this issue

laszlo-san commented 9 months ago

I think I know what causes the issue!

If you follow Strapi env variable and configuration conventions, you most likely created folders for your environemtns into the config folder. That is merged together with your default configs in the particular environment when you run strapi, but the order of each config might change!

The REST Cache plugin states that the redis plugin configuration MUST come before the REST Cache plugin config.

Now the error message what I had is not coming from this package, but from an intermediary package: strapi-provider-rest-cache-redis kép

For me, and hopefully for @neural-loop s issue the solution is to declare env dependant config into the base plugins.ts file like so:

export default ({ env }) => {
   const redisConfig =
    process.env.NODE_ENV !== 'production'
      ? {
          enabled: false,
        }
      : {
          config: {
            connections: {
              default: {
                connection: {
                  host: '127.0.0.1',
                  port: 6379,
                  db: 0,
                },
                settings: {
                  debug: false,
                },
              },
            },
          },
        };

  const restCacheProviderOptions =
    process.env.NODE_ENV !== 'production'
      ? {
          name: 'memory',
          options: {
            max: 32767,
            maxAge: 3600,
          },
        }
      : {
          name: 'redis',
          options: {
            max: 32767,
            connection: 'default',
          },
        };

  return {
    redis: redisConfig,

    'rest-cache': {
      config: {
        provider: restCacheProviderOptions,
        strategy: {}
     }
  }
};

Not pretty, but gets the job done.

derrickmehaffy commented 9 months ago

Not usually how I'd recommend doing that but that would work. If someone has a way to reproduce and show this plugin has a bug please let us know else for now I'm going to set this as resolved.