В частности, 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, если это имеет какое-либо значение ..