bhosted's avatar

View caching with different expire periods

Hi everyone,

I'm creating a new e-commerce website. A lot of views contain product prices that don't change that often, but there is off course also a shopping cart on the page.

The product prices are stable and don't change for months or maybe even years. The shopping cart can change on each request.

What is the preferred way to precompile / cache the view in such a way not all the prices are retrieved from the database on each request? I prefer to recompile / cache the views at night, so when the website is hit by more customers during the day, the views only have to add the shopping cart. Or do I only need to cache the products / prices and compile the views with these cached prices?

Thanks! Marc

0 likes
7 replies
bhosted's avatar
class ArticlesController extends Controller {
    public function index() {
        if ( Cache::has('articles_index') ) {
            return Cache::get('articles_index');
        } else {
            $news = News::all();
            $cachedData = view('articles.index')->with('articles', $news)->render();
            Cache::put('articles_index', $cachedData);                                         
            return $cachedData;           
        }  
    }
}

I find the code above very interesting, but how can I exclude the shopping cart part in the page from caching? And include /compile the shopping cart each time the page is requested?

tykus's avatar

@bhosted if you are returning the full server-rendered view, then the cart will be cached also. However, you could use Javascript to dynamically fetch the cart whenever the page is rendered in the browser. You could consider a Vue (or similar) component to handle this behaviour; although vanilla JS will also work.

bhosted's avatar

@tykus, after reading your answer I realized that most prices and other stuff that almost never changes are in the "slot" part of the view. I could cache the slot part of the view and generate all the other parts (including the cart) when the page is requested. Maybe that's the easiest way, using some kind of view component that does the caching of the slot part of the requested blade. The cart is part of the lay-out blade.

EDIT:

Maybe this is the solution I'm looking for: https://github.com/spatie/laravel-partialcache

martinbean's avatar

@bhosted View caching doesn’t cache the content of pages; it just pre-compiles the Blade templates so the Blade compiler doesn’t need to run on every page load (which can result in speed increases if you use a lot of partials or components). The templates will render whatever you return from your controllers; even if views are cached.

bhosted's avatar

@martinbean Thanks for your answer. What I want is cache the rendered template that contains all the prices and other information that almost never changes. As mentioned in my reply to @tykus: I'm going to look into the option to cache the rendered slot parts using some kind of view component.

martinbean's avatar

@bhosted You’re thinking at the wrong level. If the pricing data doesn’t change then cache that data; not where it gets rendered.

Cache the price look-ups by wrapping them in a repository of something so unnecessary database queries aren’t issued, and use that in conjunction with view caching. This way, disk is read quickly from cache, and also rendered in pre-compiled templates.

You then get the advantage of, if you want to offer an API then cached price look-ups will be available there as well. If you just start writing custom Blade caching logic, then your API won’t be able to make use of it.

Please or to participate in this conversation.