Skip to content

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

Closed
wants to merge 3 commits into from

Conversation

hxnk
Copy link

@hxnk hxnk commented Mar 8, 2018

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 :)

@coveralls
Copy link

coveralls commented Mar 8, 2018

Coverage Status

Coverage decreased (-0.3%) to 99.392% when pulling 2e8b2d0 on hxnk:respect-isCachable into 7a7bed0 on GeneaLabs:master.

@mikebronner mikebronner self-requested a review March 8, 2018 13:38
$class = get_called_class();
$instance = new $class;

if (config('laravel-model-caching.disabled') || !$instance->isCachable) {
Copy link
Owner

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.

@mikebronner
Copy link
Owner

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. :)

@hxnk hxnk force-pushed the respect-isCachable branch from 23b44d7 to 2e8b2d0 Compare March 10, 2018 08:33
@hxnk
Copy link
Author

hxnk commented Mar 10, 2018

Hi @mikebronner, what do you think of this solution? It's a bit hacky, but it works :)

  • disableCache() temporarily disables cache in the config and returns a QueryOrModelCaller object, that is able to direct chained calls to the right objects (all() to Builder, rest to Model). This allows using a query scope and still chain the all() call, which would normally not be possible since a Builder object would be returned
  • in the QueryOrModelCaller object cache setting in the config is set back to its original state, so that subsequent queries aren't affected

@mikebronner
Copy link
Owner

mikebronner commented Mar 10, 2018

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.

@mikebronner
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants