mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.23k stars 212 forks source link

Is possible to prevent relationship cache #375

Closed rodrigowbazevedo closed 3 years ago

rodrigowbazevedo commented 3 years ago

Is your feature request related to a problem? Please describe. My cached model is caching a relationship that that should not be cached.

Describe the solution you'd like A optional protected attribute to prevent relationships cache.

Describe alternatives you've considered Maybe a method to map the attributes that should be cached.

mikebronner commented 3 years ago

Hi @rodrigowbazevedo, unfortunately that is not how this package works. It caches Eloquent query results based on queries run, so if you ran a query, it will cache it as is. You would need to adjust how you run your queries in order to prevent certain aspects of them from being cached.

Are you seeing caching behavior that is leading to unexpected results? If that is the case, please open a new issue and describe the exact Eloquent queries you are running, along with the results. Even better would be to submit a failing unit test, if possible.

rodrigowbazevedo commented 3 years ago

Hi @rodrigowbazevedo, unfortunately that is not how this package works. It caches Eloquent query results based on queries run, so if you ran a query, it will cache it as is. You would need to adjust how you run your queries in order to prevent certain aspects of them from being cached.

Are you seeing caching behavior that is leading to unexpected results? If that is the case, please open a new issue and describe the exact Eloquent queries you are running, along with the results. Even better would be to submit a failing unit test, if possible.

So it's happening because I'm using eager loading? If I do my model query without it it'll cache only my model?

The package cache the raw query result?

mikebronner commented 3 years ago

@rodrigowbazevedo Unfortunately without seeing your code and understanding exactly what it is you are trying to achieve, I can't tell what would be causing your issue. This package is caching the exact eloquent query results, not the query itself.

That being said, if you are eager-loading a relationship that is not being used, it would cache that relationship, even though you aren't using it.

rodrigowbazevedo commented 3 years ago

I think I understood, for example:

$model = Model::with('relationship')->find($id);

It's going to cache model with model.relationship

$model = Model::find($id);

It's going to cache only model

$model = Model::find($id);
$model->load('relationship');

It's going to cache only model? Or both?

mikebronner commented 3 years ago

@rodrigowbazevedo Your are correct with the first two examples. The third example executes two separate queries, so the find query would not have it, but the load query would have it. :)

rodrigowbazevedo commented 3 years ago

There's a way to prevent my relationship to be cached? The only way I though is to do something like this, and I still don't know if it'll work

$model = Model::find($id);
$model->disableModelCaching()->load('relationship');
mikebronner commented 3 years ago

Why do you want it to not be cached? I'm trying to understand your reasoning for this. I think disableModelCaching() will work with load(), but not sure.

rodrigowbazevedo commented 3 years ago

I'm getting outdated data, let's say my Model is a client, each client is queried regularly but not modified so it's a good idea to cache it, but each client has one points account with a balance, these balance may change for a lot of reasons and I need to always show the correct account balance so I don't want to cache it.

mikebronner commented 3 years ago

The caches get cleared if data changes. That is not something you need to worry about. Please open a new issue describing the eloquent queries you are using that provide stale results, as well as the model definitions.

rodrigowbazevedo commented 3 years ago

Maybe it's because my PointsAccount model is not using the Cachable trait only my Client model, so if my PointsAccount model is modified it'll not clear the cache, but it got cached version when I query my Client model.

So maybe the solution is to add the Cachable trait to my PointsAccount model?

mikebronner commented 3 years ago

@rodrigowbazevedo Sure, that is certainly possible. However, I would like to isolate the actual issue you are experiencing, because it should be possible to not cache relationships.