Laravel: Cache Flexible
When we talk about cache in Laravel and in general, the first thing that comes to mind is ``` Cache::remember ``` which will provide caching for the data you need and will provide the same UX for all users when they try to access the your data, although that is not always the best option, as I understand it myself...
For my project autosearch.bg when I started working on in I was very exited to develop it and somehow I did not think about the future and how the project could grow and to develop some features in a way it will accommodate them, well I didn't and it comeback to haunt me...what I'm talking about is the number of records which currently are +560 000 which is great I've never thought it will grow some much only in a few months since I've released in production, so to the point: Cache since there a few filters on the page and therefore queries to the database I had to some adjustments because the loading time grew to 20+ seconds which started to cause some issues with CloudFlare and some 504 error pages, so what it did was:
return Cache::flexible('car-listings.filter-options.transmissions', [300, 600], function (): array {
return CarListing::query()
->whereNotNull('transmission')
->where('transmission', '!=', '')
->distinct()
->pluck('transmission')
->sort()
->values()
->toArray();
});I've used Cache::flexible to cache the date for the transmissions for the records that can be queried, what this method gives you is an implementation of the "stale-while-revalidate" method it simply accepts an array of two values: the first when how the value is considered "fresh" and the second one, after how long the value is considered "stale" and how it can be used before a recalculation which for me looks like: 300 seconds fresh, after 600 seconds is going to be stale which works like charm(in combination of a few indexes here and there) and the change in the speed was quite visible, I did this for all filters related queries.
So Cache::remember() could replace Cache::flexible() for the filter options, but flexible() is better for this workload because it avoids expiration spikes while retaining bounded freshness and avoiding giving the users "weird" waiting for records which was my goal here.
Big thanks to the Laravel Cloud Support team for lightning fast answers when it came to those sporadic 504 error page that was shown for a lot of users(me included)
In the end I was able to make those changes and adding indexes when it was appropriate and I was able to go down from ~23.5

to 5 which is huge
Conclusion
I'm glad I was able to do this and I love it keeping up the work to this project and to make better and better.
Share this post
