描述
在我的情况,我没有在本地users
表,但我有一个API将提供我一个用户列表。如何缓存当前已通过身份验证的用户? (Laravel 5)
getUsers()
修改我getUsers()
我Auth::user()
在app/Auth/ApiUserProvider.php
protected function getUsers()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, env('API_HOST') . 'vse/accounts');
$response = curl_exec($ch);
$response = json_decode($response, true);
curl_close($ch);
return $response['data'];
}
问题
每一次,我在我的代码使用Auth::user()
。它打电话给我的API .../vse/accounts
它在我的应用程序中造成了很多延迟。
尝试#1
会话
protected function getUsers()
{
if(Session::has('user')){
return Session::get('user');
}else{
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, env('API_HOST') . 'vse/accounts');
$response = curl_exec($ch);
$response = json_decode($response, true);
curl_close($ch);
$user = $response['data'];
Session::put('user',$user);
return $user;
}
}
结果
它需要2秒。 :(
尝试#2
缓存
protected function getUsers()
{
$minutes = 60;
$value = Cache::remember('user', $minutes, function() {
//your api stuff
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, env('API_HOST') . 'vse/accounts');
$response = curl_exec($ch);
$response = json_decode($response, true);
curl_close($ch);
$user = $response['data'];
return $user;
});
}
我该如何解决这个问题?
我应该开始使用缓存?如果是这样,怎么办我修改了我必须做的事情吗?
我应该将它存储在会话中吗?
我接受任何建议,现在。
任何提示/建议,将非常感谢!
试试这个:http:// laravel。io/forum/11-04-2014-laravel-5-how-do-i-create-a-custom-auth-in-laravel-5 –
我已经做了自定义认证,我试图缓存我的'认证: :user()'对象,所以我不必一直打我的API。 – ihue