The implementation of multiple redis libraries configured by laravel

Time:2020-3-21

Introducer

After compiling, installing and installing the extension, redis can be used normally. But there are other operations in laravel.

Installation extension

To use redis in laravel, you need to installpredisExpand. Usecomposer require predis/predisJust install it.

Modify configuration

  1. Redis is configured inconfig/database.phpFile, modify as required, I don’t need to change here;
  2. Set the default cache to redis, and set it to redis.evnIn fileCACHE_DRIVER=redis
  3. Set the session driver to redis, and set the session driver to redis.envIn fileSESSION_DRIVER=redis

Configure multiple libraries

After the above configuration, multiple services use redis. If they all use the same library, this is obviously unreasonable. We can configure multiple connections to solve this problem.
Redis has 16 libraries by default, which are set in the serverredis.confOfdatabaseValues can be modified.

Let’s look at it first.config/database.phpDefault connection for


'redis' => [

    'client' => 'predis',

    'default' => [
      'host' => env('REDIS_HOST', '127.0.0.1'),
      'password' => env('REDIS_PASSWORD', null),
      'port' => env('REDIS_PORT', 6379),
      'database' => env('REDIS_DB', 0),
    ],

    'cache' => [
      'host' => env('REDIS_HOST', '127.0.0.1'),
      'password' => env('REDIS_PASSWORD', null),
      'port' => env('REDIS_PORT', 6379),
      'database' => env('REDIS_CACHE_DB', 1),
    ],
  
  ],

There are two connections by default, namelydefaultandcache。 Let’s take a lookconfig/cache.phpRedis configuration in


'redis' => [
      'driver' => 'redis',
      'connection' => 'cache',
    ],

You can see itconnectionThe value iscache, i.e. usingconfig/database.phpRedis’scache

Modify belowconfig/database.phpFor redis, add a session connection, as follows


'redis' => [

    'client' => 'predis',

    'default' => [
      'host' => env('REDIS_HOST', '127.0.0.1'),
      'password' => env('REDIS_PASSWORD', null),
      'port' => env('REDIS_PORT', 6379),
      'database' => env('REDIS_DB', 0),
    ],

    'cache' => [
      'host' => env('REDIS_HOST', '127.0.0.1'),
      'password' => env('REDIS_PASSWORD', null),
      'port' => env('REDIS_PORT', 6379),
      'database' => env('REDIS_CACHE_DB', 1),
    ],

    'session' => [
      'host' => env('REDIS_HOST', '127.0.0.1'),
      'password' => env('REDIS_PASSWORD', null),
      'port' => env('REDIS_PORT', 6379),
      'database' => env('REDIS_SESSION_DB', 2),
    ],

  ],

Next on.envAdd inSESSION_CONNECTION=session

test

After the above operation, it has been modified. In summarydefaultThe 0 library is used,cache1 library is used,sessionLibrary 2 is used.

Use the following code to test

/**
   *Test redis storage
   */
  public function testRedis()
  {
    //Redis facade
    Redis::setex('facades', 30, 'i am facades');
    // Cache
    Cache::put('cache', 'i am cache', now()->addMinute(30));
    //Because cache is redis by default, it is the same as the above statement
    // Cache::store('redis')->put('cache', now(), now()->addMinute(30));
  }

After running the above code, use theredis-cliLook at storage

You can see the storage of each library

  1. Use redis facade operation, the default isconfig/database.phpRedis’sdefaultConnect and store the data in 0 library;
  2. Use the cache operation becauseconfig/cache.phpRedis’sconnectionSet tocacheOf course, it’s a library;
  3. Session is also based onSESSION_CONNECTION=sessionConfiguration, correctly stored in 2 libraries;

When using the redis facade, you can also specify the connection


$redis = Redis::connection('session');
$redis->setex('facades_connection', 30, 'i am facades_connection');

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

Hey, would you like a list of star mainstream MAC suite configuration guides

“This article will give you an overall introduction to (front-end development) the mainstream commonly used MAC suite ha” Just started MAC or ready to start friends, do not come to see??? Configuration list of mainstream MAC applications & Tools Quick operation 1 Alfred <div align=center><img style=”width:90px” /> Efficiency artifact, we use it to help us […]