laracasts / matryoshka

Russian Doll Caching in Laravel

Home Page:https://laracasts.com/series/russian-doll-caching-in-laravel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Caching paginated results

mgarciadelojo opened this issue · comments

Hi Jeffrey,

First of all, thank you very much for this package. I am a big fan of laracasts :)

I was trying to use the russian doll caching in a view with pagination, so to make it work I have to have this in my controller...

$collection = Collection::make($results->items);

... and then pass it to the view. This way I can cache the results, but I am speding twice the memory. By the way, to allow @cache directive to cache collections I needed to run:

composer require laracasts/matryoshka:dev-master

Do you come up any idea how can I solve this?

Thanks in advance,

commented

am in the same boat so i came up with a little somthing

@inject('Model','App\Post')

@foreach ($posts as $post)
    @cache("Posts/$post->id-".$Model->cacheDate($post->updated_at))
        ...
    @endcache
@endforeach

cacheDate is basically turning the value into timestamp, and we had to inject the model as the pagination returns an array so calling the method directly on $post wont work.

public function cacheDate($value) {
     return Carbon::parse($value)->timestamp;
}

and now everything works as usual without changing anything in the original code 😉

Hi @ctf0,

Thank you for your reply. This solution may work, but, instead of caching all the page (as I wanted), you are caching those items one by one.

I finally ended up using redis to cache the collection and passing it to the view. It was a lot faster than using this package.

Cheers,

commented

the problem with caching the whole page is when a fragment of that page is updated, u will have to regenerate the whole thing which is the exact opposite of what this package for.

EDIT:
just a headsup, this type of caching is a nightmare for multilang, u r better off without it.