Reputation: 253
I want cache my query results and I read about Cache::remember in laravel but it take a time parameter and I don't want to set time for my redis cache.
I need something to cache my queries and after queries have updated the results changed by the updating.
what's your recommendation?
Upvotes: 0
Views: 421
Reputation: 686
There is a very good library for this but they warn that it’s only compatible with laravel 5.8. If you could update, this is a way to go. If updating laravel is not an option, at least you can read the code and try to follow the same direction they did.
https://github.com/GeneaLabs/laravel-model-caching
This library does exactly what you need. You can have your models and/or custom queries cached and you can invalidate this cache whenever a model gets updated, created or deleted.
Upvotes: 0
Reputation: 795
Storing the full collection of eloquent models in redis can be slower than expected.
In my case, i had to create nested selection with lot of where
, count
, join
, group by
and order by
... etc.
It has consumed a lot of resources at every request, so i tried to cache the result. It was not the best solution, because it was (4 times) slower than i wanted (200+ ms response).
The solution is SELECT id FROM ...
from "huge" query and store IDs in redis. After this the SQL query looks like SELECT * FROM <table> WHERE id IN (...);
in every request. (Re-order the data in sql query if necessary)
In this way, the required data from redis and sql can be queried quickly. The average response time is less than 50 ms.
I hope this will help.
Upvotes: 1