Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not clear cache when a relation has been updated #66

Open
willryanuk opened this issue Aug 18, 2023 · 8 comments · May be fixed by #86
Open

Does not clear cache when a relation has been updated #66

willryanuk opened this issue Aug 18, 2023 · 8 comments · May be fixed by #86
Assignees
Labels
issue: bug Something isn't working severity: medium source: plugin status: confirmed version: Strapi 4 Issues related to the versions supported by Strapi 4

Comments

@willryanuk
Copy link

One of my Collection Types (Broadcast) has a "relation" to a Host... However, when i update the Host (e.g. change their name), the Broadcasts endpoint does not update the relation to reflect the new name.

@mvaisberg
Copy link

hey @willryanuk did you find a way to clean the cache of the relations?

@willryanuk
Copy link
Author

@mvaisberg i think I either pressed "Purge REST Cache" on the parent collection, or perhaps just saved it, and that then regenerated the entry, and the cache was cleared.

@de-shine
Copy link

Can this case be automated to clear the cache for all the contents that have relation to that updated relation entry?

@Boegie19
Copy link
Member

Boegie19 commented Mar 15, 2024

@de-shine if the strategy.clearRelatedCache = true than this should already happen

still since multiple people are saying someting about it there mind be a bug in tbe logic

just as a extra note: it will clear everyting from any related contentypes

@jomarmontuya
Copy link

I've also encountered the same, when content relation is updated the cache where it was use is not purge automatically

@Boegie19 Boegie19 self-assigned this Mar 28, 2024
@Boegie19 Boegie19 linked a pull request Mar 28, 2024 that will close this issue
@derrickmehaffy derrickmehaffy added the version: Strapi 4 Issues related to the versions supported by Strapi 4 label Sep 24, 2024
@Mirasaki
Copy link

Mirasaki commented Feb 3, 2025

It's almost been a year, is there any status update on this issue? :)

@Mirasaki
Copy link

Mirasaki commented Feb 4, 2025

I found the issue after some debugging. Super weird, but the following line doesn't seem to finish execution - or is having some other issue.

If you use the following, the (stale) cache invalidation issue goes away:

    const response = await this.cache.del(key);
    return response;

I've created a pnpm patch for my project, and we no longer receive reports of caching issues. I hope this is useful to some of you :)

I will definitely update this comment if we find anything else, or start receiving reports again (over 16 hours with no new reports, was around 25~ per day before haha)

Edit:

It's actually much weirder... The fix wasn't the above alone, my patch still included the debugging which somehow fixed the issue?

Running locally with just the "fix" above doesn't resolve the issue. Running the following does seem to resolve it:

  /**
   * @param {string|string[]} key
   */
  async del(key) {
    console.log('MemoryCacheProvider.del#key', key);
    const debug = async (fn) => {
      if (key.startsWith('api/products') || key.startsWith('local-development-01/api/products')) {
        await fn()
      }
    }
    debug(() => console.log('MemoryCacheProvider.del#key', key));
    await debug(async () => console.log('MemoryCacheProvider.del#has', await this.has(key)));
    const response = await this.cache.del(key);
    debug(() => console.log('MemoryCacheProvider.del#response', response));
    await debug(async () => console.log('MemoryCacheProvider.del#postDeleteHas', await this.has(key)));
    return response;
  }

  async has(key) {
    return (await this.keys()).includes(key);
  }

I was just doing some things to try and debug the issue, and using the above in a pnpm patch does fix the issue. I am very confused haha. My best bet is that it is something to do with the keys iterator.

The cache seems to invalidate after the first fetch after deleting the cache key, which is already better than it not invalidating at all - we will use this for now and see if we find anything else.

@Mirasaki
Copy link

Mirasaki commented Feb 4, 2025

I could personally not verify that the getRelatedModelsUid has an issue with missing relations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Something isn't working severity: medium source: plugin status: confirmed version: Strapi 4 Issues related to the versions supported by Strapi 4
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants