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

not reset cache when click Purge rest cache #23

Open
haidvams opened this issue May 12, 2022 · 29 comments
Open

not reset cache when click Purge rest cache #23

haidvams opened this issue May 12, 2022 · 29 comments
Labels
issue: bug Something isn't working severity: medium source: plugin status: pending reproduction version: Strapi 4 Issues related to the versions supported by Strapi 4

Comments

@haidvams
Copy link

I can rest by button "Purge rest cache"
and what option to rest cache auto when I update a post?
thank all

@jsanta
Copy link

jsanta commented Jul 14, 2022

Facing the same issue, Rest cache is not being purged when pressing the delete button. In our case we are using the Redis cache store.
Applying some debugging, on both Strapi and Redis sides, I noticed that the key being deleted is not the actual key that has to be deleted.

Eg.:

  • Key being cached strapi:development:strapi-app1:/api/testimonials?filters={"application":{"name":{"$eq":"app1"}}},sort=["id:asc"]&
  • Key trying to be deleted (fails): /api/testimonials?filters={"application":{"name":{"$eq":"app1"}}},sort=["id:asc"]&

In our configuration we are using a keyPrefix.
The cache is actually working for caching requests (RECV works as expected), but the purge cache is failing.

@jsanta
Copy link

jsanta commented Jul 14, 2022

Just figured out a workaround that worked for us.
For some strange reason I can't figure out the keys() function on the strapi-plugin-rest-cache/server/services/cacheStore.js has the following return code:

return keys .filter((key) => keysPrefixRe.test(key)) .map((key) => key.replace(keysPrefixRe, ''));

The keys function is used only on the purge and reset functions.
Commenting the .map line solves the delete issue.

@jpizzati-uvl
Copy link

@jsanta thanks for looking into this! Do you think of maybe submitting a PR for for dealing with this issue?

@jsanta
Copy link

jsanta commented Aug 12, 2022

@jpizzati-uvl truth is I ended up patching the package (as suggested on the Strapi documentation). It was way easier, besides I had to do some other custom modifications.

https://gist.github.com/jsanta/75d00e65793f4c6adcc765132fe39369

@deepakgupta-unthinkable

@jsanta not resetting the cache on the list API of a content type.Any solution for that? I am using Redis cache. Earlier i was using memory cache and it was working perfectly but when switched to Redis cache it is not working on list api

@jsanta
Copy link

jsanta commented Sep 16, 2022

@deepakgupta-unthinkable try the patch I posted in previous comments. I think that should work. If it doesn't you'll have to make further debugging in both Redis and Strapi sides, and also check you plugin configuration.

@jpizzati-uvl
Copy link

jpizzati-uvl commented Sep 19, 2022

@jsanta I tried your patch but not working with in-memory cache

@jsanta
Copy link

jsanta commented Sep 20, 2022

@jpizzati-uvl sorry about that. Cannot help you further, as even though the patch tries to be generic enough for most use cases, your own mileage may vary and most likely require specific debugging.

@dinhkhanh
Copy link

@jsanta you should not patch that way. Instead, fix the del function.
Check my pull request.

@jsanta
Copy link

jsanta commented Sep 22, 2022

@dinhkhanh it depends. i also required some other modifications for particular use cases I'm facing. Agreed that a pull request is better for general improvements, but forking and compiling the plugin for very specific business requirements is an overkill.

@KRMTL
Copy link

KRMTL commented Sep 23, 2022

Still bugged with in-memory and redis cache using patch.

Backend:
contentTypes: [
"api::novel.novel",
"api::chapter.chapter",
"api::tag.tag",
],

Frontend:
pages/novel/[slug]/[id]
/api/novels/?filters[slug][$eq]=${context.params.slug}&populate=*

/novel/, [slug], and [id] are caching. purging works, but it's ignoring [id] cache that's deeper inside-in particular the contents that are being filtered.

@jpizzati-uvl
Copy link

