Home >PHP Framework >Laravel >Let's talk about how to optimize PHP and Laravel to improve the performance of web applications
Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications has a lot of stuff. But fast is not one of them. Let’s learn some optimization tips to make things run faster!
Since the birth of Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications, there is no PHP developer who has not been influenced by her. They are junior or mid-level developers who like the rapid development that Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications provides, or they are senior developers who are forced to learn Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications due to market pressure.
In any case, it is undeniable that Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications has revitalized the PHP ecosystem (I am sure that without Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications, I would have left the PHP world long ago).
Excerpt from review of Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications
However, since Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications goes out of its way to make things easy for you, that means it does a lot of work under the hood , to ensure that you as a developer can have a comfortable programming experience. All of Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications's seemingly "magical" features have layers upon layers of code that need to be started every time a feature is run. Even a simple exception will drill down to the bottom (starting from the error, all the way to the kernel):
For what appears to be a compilation error in a view, there are 18 Function calls need to be traced. I've personally come across 40, probably more if you use other libraries and plugins.
The point is that by default, such layers of nested code make Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications very slow.
How slow is Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications?
To be honest, this question is impossible to answer for several reasons.
First of all, there is currently no recognized, objective and reasonable standard to measure the speed of network applications. Faster or slower compared to what? under what conditions?
Second, a web application depends on many things (database, file system, network, cache, etc.), so talking about speed is silly. A very fast web application is a very slow web application if it has a very slow database.
But this uncertainty is why benchmarking is so popular. Although they make no sense (see here and here), they provide some frame of reference to help us avoid getting angry. Therefore, it's best to take things with a pinch of salt and give us a false, rough idea of the speed between PHP frameworks.
According to this rather respectable GitHub source code, here is a comparison of PHP frameworks.
You probably wouldn't even notice Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications here (even if you squinted really hard) unless you cast your eyes to the very end. Yes, dear friends, Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications comes last! Now, granted, most of these "frameworks" aren't very practical or even useful, but it does tell us how slow Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications is compared to other, more popular frameworks.
Normally, this kind of "slowness" does not appear in applications, because our daily web applications rarely reach high data volumes. But once it reaches (for example, up to 200-500 or more concurrency), the server will start to block and die. At this time, no matter how much hardware you throw away, it won't solve the problem. Infrastructure costs rise rapidly, and your lofty ideals of cloud computing collapse.
But, hey, cheer up! This article is not about what not to do, but what can be done.
The good news is that there are a lot of things you can do to make your Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications application faster. Several times the speed. Yes, no joke. You can make the same code base fast and save hundreds of dollars per month in infrastructure/hosting fees. How to do it? let's start.
Four Types of Optimization
In my opinion, optimization can be done at four different levels (when it comes to PHP applications, that is):
Language level: This means you use a faster version of the language and avoid specific features/coding styles in the language that make your code slower.
Framework level: These are what we will cover in this article.
Infrastructure level: Adjust your PHP process manager, web server, database, etc.
Hardware level: Move to a better, faster, and more powerful hardware hosting provider.
All these types of optimizations have their existence (for example, the optimization of php-fpm is very critical and powerful). But the focus of this article is purely on type 2 optimizations: those related to the framework.
By the way, there is no reason behind these numbers, nor is it an accepted standard. I just made this up. Please never quote me when I say "our servers need type-3 optimization" or your team leader will kill you, find me, and kill me too.
Now, we are finally in the promised land.
n 1 query problem is a common problem when using ORM. Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications has its powerful ORM called Eloquent, which is so beautiful and so convenient that we often forget to see what's going on.
Consider a very common scenario: display all orders placed by a specified customer list. This is very common in e-commerce systems and any list that needs to display all entities related to some entity,
We can imagine having a controller like this:
class OrdersController extends Controller { // ... public function getAllByCustomers(Request $request, array $ids) { $customers = Customer::findMany($ids); $orders = collect(); // new collection foreach ($customers as $customer) { $orders = $orders->merge($customer->orders); } return view('admin.reports.orders', ['orders' => $orders]); } }
Great! What's more, elegant and beautiful. ??
Unfortunately, writing code like this in Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications is a disastrous approach.
The reasons are as follows.
When we use the ORM to find a given customer entity, a SQL query like this is generated:
SELECT * FROM customers WHERE id IN (22, 45, 34, . . .);
This is exactly as expected. As a result, all returned rows are stored in the collection $customers
in the controller function.
Now we loop through each customer one by one and get their orders. This will execute the following query...
SELECT * FROM orders WHERE customer_id = 22;
...as many times as there are customers.
In other words, if we need to get the order data of 1000 customers, the total number of database queries performed will be 1 (to get the data of all customers) 1000 (to get the order data of each customer) )=1001. This is where the name n 1 comes from.
Can we do better? Of course! By using eager loading, we can force the ORM to perform a JOIN and return all the required data in one query! Like this:
$orders = Customer::findMany($ids)->with('orders')->get();
The resulting data structure is a nested structure, of course, but the order data can be extracted easily. In this case, the single query produced looks like this.
SELECT * FROM customers INNER JOIN orders ON customers.id = orders.customer_id WHERE customers.id IN (22, 45, ...);
Of course, one query is better than a thousand queries. Imagine what would happen if there were ten thousand customers to deal with! In other words, if we also want to display the items included in each order, that is simply impossible! Remember, the name of this technique is preloading, and it can come in handy almost any time.
One of the reasons for Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications's flexibility is its large number of configuration files that are part of the framework. Want to change how/where pictures are stored?
Okay, just modify the config/filesystems.php
file (at least as far as this). Want to use multiple queue drivers? Feel free to describe it in config/queue.php
. I just did a tally and found that there are 13 configuration files for different aspects of the framework, so you won't be disappointed no matter what you want to change.
Given the characteristics of PHP, every time a new web request comes in, Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications will wake up, start everything, and parse all config file to find out how to do things differently this time. It would be silly if nothing changed these days! Rebuilding the configuration file for every request is a waste, which can (actually, must) be avoided. The solution is a simple command provided by Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications:
php artisan config:cache
The purpose of this is to All available configuration files are combined into a single file and cached somewhere for quick retrieval. The next time a web request comes in, Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications will simply read this single file and start working.
In other words, configuring the cache is an extremely delicate operation that may blow up in your face. The biggest pitfall is that once you issue this command, in addition to the configuration file, all env()
function calls elsewhere will return null
!
It does make sense if you think about it carefully. If you use the configuration cache, you are telling the framework: "You know what, I feel like I have things set up pretty well, and I'm 100% sure I don't want them to change." In other words, you want the environment to stay static, This is what the .env
file does.
Speaking of which, here are some ironclad, sacred, and unbreakable configuration caching rules:
Only do this on production systems.
Only do this if you are very, very sure you want to freeze the configuration.
In case something goes wrong, use php artisan cache:clear
to undo the settings.
Pray that the loss to the company will not be too great!
In order to help everyone, Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications loads a large number of services when waking up, these Services are found in the config/app.php
file as part of the 'providers'
array key. Let's take a look at my situation:
/* |-------------------------------------------------------------------------- | Autoloaded Service Providers |-------------------------------------------------------------------------- | | The service providers listed here will be automatically loaded on the | request to your application. Feel free to add your own services to | this array to grant expanded functionality to your applications. | */ 'providers' => [ /* * Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications Framework Service Providers... */ Illuminate\Auth\AuthServiceProvider::class, Illuminate\Broadcasting\BroadcastServiceProvider::class, Illuminate\Bus\BusServiceProvider::class, Illuminate\Cache\CacheServiceProvider::class, Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, Illuminate\Cookie\CookieServiceProvider::class, Illuminate\Database\DatabaseServiceProvider::class, Illuminate\Encryption\EncryptionServiceProvider::class, Illuminate\Filesystem\FilesystemServiceProvider::class, Illuminate\Foundation\Providers\FoundationServiceProvider::class, Illuminate\Hashing\HashServiceProvider::class, Illuminate\Mail\MailServiceProvider::class, Illuminate\Notifications\NotificationServiceProvider::class, Illuminate\Pagination\PaginationServiceProvider::class, Illuminate\Pipeline\PipelineServiceProvider::class, Illuminate\Queue\QueueServiceProvider::class, Illuminate\Redis\RedisServiceProvider::class, Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, Illuminate\Session\SessionServiceProvider::class, Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, /* * Package Service Providers... */ /* * Application Service Providers... */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, ],
我再一次数了数,一共列出了 27 项服务! 现在,你可能需要所有的服务,但不太可能。
例如,我现在正好在构建一个 REST API,这意味着我不需要 Session Service Provider、View Service Provider 等。而且由于我是按照自己的方式来做一些事情,而不是按照框架的默认值来做,所以我也可以禁用 Auth Service Provider、Pagination Service Provider、Translation Service Provider 等。总而言之,对于我的用例来说,这些几乎有一半是不必要的。
仔细审视一下你的应用吧。它是否需要所有这些服务提供者?但是看在上帝的份上,请不要盲目地注释掉这些服务,然后推送到生产中去! 运行所有的测试,在开发机和暂存机上手动检查,并且在扣动扳机之前要非常非常偏执。
当你需要对传入的 Web 请求进行一些自定义处理时,创建一个新的中间件就是答案。现在,打开 app/Http/Kernel.php
并将中间件粘在 web
或 api
堆栈中是很有诱惑力的;这样一来,它就会在整个应用程序中变得可用,而且如果它没有做一些侵入性的事情(例如,像日志或通知)。
然而,随着应用程序的增长,如果所有(或大多数)这些全局中间件都存在于每个请求中,那么这个全局中间件的集合可能会成为应用程序的一个无声负担,即使没有业务原因。
换句话说,要小心你在哪里添加/应用新的中间件。在全局范围内添加一些东西可能会更方便,但从长远来看,性能惩罚是非常高的。我知道如果每次有新的变化都要有选择地应用中间件,你要承受的痛苦,但这是我心甘情愿承受的痛苦,也是我所推荐的!
虽然 Eloquent 让 DB 交互的很多方面变得愉悦,但它是以速度为代价的。作为一个映射器,ORM 不仅要从数据库中获取记录,还要实例化模型对象,并用列数据对其进行填充。
所以,如果你做一个简单的 $users = User::all()
,比如有10000个用户,框架会从数据库中获取 10000 行记录,并在内部做 10000 个 new User()
,并用相关数据填充他们的属性。这是大量的工作在幕后进行,如果数据库是你的应用成为瓶颈的地方,绕过 ORM 有时是个好主意。
这对于复杂的 SQL 查询来说尤其如此,在这种情况下,你必须跳很多的圈子,写一个又一个的闭包,但最终还是能得到一个高效的查询。在这种情况下,最好做一个 DB::raw()
,然后手工写查询。
根据 这个 的性能研究, 即使是简单的插入, Eloquent 也会随着记录数量的增加而变慢:
Web 应用优化中最保守的秘密之一就是缓存。
对于新手来说,缓存的意思是预先计算和存储昂贵的结果(昂贵的 CPU 和内存使用量),并在重复相同的查询时简单地返回。
例如,在一个电商商店里,可能会遇到,在 200 万种产品中,大多数时候人们都会对那些新鲜出炉的、在一定价格范围内的、针对特定年龄段的产品感兴趣。在数据库中查询这些信息是很浪费的——因为查询的内容不会经常变化,所以最好把这些结果存储在我们可以快速访问的地方。
Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications 内置支持多种类型的缓存。除了使用缓存驱动和从底层构建缓存系统外,你可能还想使用一些Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications 包,方便模型缓存、查询缓存等。
但是请注意, 在一定的简化用例之外, 预制的缓存包可能会带来更多的问题, 而不是解决这些问题.
当你在 Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications 中缓存一些东西时, 你有几个选项可以选择将需要缓存的计算结果存储在哪里。这些选项也被称为 缓存驱动。所以, 虽然使用文件系统来存储缓存结果是可能的,也是完全合理的,但这并不是缓存的真正目的。
理想情况下,你希望使用内存中(完全活在 RAM 中)的缓存,比如 Redis、Memcached、MongoDB 等,这样在较高的负载下,缓存就能起到至关重要的作用,而不是自己成为瓶颈。
现在,你可能会认为拥有 SSD 磁盘和使用 RAM 棒几乎是一样的,但还差得远。即使是非正式的 基准测试也显示,在速度方面,RAM优于SSD的10-20倍。
在缓存方面,我最喜欢的系统是 Redis。它的速度 快得离谱(每秒 10 万次读取操作是很常见的),对于非常大的缓存系统,可以很容易地演变成一个 集群。
就像应用程序的配置一样,路由不会随着时间的推移而改变,是缓存的理想选择。如果你像我一样无法忍受大文件,并且最终把你的 web.php
和 api.php
分割成几个文件的话,这一点尤其适用。 一个简单的Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications命令就可以把所有可用的路由打包并保存起来, 方便以后的访问:
php artisan route:cache
而当你最终要增加或改变路由时,只需这样做即可。
php artisan route:clear
图片是大多数网络应用的核心和灵魂。巧合的是,它们也是最大的带宽消耗者,也是导致应用程序/网站速度慢的最大原因之一。如果你只是简单地将上传的图片天真地存储在服务器上,然后以 HTTP 响应的方式发送回来,你就会让一个巨大的优化机会溜走。
我的第一个建议是不要在本地存储图片——有数据丢失的问题要处理,而且取决于你的客户在哪个地理区域,数据传输可能会非常缓慢。
相反,选择像 Cloudinary 这样的解决方案,它可以自动动态调整和优化图像的大小。
如果这不可能,使用类似 Cloudflare 的东西来缓存和服务图像,同时它们存储在你的服务器上。
如果连这一点都做不到,调整一下你的网络服务器软件,压缩资产并引导访问者的浏览器去缓存东西,就会有很大的不同。下面是一个 Nginx 配置的片段。
server { # file truncated # gzip compression settings gzip on; gzip_comp_level 5; gzip_min_length 256; gzip_proxied any; gzip_vary on; # browser cache control location ~* \.(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ { expires 1d; access_log off; add_header Pragma public; add_header Cache-Control "public, max-age=86400"; } }
我知道图片优化与 Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications 无关, 但这是一个如此简单而强大的技巧 (而且经常被忽视), 所以我忍不住了。
自动加载是 PHP 中一个整洁的、并不古老的功能,它可以说是拯救了这门语言的末日。尽管如此,通过破译给定的命名空间字符串来寻找和加载相关类的过程是需要时间的,在生产部署中,如果需要高性能,可以避免这个过程。 再一次,Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications 有一个单一命令的解决方案来解决这个问题:
composer install --optimize-autoloader --no-dev
队列 是指当有很多事情时,你如何处理这些事情,而且每件事情都需要几毫秒才能完成。一个很好的例子是发送电子邮件——在网络应用中,一个广泛的用例是当用户执行一些操作时,发出几封通知邮件。
例如,在一个新推出的产品中,你可能希望每当有人下单超过一定值时,公司领导层(大约6-7个电子邮件地址)就会收到通知。假设你的邮件网关能在500ms内响应你的SMTP请求,那么在订单确认启动之前,用户需要等待3-4秒。一个非常糟糕的用户体验,我相信你会同意。
补救的办法是在任务进来的时候就把它们存储起来,告诉用户一切都很顺利,然后再处理它们(几秒钟)。如果出现错误,在宣布失败之前,排队的任务可以重试几次。
虽然队列系统使设置复杂化了一些 (并增加了一些监控开销), 但它在现代Web应用中是不可缺少的。
对于你的 Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications 应用中的任何前端资源,请确保有一个管道可以编译和最小化所有的资源文件。 那些对 Webpack,Gulp,Parcel 等打包器系统很熟悉的人不需要费心,但如果你还没有这样做,Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications Mix是一个可靠的推荐。
Mix 是一个轻量级的 (老实说,很讨人喜欢!) 围绕Webpack的打包器,它可以处理你所有的 CSS,SASS,JS 等文件。 一个典型的 .mix.js
文件可以像这样小,但仍然可以发挥出巨大的作用。
const mix = require('laravel-mix').mix.js('resources/js/app.js', 'public/js'); mix.js('resources/js/app.js', 'public/js') .sass('resources/sass/app.scss', 'public/css');
当您准备部署生产环境并运行 npm run production
时,它将自动处理导入,最小化,优化以及整个工作流程。 Mix 不仅关心传统的 JS和 CSS 文件,而且还关心您在应用程序工作流程中可能使用的 Vue 和 React 组件。
更多信息参考 这里!
结论
Performance optimization is more art than science - knowing how to do it and how much to do is more important than what to do. That said, there is no limit to what and how much you can optimize in a Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Lets talk about how to optimize PHP and Laravel to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications to improve the performance of web applications application.
But whatever you do, I want to leave you with some parting advice - Optimization should be done for a good reason, not because it sounds good or because you are interested in more than 100,000 The user is paranoid about the performance of his application when there are actually only 10 users.
If you are not sure whether you need to optimize your application, then you should not poke this hornet's nest. An app that just works, feels boring at times, but does what it has to do is ten times preferable to an app that's optimized into a mutant hybrid super machine that fails from time to time.
English original address: https://geekflare.com/laravel-optimization/
Translation address: https://learnku.com/laravel/t/47213
[Related recommendations: laravel video tutorial]
The above is the detailed content of Let's talk about how to optimize PHP and Laravel to improve the performance of web applications. For more information, please follow other related articles on the PHP Chinese website!