尽管设置了redis进行缓存,Laravel仍然使用数据库表进行缓存

问题描述 投票:0回答:1

从文档中我为缓存设置了redis。但是每当我尝试到达任何端点时,都会出现错误,因为 ebing 无法在数据库中找到表缓存。我只想使用 redis 而不是数据库,但我不确定配置文件中还缺少什么。

这些是我拥有的文件。例如在 config/database

<?php declare(strict_types=1);
return [
    'default' =>'mysql',
    'connections' => [
        'mysql' => [ ... ],
    ],
    'redis' => [
        'client' => env('REDIS_CLIENT', 'predis'),
        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD'),
            'port' => '6379'
        ],
        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD'),
            'port' => '6379',
        ],
    ],
];

config/cache.php 我有以下内容:

<?php declare(strict_types=1);

return [
    'default' => env('CACHE_STORE', 'redis'),
    "driver" => env("CACHE_DRIVER", "redis"),
    'stores' => [
        'redis' => [
            'driver' => 'redis',
            'connection' => env('REDIS_CACHE_CONNECTION', 'cache'),
            'lock_connection' => env('REDIS_CACHE_LOCK_CONNECTION', 'redis'),
        ],
    ],
    'prefix' => env('CACHE_PREFIX', 'MYT')
];

每当我尝试到达任何端点时,都会收到以下错误

{
"message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mytable.cache' doesn't exist (Connection: mysql, SQL: select * from `cache` where `key` = MYTilluminate:foundation:down limit 1)",
"exception": "Illuminate\\Database\\QueryException",
"file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
"line": 813,
"trace": [
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
        "line": 767,
        "function": "runQueryCallback",
        "class": "Illuminate\\Database\\Connection",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
        "line": 398,
        "function": "run",
        "class": "Illuminate\\Database\\Connection",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
        "line": 2996,
        "function": "select",
        "class": "Illuminate\\Database\\Connection",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
        "line": 2981,
        "function": "runSelect",
        "class": "Illuminate\\Database\\Query\\Builder",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
        "line": 3569,
        "function": "Illuminate\\Database\\Query\\{closure}",
        "class": "Illuminate\\Database\\Query\\Builder",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
        "line": 2980,
        "function": "onceWithColumns",
        "class": "Illuminate\\Database\\Query\\Builder",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php",
        "line": 343,
        "function": "get",
        "class": "Illuminate\\Database\\Query\\Builder",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Cache/DatabaseStore.php",
        "line": 103,
        "function": "first",
        "class": "Illuminate\\Database\\Query\\Builder",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Cache/Repository.php",
        "line": 116,
        "function": "get",
        "class": "Illuminate\\Cache\\DatabaseStore",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Cache/Repository.php",
        "line": 85,
        "function": "get",
        "class": "Illuminate\\Cache\\Repository",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Foundation/CacheBasedMaintenanceMode.php",
        "line": 75,
        "function": "has",
        "class": "Illuminate\\Cache\\Repository",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php",
        "line": 65,
        "function": "active",
        "class": "Illuminate\\Foundation\\CacheBasedMaintenanceMode",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 183,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php",
        "line": 49,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 183,
        "function": "handle",
        "class": "Illuminate\\Http\\Middleware\\HandleCors",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php",
        "line": 57,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 183,
        "function": "handle",
        "class": "Illuminate\\Http\\Middleware\\TrustProxies",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 119,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
        "line": 175,
        "function": "then",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
        "line": 144,
        "function": "sendRequestThroughRouter",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
        "line": 1188,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/public/index.php",
        "line": 17,
        "function": "handleRequest",
        "class": "Illuminate\\Foundation\\Application",
        "type": "->"
    },
    {
        "file": "/projects/mytable/api2/vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php",
        "line": 23,
        "function": "require_once"
    }
]
}
php laravel redis
1个回答
0
投票

该问题很可能是由 Laravel 的 维护模式 引起的。它使用专用配置设置来确定在检查维护模式当前是否处于活动状态时查询哪个存储。

您可以在

maintenance
配置文件中的
app.php
键下找到这些设置:

    /*
    |--------------------------------------------------------------------------
    | Maintenance Mode Driver
    |--------------------------------------------------------------------------
    |
    | These configuration options determine the driver used to determine and
    | manage Laravel's "maintenance mode" status. The "cache" driver will
    | allow maintenance mode to be controlled across multiple machines.
    |
    | Supported drivers: "file", "cache"
    |
    */

    'maintenance' => [
        'driver' => env('APP_MAINTENANCE_DRIVER', 'file'),
        'store' => env('APP_MAINTENANCE_STORE', 'database'),
    ],

您想要将

cache
设置为维护模式 driver 并将
redis
设置为 store,如下所示:

    'maintenance' => [
        'driver' => env('APP_MAINTENANCE_DRIVER', 'cache'),
        'store' => env('APP_MAINTENANCE_STORE', 'redis'),
    ],

或者,您可以使用环境变量来设置这两个值:

APP_MAINTENANCE_DRIVER=cache
APP_MAINTENANCE_STORE=redis

更改设置后运行

php artisan config:clear
命令,问题应该得到解决。

© www.soinside.com 2019 - 2024. All rights reserved.