laravelredispredis

Why Redis set method raised error on Laravel site?


On Laravel 11 site I add Redis and test it in a console :

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);
    }
}

and I have error :

$ 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()

I used both \Redis::set( and Redis::set( with same error My PHPstorm also proposed to import :

//use Redis;
//use SimplePie\Cache\Redis;

but with same errors.

In config/database.php I have :

    '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'),
    ],
],

and in .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

What is wrong and which library/methods are better to use ?

In my Ubuntu Redis is installed and I can open it with cli console.

MORE INFO :

I my command file is declared :

use Illuminate\Support\Facades\Redis;

I deleted files inside of /bootstrap/cache

and deleted file/directory :

composer.lock
vendor

and run :

composer install

with output mentioning "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

How to check which driver is used now? It seems to me they all my options are valid...

I can open redis-cli and phpmyadminer and can see/add valid data...

MORE INFO 2:

I found predis subdirectory

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

and

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

Not any "redis" subdirectory. I considered as I have "predis/predis" installed, I do not need any packages. Do I need some packages to install more?

    "laravel/framework": "^11.9",
    "predis/predis": "^2.2",

Solution

  • After in config/app.php I added redis parameter :

    'aliases' => Facade::defaultAliases()->merge([
        ...
        'Redis' => Illuminate\Support\Facades\Redis::class,
        ])->toArray(),
    

    works ok.

    Why Illuminate\Support\Facades\Redis in declaration of test file does not work I do not know...