renoki-co / laravel-eloquent-query-cache

Adding cache on your Laravel Eloquent queries' results is now a breeze.
Apache License 2.0
1.06k stars 118 forks source link

Invalidate after an upsert #132

Closed maher1337 closed 1 year ago

maher1337 commented 2 years ago

I'm trying to invalidate manually after an upsert as upsert is a Builder instance and would not trigger an "update" event for the model. Any workarounds?

rennokki commented 2 years ago

Can you share some of the code? I want to see which implementation you have been using.

dbfx commented 2 years ago

Just chiming in here as I believe I've had the same issue:

$attachment = Attachment::updateOrCreate(
    ['x_id' => $x->id, 'organisation_id' => $x->organisation_id],
    ['systems' => $systems, 'tags' => $tags, 'x_id' => $x->id, 'organisation_id' => $x->organisation_id]
);

I haven't gone deep to see if this is failing to invalidate cache but it looks like it is, and then I found this comment on upserts.

maher1337 commented 2 years ago

Yeah, upserts won't work. updateOrCreate requires a for loop with multiple UPDATE/INSERT transactions which could cause a heavy performance loss for say updating/inserting a 10^4 or more queries.

Upsert is a Query Builder instance and is not a Eloquent Builder instance. I'm looking for a functionality that would do something like $modelInstance->invalidateCache() and would cause that existing cache instance to be invalidated. Unfortunately I have not digged deep in the source code here but I'm willing to take sometime to do it if its non-existant.

rennokki commented 2 years ago

Upserts are two-in-one methods. Query Cache works only at the per-query level. 😀

fresent commented 1 year ago

The issue needs to be re-opened, as it's breaks updateOrCreate method.

We ran into this today, if you run updateOrCreate in a loop, the cache kicks in and caches the data, resulting in subsequent updates to fail from db insert.

stale[bot] commented 1 year ago

This issue has been automatically closed because it has not had any recent activity. 😨