[fix] Changed getCacheKey to use the method name instead of hardcoding to get #94
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi,
I implemented my own "exists" method using the examples you outline here:
https://leqc.renoki.org/advanced/implement-caching/implementing-cache-for-other-functions-than-get
My code:
public function exists() { if (! $this->shouldAvoidCache()) { return $this->getFromQueryCache('exists'); } return parent::exists(); }
However, the getFromQueryCache ignores the $method when creating the cache key and always uses "get".
What happened was I ran the exists() first, which cached the query in Redis as a bool.
Then, when I ran the get() it found the cache entry and returned that and then Laravel blew up as it was expecting a collection , but it got a bool.
I changed the code so that the key will use the $method variable and it solved my problem. Now exists and get are different entries in the cache.
I would think this would be a widespread problem for anyone trying to implement methods (other than the get) as they would always be sharing the same "get" key in the cache. Of course, if the method they were using ultimately retrieved the "get" data from the cache it would be fine.
However, with exists it's just storing the result (boolean) in the cache which is problematic for subsequent calls with the same key that expect a collection (or some result other than a boolean).
Thanks!
-Rob