Skip to content

This could be down to my weird use case #39

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
mbuk opened this issue Dec 14, 2017 · 9 comments
Closed

This could be down to my weird use case #39

mbuk opened this issue Dec 14, 2017 · 9 comments

Comments

@mbuk
Copy link

mbuk commented Dec 14, 2017

Issue

I have a complex query:

select * from `profiles` where (profiles.user_id not in(?) and profiles.active = 1)
                and (profiles.user_id in(select user_id from user_interests where interest_id in(?))
                    or profiles.user_id in(select user_id from user_locations where region_id in(?))
                        or profiles.user_id in(select user_id from profiles where country_id = ?))

which I call like this:

  $data['suggestedConnections'] = Profile::whereRaw('(profiles.user_id not in(?) and profiles.active = 1)
            and (profiles.user_id in(select user_id from user_interests where interest_id in(?))
                or profiles.user_id in(select user_id from user_locations where region_id in(?))
                    or profiles.user_id in(select user_id from profiles where country_id = ?))',
                array($excludedIds, $includedInterests, $includedRegions, $location))
            ->limit(10)
            ->inRandomOrder()
            ->get();

$excludedIds, $includedInterests, $IncludedRegions are all arrays of ids
and $location is an integer.

I really don't want this query to be cached (or maybe a better way to put it is that I want this query to be excluded from the query cache, but I want the rest of the queries on the model to be cached).

I'm wondering if anything using a raw method https://laravel.com/docs/5.5/queries#raw-expressions should bypass the cache?

Alternatively is there a way I can clear that cache or skip caching or a given query?

Environment

Laravel Version: 5.5
Laravel Model Caching Package Version: 0.2.11
PHP Version: 7.1.10
Homestead Version: N/A
Operating System & Version: OSX High Sierra

@mikebronner
Copy link
Owner

I’m working on a feature for this. Will try to work on it tomorrow.

@mikebronner
Copy link
Owner

Duplicate of #34.

@mikebronner
Copy link
Owner

Hi Matt, just wanted to note that the model cache will respect your entire complex where clause will be processed accordingly and not cause false returns, even with dynamic data.

But I do understand the need to prevent caching of a query and will work on that, just wanted to share that the caching should be able to handle your use case. :)

@mbuk
Copy link
Author

mbuk commented Dec 14, 2017

Cool thanks @mikebronner - good to know - I think something else is a play here on my end which is causing odd results :)

@mbuk
Copy link
Author

mbuk commented Dec 14, 2017

So I'm definitely seeing some unexpected behaviour here.
The use case is - a list of suggestions filtered on yourself, your already connected / invited users.
When my Profile model is extending CachedModel list of suggestions doesn't decrease when I connect with another user. When Profile model isn't extending CachedModel it behaves as expected.

If I mess around manually with my excluded $excludedIds - when caching is on - the list doesn't change - when caching is off it's as expected. It's 04:30 here so going to get some rest - but I'll put together a use case on fresh install of laravel later today and see if I can replicate the some issue on that with a base use case.

The key being created by the package is:
appprofile_and_profilesuser-id-not-in-and-profilesactive-1-and-profilesuser-id-inselect-user-id-from-user-interests-where-interest-id-in-or-profilesuser-id-inselect-user-id-from-user-locations-where-region-id-in-or-profilesuser-id-inselect-user-id-from-profiles-where-country-id_orderByRaw_rand-limit_10 and I don't see that change too much unless I adjust profile.active = 0 when it becomes: appprofile_and_profilesuser-id-not-in-and-profilesactive-0-and-profilesuser-id-inselect-user-id-from-user-interests-where-interest-id-in-or-profilesuser-id-inselect-user-id-from-user-locations-where-region-id-in-or-profilesuser-id-inselect-user-id-from-profiles-where-country-id_orderByRaw_rand-limit_10

@mbuk
Copy link
Author

mbuk commented Dec 14, 2017

I was tired and an idiot 🥇 fool!
I'd messed my bindings up......... sorry!

@mikebronner
Copy link
Owner

No worries, glad you found the root cause :)

@mikebronner
Copy link
Owner

@mbuk I just released version 0.2.12 which adds the ability to disable caching on individual queries.

@mbuk
Copy link
Author

mbuk commented Dec 14, 2017

Fantastic! I’ll check it out

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

No branches or pull requests

2 participants