Code Monkey home page Code Monkey logo

psredis's Introduction

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.

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:

"require": {
    "jamescauwelier/psredis": "~1.1"
},

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.

// 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.

// 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.

// 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

Here is an example using the incremental backoff strategy:

$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

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:

./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:

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:

vagrant up

After that, run the integration tests with

./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.

psredis's People

Contributors

jamescauwelier avatar geniuslinchao avatar advisory-board-company avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.