patrixr / strapi-middleware-cache

:electric_plug: A cache middleware for https://strapi.io
MIT License
290 stars 58 forks source link

Single type has many relation data update doesn't bust the cache #65

Open lukawski opened 2 years ago

lukawski commented 2 years ago

Hello, thanks for this great package! I noticed that if I have a single type model with hasMany relation, updating data in the related model doesn't bust this single model cache.

That's how my relation looks like. image

I wonder, is there any way to make it work? Or it would require changes in the middleware code itself?

patrixr commented 2 years ago

Hi @lukawski

Have you tried setting the clearRelatedCache option in the configuration ?

From the readme

By setting the clearRelatedCache to true, the middleware will inspect the Strapi models before a cache clearing operation to locate models that have relations with the queried model so that their cache is also cleared (this clears the whole cache for the related models). The ispection is performed by looking for direct relations between models and also by doing a deep dive in components, looking for relations to the queried model there too.

lukawski commented 2 years ago

Hi @patrixr. Yes, I do have clearRelatedCache set to true. I should probably include in in my original question, here is my config

cache: {
      type: 'redis',
      enabled: true,
      clearRelatedCache: true,
      populateStrapiMiddleware: true,
      models: [
        {
          model: 'global',
          singleType: true,
        },
        'category',
      ],
      redisConfig: {
        host: process.env.REDIS_HOST,
        port: process.env.REDIS_PORT,
      },
    },

And here a the versions of Strapi and cache middleware

I also took a dive into middleware code and I see that getRelatedModels loops over model attributes to look for related models. I console log in this loop and I can confirm that when I update the entry in categories collection there is no global attribute relation. I guess that's because of how Strapi creates relations on the global model, since its single type it's one-sided I guess. So we only have information about this relation on the Global model but not on the Category.

stafyniaksacha commented 2 years ago

Hello! Can you try to update strapi-middleware-cache to latest version (2.1.1) ?

lukawski commented 2 years ago

Hi @stafyniaksacha, yes, I will, I wanted to wait with it till we have more time on a project but it might be a good idea to do it right now. I will get back to you when I do!

lukawski commented 2 years ago

@stafyniaksacha so after the update same behavior occurs. When I do update an entry in categories collection cache for the global model is not cleared.