Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BartHuis's avatar

Caching and not caching little parts blade

Hi,

We'll start on a new laravel project within a few weeks, and need some thought from you about caching and not caching little parts, what will be the best workaround so we can begin with the best setup from the beginning?

We'll have pages with products each product will be an object with a view. When we make the views, we'd like that to be cached. Alse we'd like some little parts (like delivery moment, can change from one minute to another) not to be cached.

In the curentsystem the product's html is cached with replace key's, and when it's used, the keys are replaced by live data before presented to the user.

What are your thoughts on what will be the best way to do this all in the best setup with laravel / blade if you would write this one from scratch?

Bart

ps: i'm relatively new in laravel, dit some work with symfony before, and now looking at the possibilities of laravel to create our new project with

0 likes
9 replies
BartHuis's avatar

Thnx! :D Description sounds good, don't have a paid account yet, think we'll get one soon. Anyone experience with the russian doll caching? Pros and cons? any other ideas? Lets discuss the possibilities ;)

MikeHopley's avatar

I believe Jeffrey's series on Russian doll caching may have been inspired by this post from DHH, about how Basecamp improved its performance.

1 like
leolam2005's avatar

Basically it's a solution to N+1 problem by finding the model's updated_at timestamp. Update of Child will trigger updated_at of parent, hence the models need re-cached. If there isn't updates, just throw out the HTML of cached parent and its children.

@ foreach($posts as $post) //throw out the rest as cached data as HTML, if post hasn't been updated for the period of time
    @ cache($post)
    {{ $post->title }}

    @ foreach($posts->comments() as $comment) <!-- N+1 -->
        @ cache($comment)
        {{ $comment->body }}
        {{ $comment->user()->first()->name }} <!--Another query here-->
        @ stop
    @ endforeach

    @ stop
@ endforeach

alternatively, you can use eager load queries to load all models at once to avoid N+1.

MikeHopley's avatar

Basically it's a solution to N+1 problem by finding the model's updated_at timestamp.

Your explanation is generally right, but it's worth noting that Russian doll caching offers much better performance than eager loading alone.

Eager loading reduces some database queries. Caching not only removes those database queries altogether, but also skips other steps such as compiling the PHP view into HTML.

By caching large chunks of HTML, you can potentially get your site performance close to flat HTML pages. And by adding Pjax, you can make it even faster than that.

Whether this is worth the effort is a different question.

leolam2005's avatar

@MikeHopley right. caching saves a lot of CPU when parsing to HTML in the server side.

Russian doll caching is very essential for Server-side app.

But when it comes to Client side app, like SPA, VueJs app, and even API call, eager loading is still important to fetch all data at once.

1 like

Please or to participate in this conversation.