It's working, I rolled back modifications to my controller and made request on the from nextjs app:

Request: GET /api/pages?filters[uri][$eq]=/

Screen Shot 2022-09-30 at 13 34 38

[PURGING KEY] is now correctly estructured and cached entry is cleared.

@thibaultcarlier
Copy link

Hello,

I would like to understand, cache purging does not work naturally with redis? I tried after many attempts the button never works. If I do the purge with an API call directly "/rest-cache/purge" I always have an empty array.
The cache button does not appear in "production" mode for strapi. Except that I would like it to appear.

Do you have some answers for me? It will help me understand better.

Thanks a lot

@SanchoPanzo
Copy link

In my case, the problem is that in the request I used an additional slash (/).
Due to this slash, the regular expression could not find any matches and was unable to clear the cache.

An example of an incorrect request would be "/api/docs/?populate=deep,2" while an example of a correct request would be "/api/docs?populate=deep,2".

Once I corrected this, the cache was successfully cleared through the use of the "purge button."

@stafyniaksacha
Copy link
Member

An example of an incorrect request would be "/api/docs/?populate=deep,2" while an example of a correct request would be "/api/docs?populate=deep,2".

This may be a PR to match trailing slashes (https://github.com/strapi-community/strapi-plugin-rest-cache/blob/main/packages/strapi-plugin-rest-cache/server/utils/config/getRouteRegExp.js#L16)

@Shekhar-Zealous
Copy link

Shekhar-Zealous commented Oct 5, 2023

Below of the links suggest that the purge cache issue from Content type was fixed but actually the issue still persist.

Even today I am facing this issue, the cache is not cleared after clicking on Purge cache button from the Content type and from inside of any particular content.

Is there any latest update related to this issue from Strapi community?

Addendum, is there any other way to apply caching for custom added routes?

@web-admin
Copy link

I have a similar issue but only when using "routes -> path". Cache is saving well in Redis and is automatically deleted after chosen time but... cache is not purged when I update an entry OR click PURGE button in CMS. Everything works fine if I do not use "routes" option. Any thoughts?

@girish-soman
Copy link

I have a similar issue but only when using "routes -> path". Cache is saving well in Redis and is automatically deleted after chosen time but... cache is not purged when I update an entry OR click PURGE button in CMS. Everything works fine if I do not use "routes" option. Any thoughts?

I encountered a similar challenge in one of my strapi project and managed it by establishing a connection between the Strapi server and the Redis instance. Leveraging Strapi's lifecycle hooks (afterCreate, afterUpdate, afterDelete, afterDeleteMany), I successfully implemented a cache flush mechanism. This solution effectively manages cache updates in response to CRUD operations, ensuring optimal system performance.
Maybe this will help you with your use case too.

@Shekhar-Zealous
Copy link

@girish-soman Thanks for the feedback.

I understood the implementation you did in lifecycle but can you show us which redis method did you use flushing caching?

@girish-soman
Copy link

@girish-soman Thanks for the feedback.

I understood the implementation you did in lifecycle but can you show us which redis method did you use flushing caching?

@Shekhar-Zealous I was using redis.flushall() which gets triggered on the above lifecycle hooks and clears the entire Redis cache.
Thanks

@Shekhar-Zealous
Copy link

Shekhar-Zealous commented Jan 22, 2024

@girish-soman How come you used redis.flushall() using this package https://market.strapi.io/plugins/strapi-plugin-rest-cache?
Does this really work!

Would really appreciate if you share us an example here.

@girish-soman
Copy link

@girish-soman How come you used redis.flushall() using this package https://market.strapi.io/plugins/strapi-plugin-rest-cache? Does this really work!

Would really appreciate if you share us an example here.

Hey @Shekhar-Zealous, sharing the code snippet,

Step 1:
Install this npm package to establish connection from strapi server and redis instance "npm i ioredis".
You can follow the package documentation from here

Step 2:
Create a file named "redis.js" and define an async function called flushCache().

Sharing you a sample code.

const Redis = require('ioredis');

async function flushCache() {
    const redis = new Redis({
        host:process.env.REDIS_DATABASE_HOST,
        port:process.env.REDIS_DATABASE_PORT,
    });
    try {
        // Flushes all the cached data from redis
        await redis.flushall();
        console.log('Cache flushed successfully.');
    } catch (error) {
        console.error('Error flushing cache:', error.message);
    } finally {
        // Close the Redis connection
        redis.quit();
    }
}

module.exports = { flushCache };

Step 3:
Inside your targeted API content type folder, create a lifecycles.js (e.g., src/api/pages/content-types/pages/lifecycles.js), Define your hooks to trigger the flushCache():

Eg

const {flushCache} = require("path-to-your-redis.js-file")
module.exports = {
    async afterUpdate() {
        await flushCache();
    }
};

Sharing the lifecycles documentation here

This works well on my use case.

Hope this will help you too!

Thanks!

@web-admin
Copy link

@girish-soman I'd say it's to wide workaround as it will flush WHOLE cache saved in Redis, not the chosen entry or collection. I think that there is a problem with routes path saved in Redis as a key. Saving works fine but then the plugin cannot find the proper key to delete it. No doubt it's a bug as solution WITHOUT routes works well.

@girish-soman
Copy link

girish-soman commented Jan 22, 2024

@girish-soman I'd say it's to wide workaround as it will flush WHOLE cache saved in Redis, not the chosen entry or collection. I think that there is a problem with routes path saved in Redis as a key. Saving works fine but then the plugin cannot find the proper key to delete it. No doubt it's a bug as solution WITHOUT routes works well.

Yes @web-admin , this is the only option I could figure out. Until the plugin itself fixes this, though, it works for my use case.

@Shekhar-Zealous
Copy link

@girish-soman I will take a look at the solution you gave. Many thanks.

I agree with @web-admin

@web-admin
Copy link

@derrickmehaffy Is it possible to look up this issue? Definitely there is an issue with resetting cache when "routes" are enabled.

@LBC000
Copy link

LBC000 commented Mar 1, 2024

Delete Redis data based on the specified key at the points of updating and deleting during the mode lifecycle.

// redis.ts
import Redis from "ioredis";

export const redisClient = new Redis({
  port: 6379,
  host: "127.0.0.1",
});

export async function deleteKeysWithPattern(pattern) {
  const keysToDelete = await redisClient.keys(pattern);

  if (keysToDelete.length > 0) {
    await redisClient.del(keysToDelete);
    console.log(`Deleted keys matching pattern '${pattern}':`, keysToDelete);
  } else {
    console.log(`No keys found matching pattern '${pattern}'`);
  }
}
// lifecycles.ts
import { deleteKeysWithPattern } from "../../../../utils/redis";

export default {
  afterUpdate(event) {
    deleteKeysWithPattern(`*api\\\\${event.model.info.singularName}*`);
  },
  afterUpdateMany(event) {
    deleteKeysWithPattern(`*api\\\\${event.model.info.singularName}*`);
  },

  afterDelete(event) {
    deleteKeysWithPattern(`*api\\\\${event.model.info.singularName}*`);
  },
  afterDeleteMany(event) {
    deleteKeysWithPattern(`*api\\\\${event.model.info.singularName}*`);
  },
};

image

// plugins.ts
contentTypes: [
  "api::notice.notice",
  "api::article.article",
  "api::global-config.global-config",
],

@LBC000
Copy link

LBC000 commented Mar 12, 2024

my-purge-rest-cache.zip
image
image

@Shekhar-Zealous
Copy link

@LBC000 Is this solution working after directly making changes to the Package?

@derrickmehaffy derrickmehaffy added the version: Strapi 4 Issues related to the versions supported by Strapi 4 label Sep 24, 2024
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: pending reproduction version: Strapi 4 Issues related to the versions supported by Strapi 4
Projects
None yet
Development

No branches or pull requests