Laravel使用Redis缓存非常慢 [英] Laravel Caching with Redis is very slow

查看:506
本文介绍了Laravel使用Redis缓存非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Redis一起在Laravel上迈出第一步,我发现有些奇怪之处。

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安装程序,并为一个简单的Article模型构建了移植和种子安装程序。

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-cli那样存储在redis中。使用KEYS *搜索时不显示它们。我发现缓存已存储在另一个数据库中,该数据库的 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框,并将项目上传到那里。同样在这里。

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 Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
use App\Article;

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缓存非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