Views: 6330
Last Modified: 25.05.2021

Add a titled connection into connections section to create a connection inside bitrix/.settings.php settings file.

Redis

Ensure that you have installed Redis extension for working via PHP.

Standard connection:

 'connections' => [
    'value' => [
      'default' => [
        'className' => \Bitrix\Main\DB\MysqliConnection::class,
        //... config for existing database connection
      ],
      'custom.redis' => [
        'className' => \Bitrix\Main\Data\RedisConnection::class,
        'port' => 6379,
        'host' => '127.0.0.1',
        'serializer' => \Redis::SERIALIZER_IGBINARY,
      ],
      'custom2.redis' => [
        'className' => \Bitrix\Main\Data\RedisConnection::class,
        'port' => 6379,
        'host' => '127.0.0.4',
        'serializer' => \Redis::SERIALIZER_IGBINARY,
      ],
    ],
    'readonly' => true,
  ]

You can find additional information about serializer config variants in the official documentation.

Cluster

When creating cluster from redis servers, just add servers config.

'connections' => [
    'value' => [
      'default' => [
        'className' => \Bitrix\Main\DB\MysqliConnection::class,
        //... config for existing database connection
      ],
      'custom.redis' => [
        'className' => \Bitrix\Main\Data\RedisConnection::class,
        'servers' => [
            [
                'port' => 6379,
                'host' => '127.0.0.1',
            ],
            [
                'port' => 6379,
                'host' => '127.0.0.2',
            ],
            [
                'port' => 6379,
                'host' => '127.0.0.3',
            ],
        ],
        'serializer' => \Redis::SERIALIZER_IGBINARY,
        'persistent' => false,
        'failover' => \RedisCluster::FAILOVER_DISTRIBUTE,
        'timeout' => null,
        'read_timeout' => null,
      ],
    ],
    'readonly' => true,
  ]

Additional information about configurations for variants serializer, persistent, failover, timeout, read_timeout can be found in the official documentation.

Use

To get a connection instance, you may query the connection name using the method \Bitrix\Main\Application::getConnection.

/** @var \Redis $redisConnection **/
$redisConnection = \Bitrix\Main\Application::getConnection('custom.redis')->getResource();
$redisConnection->setnx('foo', 'bar');

Memcache

Ensure that you have installed Memcache extension for working via PHP.

Standard connection

'connections' => [
    'value' => [
      'default' => [
        'className' => \Bitrix\Main\DB\MysqliConnection::class,
        //... config for existing database connection
      ],
      'custom.memcache' => [
        'className' => \Bitrix\Main\Data\MemcacheConnection::class,
        'port' => 11211,
        'host' => '127.0.0.1',
      ],
      'custom42.memcache' => [
        'className' => \Bitrix\Main\Data\MemcacheConnection::class,
        'port' => 6379,
        'host' => '127.0.0.4',
      ],
    ],
    'readonly' => true,
  ]

Cluster

When creating cluster from memcache servers, just add servers config.

'connections' => [
    'value' => [
      'default' => [
        'className' => \Bitrix\Main\DB\MysqliConnection::class,
        //... config for existing database connection
      ],
      'custom.memcache' => [
        'className' => \Bitrix\Main\Data\MemcacheConnection::class,
        'servers' => [
            [
                'port' => 11211,
                'host' => '127.0.0.1',
                'weight' => 1, //read more about weight config in the memcahe documenation
            ],
            [
                'port' => 11211,
                'host' => '127.0.0.2',
                'weight' => 1, //read more about weight config in the memcahe documenation
            ],
        ],
      ],
    ],
    'readonly' => true,
  ]

Use

To get a connection instance, query connection name using the method \Bitrix\Main\Application::getConnection.

/** @var \Memcache $memcacheConnection **/
$memcacheConnection = \Bitrix\Main\Application::getConnection('custom.memcache')->getResource();
$memcacheConnection->set('foo', 'bar');



Courses developed by Bitrix24