mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 217 forks source link

Generated cache tag does not respect dynamic where clauses #113

Closed madsem closed 6 years ago

madsem commented 6 years ago

Question

Querying across multiple children, was hoping that laravel-model-caching would actually generate a dynamic cache tag based on the query conditions. But the result is that it seems to simply use model names to build the string?

return $this->domain->load([
                    'advertisers' => function ($q) {
                        $q->with([
                            $this->adsRelationship => function ($q) {
                                $q->whereHas('countries', function ($q) {
                                    $q->whereIn('key', [session('country_code'), 'XX']);
                                })
                                  ->whereHas('operatingSystems', function ($q) {
                                      $q->whereIn('key', [session('os'), 'all']);
                                  })
                                  ->whereHas('deviceTypes', function ($q) {
                                      $q->whereIn('key', [session('device_type'), 'all']);
                                  })
                                  ->whereHas('browsers', function ($q) {
                                      $q->whereIn('key', [session('browser'), 'all']);
                                  })
                                  ->when($this->adType == 'images', function ($q) {
                                      $q->with([
                                          'cpaBanners' => function ($q) {
                                              $q->whereHas('adSize', function ($q) {
                                                  $q->where('size', '=',
                                                      $this->width . 'x' . $this->height);
                                              });
                                          }
                                      ]);
                                  })
                                  ->when($this->adType == 'contextuals', function ($q) {
                                      $q->whereHas('adSize', function ($q) {
                                          $q->where('size', '=',
                                              $this->width . 'x' . $this->height);
                                      });
                                  })
                                  ->when($this->adType == 'urls', function ($q) {
                                      $q->doesnthave('cpaBanners');
                                  })
                                  ->when($this->adType == 'snippets', function ($q) {
                                      $q->doesnthave('adSize');
                                  });
                            }
                        ]);
                    }
                ]);

In short: Can it handle queries like the above so that it actually builds multiple versions of the cache for each of the results this query can produce? (country, device type etc)

What I'd like to achieve is that the results are cached, but different versions based on domain, country, browser, device type, OS and ad type. And then the correct version is automatically delivered from cache to visitors that have the same environment variables, or hit database if not.

Hope I am explaining this good enough :)

Or am I way off here and this can only work by manually creating a cache key and disabling caching for this particular query?

mikebronner commented 6 years ago

No, I believe this is a current bug, probably the similar or same as #111.

mikebronner commented 6 years ago

@madsem Looking at this more closely now, I believe this is because you are running the queries in the load method. Right now the load method does not get cached. I'm undecided if I want to have that be cached or not, so far I have not had a use-case for it. I would recommend for you to run all your querying outside the load method.

mikebronner commented 6 years ago

Closing for now ... decided not to implement caching in the load method, at least for now.