<bdo id='w2e2F'></bdo><ul id='w2e2F'></ul>
  1. <i id='w2e2F'><tr id='w2e2F'><dt id='w2e2F'><q id='w2e2F'><span id='w2e2F'><b id='w2e2F'><form id='w2e2F'><ins id='w2e2F'></ins><ul id='w2e2F'></ul><sub id='w2e2F'></sub></form><legend id='w2e2F'></legend><bdo id='w2e2F'><pre id='w2e2F'><center id='w2e2F'></center></pre></bdo></b><th id='w2e2F'></th></span></q></dt></tr></i><div id='w2e2F'><tfoot id='w2e2F'></tfoot><dl id='w2e2F'><fieldset id='w2e2F'></fieldset></dl></div>

    1. <tfoot id='w2e2F'></tfoot>
      <legend id='w2e2F'><style id='w2e2F'><dir id='w2e2F'><q id='w2e2F'></q></dir></style></legend>

      <small id='w2e2F'></small><noframes id='w2e2F'>

    2. Laravel 缓存与 Redis 非常慢

      时间:2023-10-04
    3. <small id='9LO1J'></small><noframes id='9LO1J'>

      <i id='9LO1J'><tr id='9LO1J'><dt id='9LO1J'><q id='9LO1J'><span id='9LO1J'><b id='9LO1J'><form id='9LO1J'><ins id='9LO1J'></ins><ul id='9LO1J'></ul><sub id='9LO1J'></sub></form><legend id='9LO1J'></legend><bdo id='9LO1J'><pre id='9LO1J'><center id='9LO1J'></center></pre></bdo></b><th id='9LO1J'></th></span></q></dt></tr></i><div id='9LO1J'><tfoot id='9LO1J'></tfoot><dl id='9LO1J'><fieldset id='9LO1J'></fieldset></dl></div>
        <tfoot id='9LO1J'></tfoot>
        <legend id='9LO1J'><style id='9LO1J'><dir id='9LO1J'><q id='9LO1J'></q></dir></style></legend>

          <tbody id='9LO1J'></tbody>

              <bdo id='9LO1J'></bdo><ul id='9LO1J'></ul>

              • 本文介绍了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:clearphp 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模板网!

                上一篇:使用(客户端)javascript 直接连接到 Redis? 下一篇:Laravel 5.2 - 如何从用户的所有设备上注销

                相关文章

                最新文章

              • <i id='Gm90v'><tr id='Gm90v'><dt id='Gm90v'><q id='Gm90v'><span id='Gm90v'><b id='Gm90v'><form id='Gm90v'><ins id='Gm90v'></ins><ul id='Gm90v'></ul><sub id='Gm90v'></sub></form><legend id='Gm90v'></legend><bdo id='Gm90v'><pre id='Gm90v'><center id='Gm90v'></center></pre></bdo></b><th id='Gm90v'></th></span></q></dt></tr></i><div id='Gm90v'><tfoot id='Gm90v'></tfoot><dl id='Gm90v'><fieldset id='Gm90v'></fieldset></dl></div>

                  <legend id='Gm90v'><style id='Gm90v'><dir id='Gm90v'><q id='Gm90v'></q></dir></style></legend>
                1. <small id='Gm90v'></small><noframes id='Gm90v'>

                      <bdo id='Gm90v'></bdo><ul id='Gm90v'></ul>
                  1. <tfoot id='Gm90v'></tfoot>