strapi-community / strapi-plugin-rest-cache

Speed-up HTTP requests with LRU cache.
https://strapi-community.github.io/strapi-plugin-rest-cache/
MIT License
129 stars 29 forks source link

Redis Cache Does not Seem to do anything #20

Closed jordanvector closed 2 years ago

jordanvector commented 2 years ago

On the latest version of the associated plugins I think

Current strapi version 4.1.7 node v14

"strapi-plugin-redis": "^1.0.0-alpha.2",
"strapi-plugin-rest-cache": "^4.2.4",
"strapi-provider-rest-cache-redis": "^4.2.4",

with a configuration block that looks like this

    redis: {
      config: {
        connections: {
          default: {
            connection: {
              host: process.env.REDIS_HOST,
              port: process.env.REDIS_PORT,
              db: process.env.REDIS_DB,
              password: process.env.REDIS_PW
            },
            settings: {
              debug: false,
              enableEtag: true,
              enableXCacheHeaders: true,
              resetOnStartup: true
            },
          },
        },
      },
    },
    'rest-cache': {
      config: {
        provider: {
          name: "redis",
          options: {
            max: 32767,
            connection: "default",
          },
        },
        strategy: {
          contentTypes: [
            "api::article.article",
          ],
        },
      },
    },

However this does not seem to do anything, nothing is being cached in redis and I am not sure why

stafyniaksacha commented 2 years ago

hello @jordanvector, can you paste the output with debug mode enabled? DEBUG=strapi:strapi-plugin-rest-cache yarn strapi develop

jordanvector commented 2 years ago

[2022-04-08 10:45:19.445] info: [slugify] graphql detected, registering queries
  strapi:strapi-plugin-rest-cache [STRATEGY]: CachePluginStrategy {
  strapi:strapi-plugin-rest-cache   debug: false,
  strapi:strapi-plugin-rest-cache   enableEtag: false,
  strapi:strapi-plugin-rest-cache   enableXCacheHeaders: false,
  strapi:strapi-plugin-rest-cache   enableAdminCTBMiddleware: true,
  strapi:strapi-plugin-rest-cache   resetOnStartup: false,
  strapi:strapi-plugin-rest-cache   clearRelatedCache: true,
  strapi:strapi-plugin-rest-cache   maxAge: 3600000,
  strapi:strapi-plugin-rest-cache   keysPrefix: '',
  strapi:strapi-plugin-rest-cache   contentTypes: [
  strapi:strapi-plugin-rest-cache     CacheContentTypeConfig {
  strapi:strapi-plugin-rest-cache       singleType: false,
  strapi:strapi-plugin-rest-cache       injectDefaultRoutes: true,
  strapi:strapi-plugin-rest-cache       maxAge: 3600000,
  strapi:strapi-plugin-rest-cache       hitpass: [Function: hitpass],
  strapi:strapi-plugin-rest-cache       keys: [CacheKeysConfig],
  strapi:strapi-plugin-rest-cache       plugin: undefined,
  strapi:strapi-plugin-rest-cache       routes: [Array],
  strapi:strapi-plugin-rest-cache       contentType: 'api::article.article',
  strapi:strapi-plugin-rest-cache       relatedContentTypeUid: [Array]
  strapi:strapi-plugin-rest-cache     }
  strapi:strapi-plugin-rest-cache   ],
  strapi:strapi-plugin-rest-cache   keys: CacheKeysConfig { useHeaders: [], useQueryParams: true }
  strapi:strapi-plugin-rest-cache } +0ms
[2022-04-08 10:45:22.997] info: REST Cache provider "redis": connection established
  strapi:strapi-plugin-rest-cache [REGISTER] api::article.article routes middlewares +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] POST /api/articles purge +1ms
  strapi:strapi-plugin-rest-cache [REGISTER] DELETE /api/articles/:id purge +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] PUT /api/articles/:id purge +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] GET /api/articles recv maxAge=3600000 +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] GET /api/articles/:id recv maxAge=3600000 +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] admin routes middlewares +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] POST /content-manager/single-types/:model/actions/publish purge-admin +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] POST /content-manager/single-types/:model/actions/unpublish purge-admin +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] POST /content-manager/collection-types/:model purge-admin +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] POST /content-manager/collection-types/:model/:id/actions/publish purge-admin +1ms
  strapi:strapi-plugin-rest-cache [REGISTER] POST /content-manager/collection-types/:model/:id/actions/unpublish purge-admin +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] POST /content-manager/collection-types/:model/actions/bulkDelete purge-admin +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] PUT /content-manager/single-types/:model purge-admin +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] PUT /content-manager/collection-types/:model/:id purge-admin +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] DELETE /content-manager/single-types/:model purge-admin +0ms
  strapi:strapi-plugin-rest-cache [REGISTER] DELETE /content-manager/collection-types/:model/:id purge-admin +0ms
[2022-04-08 10:45:23.002] info: Using REST Cache plugin with provider "redis"

 Project information                                                          

┌────────────────────┬──────────────────────────────────────────────────┐
│ Time               │ Fri Apr 08 2022 10:45:25 GMT-0400 (Eastern Dayl… │
│ Launched in        │ 6892 ms                                          │
│ Environment        │ development                                      │
│ Process PID        │ 84108                                            │
│ Version            │ 4.1.7 (node v17.8.0)                             │
│ Edition            │ Enterprise                                       │
└────────────────────┴──────────────────────────────────────────────────┘

 Actions available                                                            

Welcome back!
To manage your project 🚀, go to the administration panel at:
http://localhost:1337/admin

To access the server ⚡️, go to:
http://localhost:1337

  strapi:strapi-plugin-rest-cache [RECV] GET /api/articles?& MISS +0ms
[2022-04-08 10:46:06.608] http: GET /api/articles (204 ms) 403
  strapi:strapi-plugin-rest-cache [RECV] GET /api/articles?& HITPASS +8s
[2022-04-08 10:46:14.517] http: GET /api/articles (318 ms) 200
  strapi:strapi-plugin-rest-cache [RECV] GET /api/articles?& HITPASS +6s
[2022-04-08 10:46:20.570] http: GET /api/articles (309 ms) 200

Ok after turning on debug i think i see whats going on, is that all of my api calls are getting HITPASS and not caching it I think? Is there a way to turn that off like the strapi v3 module?

jordanvector commented 2 years ago

Ok great call adding the debug env var, that helped me identify the issue, here is my new config which is now caching as expected. I think we can call this one closed

    redis: {
      enabled: true,
      config: {
        connections: {
          default: {
            connection: {
              host: process.env.REDIS_HOST,
              port: process.env.REDIS_PORT,
              db: process.env.REDIS_DB,
              password: process.env.REDIS_PW
            },
            settings: {
              debug: false,
            },
          },
        },
      },
    },
    'rest-cache': {
      enabled: true,
      config: {
        provider: {
          name: "redis",
          options: {
            max: 32767,
            connection: "default",
          },
        },
        strategy: {
          enableEtag: true,
          enableXCacheHeaders: true,
          resetOnStartup: true,
          hitpass: false,
          contentTypes: [
            "api::article.article",
          ],
        },
      },
    },
stafyniaksacha commented 2 years ago

Ok great call adding the debug env var, that helped me identify the issue, here is my new config which is now caching as expected. I think we can call this one closed

Yes by default hitpass don't cache requests that contains cookie or authorization header!