Note: Now that I know where the issue comes from, I modified the question. It now contains only the information needed.

我不熟悉Laravel PHP框架.

I have a very small app working on my computer. It is connected to a MySQL database and has a User model. I use the Auth class to log in and out.

一切都很正常,但是当我登录时,加载一个页面需要大约一秒钟,这是非常慢的.当我没有登录时,只有几毫秒的时间.

By using the built-in profiler, I realized two problems. First, like I said, loading a page takes a bit more than 1000 milliseconds. Second, the framework makes one SQL every time I load a page when I'm logged in. The query searches a user with a certain id (my id). I guess it is there to get information about the logged in user. But isn't there supposed to be some sort of cache. Will this be a problem if my website will have to manage many requests per seconds.

I realized that using Auth::check() in the view is what is causing the issue. I have around 4 Auth::check() is my Blade view. When I have none, it goes fast. If I have one, it is slow. Then, no matter how many I have, it doesn't get much slower. It's like if the Auth class' initialization takes too much time or something like that. I guess it explains why it only happens when I'm logged in.

I dived into Laravel's code and I found out that when Auth::check() is called for the first time, the Auth class needs to "activate" my Session by retrieving the user's info from the database. That explains the query being executed every page request. But since the profiler says that the query doesn't even take a millisecond to execute, I still don't know why it slows down the app.

New information: Even when I'm not sending a query to the database, the simple act of connecting to it takes almost a second. This is the reason it is slow. I think I'm getting really close to solve the issue.

Any idea so far?

Thanks in advance.

Notes

  • The fact that Auth::check() is in the view doesn't change anything.
  • 使用另一种方法,比如Auth::guest(),并不能解决这个问题.
  • New: Connecting to the database is what is slow.

推荐答案

I finally found a way to fix this.

在很多论坛上读到一些关于XAMPP、MySQL和PHP的帖子时,我在某个地方读到,最好使用127.0.0.1,因为locahost需要额外的DNS查找.

In the database configuration file, I simply changed locahost for 127.0.0.1.

Now everything is fast.

I find this really strange. Using locahost in the configuration file used to make the database connection take more than a second!

Laravel相关问答推荐

如何在 Laravel 9 中获取带分页的订单列表?

如何在数据来自Rest API的flutter中创建下拉类别名称列表?

自从 Laravel 使用 Vite 更新后,我无法运行npm run dev

使用 App::environment() 与 app()->environment() 与 config('app.env') 之间的区别

RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException

Laravel:Blade foreach 循环 bootstrap 列

路由中间的可选参数

Laravel 4,如何测试复选框是否被选中?

Laravel - 如何恢复本地存储符号链接并刷新

Laravel:如何在没有数据库的情况下对用户进行身份验证

Laravel 5 - 没有重叠的任务计划不起作用

如何在没有 Eloquent 的情况下创建 Laravel 模型?

如何在包中安排 Artisan 命令?

在 Eloquent 中按自定义顺序对集合进行排序

Heroku CLI 安装错误:PATH 未更新,原始长度 1585 > 1024

数组的旧输入?

Laravel Eloquent - 加入表时防止覆盖值

Laravel:方法[显示]不存在

控制器中的artisan调用输出?

如果参数不是整数,如何以不同方式定义路由