dqvzfp6468 2016-04-30 01:19
浏览 52
已采纳

如何缓存当前经过身份验证的用户? (Laravel 5)

Description

In my situation, I don't have a users table locally, but I have an api that will provide me a list of users.


getUsers()

I modify my getUsers() for my Auth::user() in 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'];
}

Issue

Each time, I used Auth::user() in my code. It makes a call to my API .../vse/accounts It effects a lot of latency in my application.


Try#1

Session

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;
    }

}

Result

It takes 2 seconds longer. :(


Try#2

Cache

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;
    });
}

How do I solve this ?

Should I start using cache ? If so, how do I modify what I have to do something like that ?

Should I store it in the session ?

I'm open to any suggestions right now.

Any hints / suggestions on this will be much appreciated !

  • 写回答

1条回答 默认 最新

  • doudi1978 2016-04-30 01:53
    关注

    You can do this

    protected function getUsers() {
        $minutes = 60;
        $user = 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);
            return $response['data'];
        });
             return $user;
    }
    

    this should work

    Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist -laravel docs

    you will get the user from the cache or, if he don't exist, retrieve user from the api and add him to the cache

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化