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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler