简体   繁体   English

Laravel缓存视图或控制器响应

[英]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 . 我需要的是缓存content.blade.phpsidebar.blade.phpfooter.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. 我不想缓存header.blade.php,因为它会打印当前用户信息和一些动态js var。

For controllers, I wanted to cache the function_name(argument_var). 对于控制器,我想缓存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. 这样,如果第二次调用具有相同arguments_var的相同函数,则会立即写回响应,而无需与db通信(如mem cahche),但不会使用它。

I need a possibility to have controls over cache. 我需要对缓存进行控制。

I've looked upon some Cache tutorials . 我看过一些Cache教程 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. 我试图创建html文件作为磁盘上的缓存,但是这似乎减少了控制器,并增加了所有第一个响应的ttfb

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. 首先,值得阅读Laravel View模块(vendor / laravel / framework / src / Illuminate / View)中的代码以了解发生了什么。

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. 刀片模板(filename.blade.php)被编译成一个PHP文件,该文件以类似<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. 实际上,将刀片呈现为HTML所花费的大部分时间是将这些视图编译为PHP。

Once these views are on disk, the PHP engine renders them from disk as PHP code. 一旦这些视图在磁盘上,PHP引擎就会将它们从磁盘呈现为PHP代码。 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. 让PHP引擎从内存中编译代码是非常棘手的,实际上这样做并不比从磁盘中编译代码快得多,因为它需要在PHP中较慢的eval()调用,而不是include()调用。 。 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. 此外,由PHP编译器执行include()调用创建的操作码被缓存在操作码缓存中(假设您有一个),而由eval()调用创建的操作码则没有,这意味着从本质上讲,如果刀片位于内存中,则PHP必须为每个eval()调用调用编译器。

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. 忽略了将.blade编译为PHP并将其存储在RAM中的诱惑,这对您不起作用。
  • 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. 将ramdisk(例如/ dev / shm)放在您的storage / framework / views目录上以加快访问速度-例如,将该目录链接到/ dev / shm(在启动时必须执行此操作)或放置一个特定的fstab条目为它。
  • 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. 如果确实需要高速缓存的视图,那么可以考虑使用替代模板引擎,例如smarty,它可以从RAM编译,而不能编译为PHP代码。

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. 可以缓存View :: phpEngine编译器的输出(可从PHP代码创建HTML),但是它很杂乱,并且在Laravel中涉及一些讨厌的黑客。 I have done it once for Laravel 3 but the payout wasn't good, and I wouldn't recommend trying it again. 我曾经为Laravel 3做过一次,但是支出并不高,我不建议再次尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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