問題描述
我正在 Laravel 上使用 Redis 邁出我的第一步,我發現了一些奇怪的東西.
I′m making my first steps with Redis on Laravel and there is something odd I figured out.
在我的設置中使用 Redis 作為緩存驅動程序時,加載頁面需要很長時間.
When using Redis as a cache driver in my setup it is taking far way to much time to load a page.
我怎么知道?當不使用 Cache 門面而直接使用 Redis 門面時,響應時間只是一小部分.我從頭開始安裝 Laravel 并為簡單的文章模型構建遷移和播種器.
How do I know? When not using the Cache facade but the Redis facade directly response times are just a fraction. I set up a laravel installation on scratch and build a migration and seeder for a simple Article model.
首先我認為這些項目沒有存儲在 redis 中,因為在使用 KEYS * 搜索時 redis-cli 沒有顯示它們.我發現緩存存儲在另一個帶有 REDIS_CACHE_DB
的數據庫中,如 config/database.php`redis-cli 中的 INFO 鍵空間列出了這兩個名為 0 和 1 的數據庫.
First I thought the items were not stored in redis as redis-cli didn′t show them when searching with KEYS *. I figured out the cache is stored in another DB with REDIS_CACHE_DB
as found in config/database.php
`INFO keyspace in redis-cli lists those two DB′s named 0 and 1.
我認為問題可能是由我使用 Mamp Pro 設置的本地主機引起的.所以我切換到 Laravel Homestead box 并將我的項目上傳到那里.同樣在這里.
I thought the problem could be caused by my localhost setup with Mamp Pro. So I switched over to the Laravel Homestead box and uploaded my project there. Same here.
這是我正在使用的代碼:路由/web.php
Here′s the code I′m using: routes/web.php
use IlluminateSupportFacadesRedis;
use IlluminateSupportFacadesCache;
use IlluminateHttpRequest;
use AppArticle;
Route::get('/get-articles-mysql', function (Request $request) {
return response()->json(Article::take(20000)->get());
});
Route::get('/get-articles-cache', function (Request $request) {
return Cache::remember('posts', 60, function () {
return Article::take(20000)->get();
});
});
Route::get('/get-articles-redis', function (Request $request) {
if($posts = Redis::get('posts.all')) {
return response()->json(json_decode($posts));
}
$posts = Article::take(20000)->get();
Redis::set('posts.all', Article::take(20000)->get());
return response()->json($posts);
});
我正在使用郵遞員來獲取響應時間.我進行了幾次運行,因為當緩存為空時,第一個請求的緩存路由應該很慢.但我得到的平均結果是:
I′m using postman to get the response times. I made several runs as the caching routes should be slow on the first request when caching is empty. But what I get on the average is this:
http://laravel-echo.local/get-articles-mysql 583ms
http://laravel-echo.local/get-articles-redis 62ms
http://laravel-echo.local/get-articles-cache 730ms
我不明白這個.直接使用 Redis 門面非常快.但為什么緩存這么慢? 是的,我仔細檢查了我的 .env 文件.有 CACHE_DRIVER=redis 所以我不是偶然使用文件系統的.我同時使用了 php artisan config:clear
和 php artisan cache:clear
以避免調試時出錯.
I′m not getting this. Using the Redis facade directly is super-fast. But why is caching so slow? Yes, I double checked my .env files. There is CACHE_DRIVER=redis so I′m not using file system by accident. And I used both php artisan config:clear
and php artisan cache:clear
to avoid mistakes when debugging.
我在 redis-cli 中看到一個名為laravel_cache:posts"的鍵.緩存的帖子在那里.加載它們只需要很長時間.我還在 Chrome 中測試了請求.響應時間要長得多,但緩存仍然比單純的 mysql 查詢需要更多的時間.
I see a key called "laravel_cache:posts" in redis-cli. The cached posts are there. It only takes ages to load them. I also tested the requests in Chrome. The response times are much longer but still caching takes more than mere mysql querying.
那么有什么建議可能會發生在這里嗎?
So any suggestions what could be going on here?
推薦答案
我知道這個帖子已經很老了,但我還是一樣.
I know this thread is already very old, but I am still getting the same.
我使用 Laragon 進行本地開發,Redis 使我的 API 請求速度降低了 4 倍.
I am using Laragon for local development and Redis makes my API request 4x slower.
OMFG...我只是問題所在.
OMFG... I just the problem.
在我的 .env 文件中,我有REDIS_HOST=localhost",這正是問題所在.
In my .env file I had "REDIS_HOST=localhost" and that is exactly the problem.
我把它改成REDIS_HOST=127.0.0.1"后,一切都運行得很快.
After I change it to "REDIS_HOST=127.0.0.1", everything is running fast.
試試看告訴我.
這篇關于Laravel 緩存與 Redis 非常慢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!