在我的Laravel应用程序中,我在多个地方使用了Auth::user()
。我只是担心Laravel可能会对Auth::user()
请咨询
答案 0 :(得分:12)
不缓存用户模型。我们来看看Illuminate\Auth\Guard@user
:
public function user()
{
if ($this->loggedOut) return;
// If we have already retrieved the user for the current request we can just
// return it back immediately. We do not want to pull the user data every
// request into the method because that would tremendously slow an app.
if ( ! is_null($this->user))
{
return $this->user;
}
正如评论所说,在第一次检索用户后,它将存储在$this->user
中,并在第二次通话时返回。
答案 1 :(得分:3)
对于同一个请求,如果您多次运行Warning: fwrite() expects parameter 1 to be resource, integer given in /home/u257807476/public_html/ready.php on line 8
Warning: fclose() expects parameter 1 to be resource, string given in /home/u257807476/public_html/ready.php on line 10
,它将只运行1 Auth::user()
而不是多次。
但是,如果您使用query
拨打另一个请求,它将再次运行1 Auth::user()
。
由于安全性的观点,在第一次请求后,无法为所有请求缓存此内容。
因此,无论您拨打多少时间,每个请求都会运行1个查询。
我在这里看到使用了一些会话以避免运行多个查询,因此您可以尝试以下代码:http://laravel.usercv.com/post/16/using-session-against-authuser-in-laravel-4-and-5-cache-authuser
由于