We use cookies
We use cookies to optimize our website. By continuing to browse the site, you agree to our use of cookies.
New design
Control panel
  • Русский
  • Українська
  • English
  • UAH
  • USD
  • RUB
  • EUR
  • 0-800-307-307 Hotline
  • +38 (044) 392-74-33 Kiev
  • +38 (057) 728-39-00 Kharkiv
  • +38 (056) 794-38-31 Dnipro
  • +38 (032) 229-58-93 Lviv
  • +38 (048) 738-57-70 Odessa
  • +38(093) 170-15-42  Life
  • +38 (067) 400-88-44 Kievstar
  • +1(888)393-24-51  USA, Toll free
  • +44(131)507-01-14  Great Britain
  • +7 (499) 348-28-61 Moscow

2.14.2.6. Setting up Redis in Laravel

Important points:

  • You can only connect to Redis on a hosting via a Unix socket.
  • PECL extension is available on hosting by default PhpRedis... There is no need to install additional clients.
  • The instructions describe a general way to configure Redis. It may not be suitable for sites where changes have been made to the functionality of working with Redis.

For settings Redis in Laravel do the following:

  1. Edit the file .envlocated in the root of the project:
    • Find the Redis config block:
      REDIS_HOST=127.0.0.1
      REDIS_PASSWORD=null
      REDIS_PORT=6379
    • Replace the lines with the following:
      REDIS_HOST=/home/example/.system/redis.sock
      REDIS_DB=0
      REDIS_CACHE_DB=1
      REDIS_PREFIX=doepref_
    • In the line with the parameter REDIS_HOST instead example indicate hosting account namewhere the site is hosted.
    • In parameters REDIS_DB and REDIS_CACHE_DB provide a unique identifier for the Redis database.
    • In the parameter REDIS_PREFIX specify the required prefix.
  2. Edit the file database.phplocated in the directory config/:
    • Find the Redis connection code in the file, it will start with 'redis' => [... The connection setup should look something like this:
           'redis' => [
       
              'client' => env('REDIS_CLIENT', 'phpredis'),
       
              'options' => [
                  'cluster' => env('REDIS_CLUSTER', 'redis'),
                  'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
              ],
       
              'default' => [
                  'host'     => env('REDIS_HOST'),
                  'password' => env('REDIS_PASSWORD'),
                  'port'     => env('REDIS_PORT'),
                  'database' => env('REDIS_DB', 0),
              ],
       
              'cache' => [
                  'host'     => env('REDIS_HOST'),
                  'password' => env('REDIS_PASSWORD'),
                  'port'     => env('REDIS_PORT'),
                  'database' => env('REDIS_CACHE_DB', 1),
              ],
       
          ],
  3. Check Redis is working. For the test, edit the file web.phplocated in the directory routes/by adding the following code to it:
    Route::get('redis-test', function (){
    if(!Redis::get('test_time')){
    Redis::set('test_time', date(DATE_RFC822));
    }
    return "Date inserted in database: ".Redis::get('test_time');
    });

    Open your project by link http://example.com/redis-testwhere instead of example.com enter your domain name. After the transition, the time of the first access to this link should be displayed.

Attention!

Redis database id must be unique for everyone CMS and for different types of caching, so that there are no errors with the intersection of data from different services.