I have some repositories for my models and I would like to add a cache decorator to some of them. The only issue is that I have a method paginate() which I don't know how to cache:
public function paginate($perPage = 15, $columns = ['*'])
{
return $this->model->paginate($perPage, $columns);
}
How can I cache it efficiently and make it aware to the current requested page?
@bimalshah72 hey. How does your code is aware to the requested page? If I have /collection?page=3 then obviously I want page number 3 results. I'm not sure the code you just shared is aware of that and I think it will always return the first page.
@bimalshah72 well I'm not sure how would it work. In order to have a lengthAwarePaginator we need to create a paginatior which means we need to query the DB for the collection. The only solution I currently see is to have a Request object in the paginate method and check for a page in the query string and append that to the generated key. Will have to test that theory out :)