2017 © Pedro Peláez
 

library psredis

Sentinel client for the popular php redis client

image

sparkcentral/psredis

Sentinel client for the popular php redis client

  • Tuesday, May 24, 2016
  • by pulyaevskiy
  • Repository
  • 1 Watchers
  • 3 Stars
  • 98,097 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 39 Forks
  • 0 Open issues
  • 11 Versions
  • 4 % Grown

The README.md

PSRedis - Sentinel wrapper for PHP redis clients

A PHP client for redis sentinel connections as a wrapper on other redis clients. The name stands for PHP Sentinel client for Redis. I am sure other would be more creative in coming up with a name., (*1)

Installation

The easiest way to install is by using composer. The package is available on packagist so installing should be as easy as putting the following in your composer file:, (*2)

"require": {
    "sparkcentral/psredis": "*"
},

Usage

Basic example

The most basic example makes use of the Predis adapter by default. This is the least amount of code needed to get going with PSRedis, although we are making plans to make the configuration more concise in the future., (*3)

// configure where to find the sentinel nodes

$sentinel1 = new Client('192.168.50.40', '26379');
$sentinel2 = new Client('192.168.50.41', '26379');
$sentinel3 = new Client('192.168.50.30', '26379');

// now we can configure the master name and the sentinel nodes

$masterDiscovery = new MasterDiscovery('integrationtests');
$masterDiscovery->addSentinel($sentinel1);
$masterDiscovery->addSentinel($sentinel2);
$masterDiscovery->addSentinel($sentinel3);

// discover where the master is

$master = $masterDiscovery->getMaster();

Auto failover

You have the option of letting the library handle the failover. That means that in case of connection errors, the library will select the new master or reconnect to the existing ones depending of what the sentinels decide is the proper action. After this reconnection, the command will be re-tried, but only once. No option for backoff exists here., (*4)

// configuration of $masterDiscovery
$masterDiscovery = ...

// using the $masterDiscovery as a dependency in an Highly Available Client (HAClient)
$HAClient = new HAClient($masterDiscovery);
$HAClient->set('test', 'ok');
$test = $HAClient->get('test');

Customizing the adapter

You can choose what kind of client adapter to use or even write your own. If you write your own you need to make sure you implement the \PSRedis\Client\ClientAdapter interface., (*5)

// we need a factory to create the clients
$clientFactory = new PredisClientCreator();

// we need an adapter for each sentinel client too!

$clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL);
$sentinel1 = new Client('192.168.50.40', '26379', $clientAdapter);

$clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL);
$sentinel2 = new Client('192.168.50.41', '26379', $clientAdapter);

$clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL);
$sentinel3 = new Client('192.168.50.30', '26379', $clientAdapter);

// now we can configure the master name and the sentinel nodes

$masterDiscovery = new MasterDiscovery('integrationtests');
$masterDiscovery->addSentinel($sentinel1);
$masterDiscovery->addSentinel($sentinel2);
$masterDiscovery->addSentinel($sentinel3);

// discover where the master is

$master = $masterDiscovery->getMaster();

Configuring backoff

When we fail to discover the location of the master, we need to back off and try again. The back off mechanism is configurable and you can implement your own by implementing the \PSRedis\Client\BackoffStrategy, (*6)

Here is an example using the incremental backoff strategy:, (*7)

$sentinel = new Client('192.168.50.40', '26379');
$masterDiscovery = new MasterDiscovery('integrationtests');
$masterDiscovery->addSentinel($sentinel);

// create a backoff strategy (half a second initially and increment with half of the backoff on each succesive try)
$incrementalBackoff = new Incremental(500, 1.5);
$incrementalBackoff->setMaxAttempts(10);

// configure the master discovery with this backoff strategy
$masterDiscovery->setBackoffStrategy($incrementalBackoff);

// try to discover the master
$master = $masterDiscovery->getMaster();

Testing

Note: For testing, we still use PHPUnit 3.7 due to a bug in PhpStorm not allowing us to run unit tests from our IDE. See http://youtrack.jetbrains.com/issue/WI-21666, (*8)

Unit testing

We use PHPUnit for unit testing and Phake for mocking. Both are installed using composer. Running the unit tests can be done using the following command:, (*9)

./vendor/bin/phpunit -c ./phpunit.xml --bootstrap ./tests/bootstrap.php

Integration testing

Prerequisites

Make sure you have installed these on your machine before running the tests:, (*10)

Running the tests

Before running the tests you need to setup the VM to run the tests on. Use the following command to bring the box up:, (*11)

vagrant up

After that, run the integration tests with, (*12)

./vendor/bin/phpunit -c ./phpunit.int.xml --bootstrap ./tests/bootstrap.int.php

You will see some warnings that need to be fixed, but the tests themselves should all pass. The warnings are a result of the reset of the environment just before every integration test., (*13)

The Versions

24/05 2016

dev-develop

dev-develop

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

24/05 2016

0.2.8

0.2.8.0

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

27/02 2015

dev-master

9999999-dev

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

27/02 2015

0.2.7-alpha

0.2.7.0-alpha

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

27/02 2015

0.2.5-alpha

0.2.5.0-alpha

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

24/02 2015

0.2.4-alpha

0.2.4.0-alpha

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

26/08 2014

0.2.3-alpha

0.2.3.0-alpha

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

26/08 2014

0.2.2-alpha

0.2.2.0-alpha

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

23/07 2014

0.2.1-alpha

0.2.1.0-alpha

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

16/07 2014

0.2-alpha

0.2.0.0-alpha

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel

13/07 2014

0.1-alpha

0.1.0.0-alpha

Sentinel client for the popular php redis client

  Sources   Download

MIT

The Requires

 

The Development Requires

redis predis nosql sentinel