在 Laravel 11 站点上,我添加了 redis 并在控制台中测试它:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class RedisTest extends Command
{
protected $signature = 'app:redis-test';
protected $description = 'Command description';
public function handle()
{
\Redis::set('city_name', 'San Francisco city');
echo '::RedisTest::'.print_r('RedisTest::',true);
$cityName = \Redis::get('city_name');
echo '::RedisTest $cityName::'.print_r($cityName,true);
}
}
我有错误:
$ php artisan app:redis-test
Error
Class "Redis" not found
at app/Console/Commands/RedisTest.php:38
34▕ * Execute the console command.
35▕ */
36▕ public function handle()
37▕ {
➜ 38▕ \Redis::set('city_name', 'San Francisco city');
39▕
40▕ echo '::RedisTest::'.print_r('RedisTest::',true);
41▕
42▕ $cityName = \Redis::get('city_name');
+13 vendor frames
14 artisan:13
Illuminate\Foundation\Application::handleCommand()
我同时使用了
\Redis::set(
和Redis::set(
,但出现了同样的错误
我的PHPstorm也建议导入:
//use Redis;
//use SimplePie\Cache\Redis;
但有同样的错误。
在 config/database.php 中我有:
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
在 .env 中:
CACHE_DRIVER=file
CACHE_STORE=database
CACHE_PREFIX=
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_CLIENT=predis
出了什么问题,使用哪些库/方法更好?
在我的ubuntu中安装了redis,我可以使用cli控制台打开它。
更多信息:
我的命令文件已声明:
use Illuminate\Support\Facades\Redis;
我删除了
/bootstrap/cache
中的文件
并删除文件/目录:
composer.lock 供应商
然后运行:
composer install
输出提到“predis”:
o composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Updating dependencies
Lock file operations: 176 installs, 0 updates, 0 removals
...
- Locking predis/predis (v2.2.2)
- Installing predis/predis (v2.2.2): Extracting archive
INFO Discovering packages.
barryvdh/laravel-debugbar ................................................................................................................... DONE
blade-ui-kit/blade-heroicons ................................................................................................................ DONE
blade-ui-kit/blade-icons .................................................................................................................... DONE
hisorange/browser-detect .................................................................................................................... DONE
laravel/breeze .............................................................................................................................. DONE
laravel/sail ................................................................................................................................ DONE
laravel/telescope ........................................................................................................................... DONE
laravel/tinker .............................................................................................................................. DONE
livewire/livewire ........................................................................................................................... DONE
mews/purifier ............................................................................................................................... DONE
nesbot/carbon ............................................................................................................................... DONE
nunomaduro/collision ........................................................................................................................ DONE
nunomaduro/termwind ......................................................................................................................... DONE
omnia-digital/livewire-calendar ............................................................................................................. DONE
rappasoft/laravel-livewire-tables ........................................................................................................... DONE
spatie/eloquent-sortable .................................................................................................................... DONE
spatie/laravel-backup ....................................................................................................................... DONE
spatie/laravel-collection-macros ............................................................................................................ DONE
spatie/laravel-data ......................................................................................................................... DONE
spatie/laravel-feed ......................................................................................................................... DONE
spatie/laravel-health ....................................................................................................................... DONE
spatie/laravel-livewire-wizard .............................................................................................................. DONE
spatie/laravel-permission ................................................................................................................... DONE
spatie/laravel-signal-aware-command ......................................................................................................... DONE
spatie/laravel-tags ......................................................................................................................... DONE
spatie/laravel-translatable ................................................................................................................. DONE
spatie/php-structure-discoverer ............................................................................................................. DONE
willvincent/feeds ........................................................................................................................... DONE
wire-elements/modal ......................................................................................................................... DONE
如何查看现在使用的是哪个驱动程序?在我看来,我所有的选择都是有效的......
我可以打开 redis-cli 和 phpmyadminer 并且可以查看/添加有效数据...
更多信息2: 我找到了 predis 子目录
Project/vendor/predis/predis$ ls -la
total 37
drwxrwxrwx 1 root root 4096 вер 13 2023 .
drwxrwxrwx 1 root root 0 сер 2 10:24 ..
-rwxrwxrwx 1 root root 260 вер 13 2023 autoload.php
-rwxrwxrwx 1 root root 1316 вер 13 2023 composer.json
drwxrwxrwx 1 root root 0 вер 13 2023 docker
-rwxrwxrwx 1 root root 1148 вер 13 2023 LICENSE
-rwxrwxrwx 1 root root 20436 вер 13 2023 README.md
drwxrwxrwx 1 root root 4096 вер 13 2023 src
和
Project/vendor/predis/predis/src$ ls -la
total 118
drwxrwxrwx 1 root root 4096 вер 13 2023 .
drwxrwxrwx 1 root root 4096 вер 13 2023 ..
-rwxrwxrwx 1 root root 1752 вер 13 2023 Autoloader.php
-rwxrwxrwx 1 root root 1150 вер 13 2023 ClientConfiguration.php
-rwxrwxrwx 1 root root 20679 вер 13 2023 ClientContextInterface.php
-rwxrwxrwx 1 root root 389 вер 13 2023 ClientException.php
-rwxrwxrwx 1 root root 27495 вер 13 2023 ClientInterface.php
-rwxrwxrwx 1 root root 18758 вер 13 2023 Client.php
drwxrwxrwx 1 root root 4096 вер 13 2023 Cluster
drwxrwxrwx 1 root root 0 вер 13 2023 Collection
drwxrwxrwx 1 root root 4096 вер 13 2023 Command
-rwxrwxrwx 1 root root 2140 вер 13 2023 CommunicationException.php
drwxrwxrwx 1 root root 0 вер 13 2023 Configuration
drwxrwxrwx 1 root root 4096 вер 13 2023 Connection
drwxrwxrwx 1 root root 0 вер 13 2023 Monitor
-rwxrwxrwx 1 root root 457 вер 13 2023 NotSupportedException.php
drwxrwxrwx 1 root root 4096 вер 13 2023 Pipeline
-rwxrwxrwx 1 root root 404 вер 13 2023 PredisException.php
drwxrwxrwx 1 root root 4096 вер 13 2023 Protocol
drwxrwxrwx 1 root root 0 вер 13 2023 PubSub
drwxrwxrwx 1 root root 0 вер 13 2023 Replication
drwxrwxrwx 1 root root 4096 вер 13 2023 Response
drwxrwxrwx 1 root root 0 вер 13 2023 Session
drwxrwxrwx 1 root root 0 вер 13 2023 Transaction
不是任何“redis”子目录。我认为因为我已经安装了“predis/predis”,所以我不需要任何软件包。我需要一些软件包来安装更多吗?
"laravel/framework": "^11.9",
"predis/predis": "^2.2",
提前致谢!
在 config/app.php 中我添加了 redis 参数:
'aliases' => Facade::defaultAliases()->merge([
...
'Redis' => Illuminate\Support\Facades\Redis::class,
])->toArray(),
工作正常。
为什么测试文件声明中的
Illuminate\Support\Facades\Redis
不起作用我不知道......