简体   繁体   中英

Laravel Cache views or controller response

I've a Blog with views ,

header.blade.php - prints dynamic navbar along with categories and login info.

content.blade.php - prints dynamic content of the page.

sidebar.blade.php - prints dynamic related articles. 

footer.blade.php - prints static & some php dynamic content(date). 

What I need is to cache content.blade.php , sidebar.blade.php , footer.blade.php . This will help in reducing load, as far as I can think.

I don't want to cache header.blade.php as it prints current user info and some dynamic js vars.

For controllers, I wanted to cache the function_name(argument_var). Such that, if second time same function with same argument_var is made a call, responses are written back immediately, without communicating with db, like mem cahche, but not using it.

I need a possibility to have controls over cache.

I've looked upon some Cache tutorials . But I'm unhappy to see it going still slow.

I tried to create html files as cache on disk, but that seems to have less controllers and increases ttfb of all first response.

What you're asking to do is quite complex. I have gone through this before and have solved it but I wouldn't recommend the solution, for reasons that I will try to explain later. Firstly, it's worth reading the code in the Laravel View module (vendor/laravel/framework/src/Illuminate/View) to figure out what's going on.

A blade template (filename.blade.php) is compiled into a PHP file which is kept in storage/framework/views with a name like <hash>.php. A large portion of the time taken to render the blade into HTML is actually taken up in compiling these views to PHP.

Once these views are on disk, the PHP engine renders them from disk as PHP code. It's very tricky to get the PHP engine to compile code from memory, and in fact doing so isn't significantly faster than doing so from disk because it requires an eval() call which is slow in PHP, instead of an include() call. Furthermore the opcodes created by the PHP compiler executing the include() call are cached inside the opcode cache (assuming you have one) and the opcodes created by the eval() call are not, meaning that essentially if your blade is in memory the PHP compiler has to be invoked for each eval() call.

So what I would recommend is:

  • Ignore the temptation of compiling .blade into PHP and storing that in RAM, it's not going to work for you.
  • Put a ramdisk (eg /dev/shm) over your storage/framework/views directory to speed up access -- eg link that directory to /dev/shm (which you will have to do on boot up) or put a specific fstab entry in for it.
  • If cached views are really what you need for speed then consider an alternative template engine such as smarty which can compile from RAM and doesn't compile to PHP code.

I hope that helps. It's possible to cache the output of the View::phpEngine compiler (which creates HTML from PHP code) but it's messy and involves some nasty hacks inside Laravel. I have done it once for Laravel 3 but the payout wasn't good, and I wouldn't recommend trying it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM