-
Notifications
You must be signed in to change notification settings - Fork 220
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
Global scopes do not work with cachable models #106
Comments
Hi @scottgrayson, thanks for reporting this use-case. I don't think this package is a good fit for you, if you only want to cache a single query in your entire app. You would be better off writing the cache functionality manually. However, if you are using this on all other models, but only wish to have special treatment on a single model, my suggestion would be similar to above: do not make that model Cachable, but instead manually cache that one query as needed. I will look into why the scope would not be working and see if there is a fix for that. |
PS: have you tried using a local scope on the model? |
This describes my situation. I'm working on caching it manually now, but if I could use your package for users as well, it would be a cleaner solution for me.
I have tried a local scope and it does work. public function scopeNocache($query)
{
return $query->disableCache();
} this is not very useful though. is there a different way you thought of using a local scope? |
Hmm, I see. It will take some time for me to evaluate and research this, as we have Easter weekend coming up, and I will be out of town. I hope to get to it within the next week or so. I will post back here when I have an update. :) |
I also noticed this issue when using the Clockwork package. However in this case, it actually throws an error instead of silently failing. The error is thrown when any model with the
Help with this would be much appreciated, I'm a big fan of this package 😄 |
Everyone active on this issue - I am currently investigating the global scope It seems the way that the cache key is formed is already taking any scopes that have been applied into account. So global scopes will be caught. The problem you are facing here is only limited to the $this->isCachable = false; it seems to fix the problem. @mikebronner Can you confirm this will not break other functionality? I will create a PR and you can close if it breaks something I don't know about. |
@mycarrysun Thanks for looking into this!!! :) |
@scottgrayson @jcsoriano @sgtaziz Can you test if this is now fixed in the latest release, 0.4.12 or newer? I believe this is working now. If you are still having issues, please re-open. |
Issue
I would like to have a cache for users.index queries because they take a while. I want to ->disableCache() on all other queries. I tried a global scope that disables cache but found that global scopes do not work with models that use the
Cachable
trait.This does not work:
enabled
nornocache
scopes work hereenabled
works if I remove theCachable
traitdd('hi');
output if i uncomment it (shows that the callback given to addGlobalScope is never applied)Environment
Laravel Version: 5.6.10
Laravel Model Caching Package Version: 0.2.52
PHP Version: 7.1.11
Valet Version: 2.0.7
Operating System & Version: MacOS 10.12.6
Stack Trace
N\A
The text was updated successfully, but these errors were encountered: