// advanced
Caching
Cache the discovered filters in production and know when the cache needs to be regenerated.
Why cache
To find your filters, the package reads the attributes of the model's methods every time the queryString() scope runs. The cost is small, but in production you can skip it by caching the filters of all your models:
php artisan querystring:cache
The command writes the map of every model's filters to bootstrap/cache/querystring.php. To remove it:
php artisan querystring:clear
Laravel's optimize command
The cache is part of Laravel's optimize command, so if your deployment already runs it there is nothing to add:
php artisan optimize # also runs querystring:cache
php artisan optimize:clear # also runs querystring:clear
Regenerating the cache
While the cache exists, the package doesn't read the attributes anymore: it uses the cached map. Regenerate it whenever you add, rename or remove a filter, or add the trait to a new model. Otherwise the change has no effect.
In local development you usually don't need the cache at all. If you run optimize locally, remember to clear it before working on filters.
If generating the cache fails, for example because a model throws an exception in its constructor, the previous cache is kept.
Which models are cached
The command looks for models under your application's app directory. A model is included when it:
- extends Eloquent's
Model; - can be instantiated, so abstract models are skipped;
- uses the
UseQueryStringtrait, directly, through a parent class or through another trait.
To cache it, the command creates each model with new and calls its getQueryStringObject() method, so the cached filters are those of the object returned at that moment.
To scan a different directory, pass its namespace and path as declared in the autoload.psr-4 section of composer.json:
php artisan querystring:cache --namespace="Domain\\" --path=src/Domain
Each run replaces the whole cache with the models of the given directory.