Laravel Caching with Redis is very slow(Laravel 缓存与 Redis 非常慢)
问题描述
我正在 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 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 缓存与 Redis 非常慢
基础教程推荐
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01