-
Notifications
You must be signed in to change notification settings - Fork 53
Bust cache of specific endpoint when PUT/DELETE request with other endpoint #48
Comments
Hi @ViVa98 The version of the middleware that you are using does not support anything like that, however @stafyniaksacha has been working on a v2 (you can check out the Beta branch, which contains a great amount of new features, including the ability to set custom routes, which might help you fix your issue ! You can try installing it by using However note that this version's configuration is not backwards compatible with v1, so do take a look at the documentation on that branch to learn how to configure it. Hope this helps ! |
@patrixr thanks for the reply and info about the beta version. There is an option to specify a particular API endpoint to bust when a PUT/DELETE request comes for the same endpoint. What I'm looking for is to bust a specific endpoint cache when PUT/DELETE request for another endpoint. Requesting @stafyniaksacha to please go through the above-mentioned use case and also to provide a brief/one-line description for the beta version. |
Hello @ViVa98 Let's say you have this cache config: // file: config/cache.js
/**
* @type {import('strapi-middleware-cache').UserMiddlewareCacheConfig}
*/
module.exports = {
enabled: true,
clearRelatedCache: true,
withStrapiMiddleware: true,
models: [
{
model: "profile",
injectDefaultRoutes: false,
routes: [
"/profiles/:slug",
],
},
],
}; this will register only // file: api/profile/models/profile.js
/**
* Lifecycle callbacks for the `profile` model.
*/
async function clearProfileCache(data) {
const cache = strapi?.middleware?.cache || {};
if (cache && typeof cache.clearCache === "function") {
const profileCache = cache.getCacheConfig("profile");
if (profileCache && typeof data.slug === "string") {
await cache.clearCache(profileCache, { slug: data.slug });
return;
}
}
}
module.exports = {
lifecycles: {
async afterDelete(result, data) {
try {
await clearProfileCache(result);
} catch (error) {
strapi.log.error("profile afterDelete:clearProfileCache");
strapi.log.error(error);
if (
typeof strapi.plugins?.sentry?.services?.sentry?.sendError ===
"function"
) {
strapi.plugins.sentry.services.sentry.sendError(error);
}
}
},
async afterUpdate(result, params, data) {
try {
await clearProfileCache(result);
} catch (error) {
strapi.log.error("profile afterUpdate:clearProfileCache");
strapi.log.error(error);
if (
typeof strapi.plugins?.sentry?.services?.sentry?.sendError ===
"function"
) {
strapi.plugins.sentry.services.sentry.sendError(error);
}
}
},
},
}; |
And also for the |
Sorry, I don't understand what you are expecting. Can you provide more information of what you need and your current configuration?
|
You're right about populating paramNames from the cache config but in my case, I configured two models with a route in each one.
After the below line in
If you observe |
For some reason,
After this modification cache is busting when PUT/DELETE request is completed. @stafyniaksacha thank you. |
Hum, this is wired. The Can you log the Also, we will have more information using // file: config/middleware.js
module.exports = ({ env }) => ({
settings: {
logger: {
level: "debug",
exposeInContext: true,
},
cache: {
// ...
},
},
}); |
Response from the cache.options using basic
Response using
|
I have a model with 3 fields containing {id(auto-generated), username, email}
I have modified the default GET endpoint to "username" instead of "id" to search specific record based on username like
GET /profiles/someusername
But for PUT/DELETE I'm using "id" as the endpoint
PUT/DELETE /profiles/random_generated_id
Whenever I use PUT/DELETE, data changes in the database, the previously cached data of different endpoint is not busting. I'm thinking of extending those PUT/DELETE API and manually deleting the specific endpoint's cache.
Help me get out of this situation.
The text was updated successfully, but these errors were encountered: