为什么Redis缓存驱动程序不能在Laravel中为我工作?

时间:2019-04-09 02:52:52

标签: php laravel caching redis

具体地说,Cache::put似乎不适合我。我已经在.env文件中设置了以下项目,并且已经安装了predis / predis库。

# redis + cache
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_CACHE_DB=1

我未修改配置文件(它们使用的是上面的值。)

然后在我的php代码中,我有:

if (!empty($request->header('Authorization'))) {
        $token = str_replace('Bearer ', '', $request->header('Authorization'));
        // first check the cache for the user id
        Log::debug("attempting to get user id for token {$token} from cache");

        $response = json_decode(Cache::get($token, function() use ($request, $token) {
            Log::debug("token not found in cache, fetching");
            $auth_server = config('auth.oauth.auth_server');
            try {
                $response = $this->http->get("{$auth_server}/api/verify", [
                    'headers' => [
                        'Authorization' => $request->header('Authorization'),
                        'Accept'        => 'Application/json'
                    ] 
                ]);
            }
            catch (ClientException $e) {
                $response = $e->getResponse();

                $json_response = json_decode((string)$response->getBody());

                $json_response->code = $response->getStatusCode();

                return $json_response;
            }

            $response_body = $response->getBody()->getContents();
            $json = json_decode($response_body);

            if ($json->success) {
                Log::debug("adding cache with key $token and value $response_body");
                Cache::put($token, $response_body);
                return $response_body;
            }

            return null;
        }));
}

我已经在命令行中发出了一个redis-cli monitor命令,尽管我可以肯定地看到它试图从缓存中获取,但是我从没见过将东西放入缓存的调用。如果有什么不同,我目前正在使用Laravel 5.7。

0 个答案:

没有答案