-
Notifications
You must be signed in to change notification settings - Fork 231
dont cache if model::$isCachable == false #97
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
Conversation
$class = get_called_class(); | ||
$instance = new $class; | ||
|
||
if (config('laravel-model-caching.disabled') || !$instance->isCachable) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that isCachable
can never be set for the all()
method, since setting isCachable
is set through a query scope, which returns a Builder object, which doesn't have an all method. Consider that the following code throws an exception:
$model->disableCache()->all();
Unfortunately the only way to disable caching on all()
queries is to turn it off in config at this point, or change to get()
. Perhaps you have some thoughts on how we can disable the cache on the model? In the past, I've had a method on the model, instead of a scope, and that had more problems than it solved, so I moved to scope.
Hi @hxnk, thank you for submitting this PR! :) I left a comment explaining why this code won't work at this point. Please let me know if you have additional ideas. :) |
23b44d7
to
2e8b2d0
Compare
Hi @mikebronner, what do you think of this solution? It's a bit hacky, but it works :)
|
Hi @hxnk, I initially thought that might work as well, but then realized that any global method of disabling cache would cause unexpected behavior for other possible simultaneous request. To avoid uninterrupted side-effects, we will have to find a solution that works on the model instance. |
Hi @hxnk -- I was able to come up with a solution! :) Thanks for keeping the discussion going around this challenge and prompting me to think about different ways of achieving this! |
When calling model::all(), the $isCachable property on the model wasn't checked, resulting in model:all() results to be cached, even when $isCachable was set to false.
This PR attempts to fix that :)