Laravel: How to Use Cache With Pagination?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i'm answering another question on youtube comments would you make a video about pagination with cash in laravel and i've seen that question popping up from time to time a few times already so enough for me to make a video about that to be honest i haven't used cash with pagination specifically but i know the problem and i will try to show it in this video and i will try to suggest my own way of solving that but that's not the only way so please in the comments be active in suggesting your own solutions so i guess this video will be not that helpful as much from my side but together with you in the comments we will help anyone who encounters this problem and in general i would raise a question why would you use cash with pagination but step by step so a demo project of list of users very simple demo with debug bar installed at the bottom so you would see how many queries or are there any queries under the hood so we refresh the page we get the users for the first page with two queries as it should be with pagination we go to the second page another set of users another two queries good and in the code we have simply paginate the users with default pagination and getting those users to the blade file now what if you want to introduce the cache here so cache the page 1 page 2 and page 3 respectively it's easy to add a cache if you cache all the records for example result of such query you just add cache remember duration and the result of eloquent query is your cache data but what about pagination how would it work if we just add cash remember here and i have tried to do that so cash remember key let it be users then let's save it for an hour so 60 seconds times 60 minutes and we use cash here on top what would be the result so let's clear the cash just in case and go to page one and we will see two queries but if we refresh the page we see zero queries which means it goes from cache but the problem arises if you click page 2. then although page 2 is here it's taking the data from the cache and shows the first page let's try to click page 3 and see what happens page three the data still doesn't change so adding the cache to paginated query is not that easy we cannot just assign the result to users because users then refers to the first page all the time the solution or one of the potential solutions may be to save the data for each of the pages separately so the key would be users page and for example request page or default if it doesn't exist page one so now we have different keys for different pages and let's see how it works again let's clear the cache and let's go to the first page so click users here it gets the data from the database as you can see and if we refresh it doesn't get it from the database that's good and if we go to page two now it shows page two from the database again caches page two let's try page 5 for example i think it shows page 5 correctly and if we now go to page 2 it should come from the cache yep it does so now in the cache you have separate pages each with their own key that is great but now the problem becomes how and when to clear that cache generally you would clear the cache whenever the records are changed so for example with model observer in eloquent or some events or something like that but we have a lot of cash records now the solution to that is in the observer file where you observe for the changes one of the potential solutions again add your own opinion in the comments but in the observer you should clear the cache on every created updated and deleted and that clear cache could be a private function inside of that observer which deletes the cache for as many pages as you want for example it could be 100 or whatever is the number i don't think anyone would navigate manually to bigger page than 100 but who knows so now if we launch for example artisan tinker and change some data for example we do user first and then update that user i have that in the history update the name for example it returns true and now the first page or second page or any page should come not from the cache but have the query here refresh and we do have those queries because they take the real data the updated data and not from the cache let's see page one also with the query but if we refresh it now it comes from the cache because nothing was changed in the meantime and we see the name john here appearing on the top so this is my kind of way i know it's not ideal i know it's not pretty to clear the cache like that what do you think what would you suggest and also generally i'm not a fan of using cache with paginated data or with any data that is dynamic and potentially has a lot of different cache values cache is great for pretty static amount of data for example some report that could be cash for a day or for an hour some number that could be also cashed for some time because the possibility is pretty low that it would change in an hour or so but to cash paginated data and to cash every page it's a pretty risky strategy if you ask me because you're risking to lose some data in the cache show the cached data instead of real data or vice versa or maybe even lose the track of what is cached and what isn't again what do you think share in the comments below and let's discuss that's it for now and see you guys in other videos
Info
Channel: Laravel Daily
Views: 19,516
Rating: undefined out of 5
Keywords:
Id: Ga7-a826i5c
Channel Id: undefined
Length: 5min 58sec (358 seconds)
Published: Sun Feb 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.