2017 © Pedro Peláez
 

library redis

image

shibo/redis

  • Wednesday, April 25, 2018
  • by qishibo
  • Repository
  • 3 Watchers
  • 4 Stars
  • 47 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 7 Versions
  • 4 % Grown

The README.md

PHP Redis

Redis client for php, which supports single redis server, or redis Master-Slave clusters., (*1)

Install

1.composer is recommended, (*2)

composer require shibo/redis, (*3)

or in your composer.json, (*4)

{
    "require": {
        "shibo/redis" : "^1.0"
    }
}

2.if you do not use composer, you should include 'Autoload.php' in your code, (*5)

Example

1.single redis server, (*6)

read & write operations are all executed in the single serve., (*7)

use \Redis\SingleClient;
use \Redis\Drivers\RedisFactory;

include 'Autoload.php';

$config = ['host' => '127.0.0.1', 'port' => 6379, 'weight' => 1];

// if need auth
// $config = ['host' => '127.0.0.1', 'port' => 6379, 'weight' => 1, 'auth' => 'qii'];

$redis = new SingleClient(
    $config,
    RedisFactory::PHPREDIS // this is optional param, default is PHPREDIS driver
);

$redis->set('name', 'qii404'); // true
$redis->get('name'); // 'qii404'

2.redis cluster without slaves, (*8)

read & write operations executed in the same one server of the cluster., (*9)

use Redis\Drivers\RedisFactory;
use Redis\WithoutSlavesClient;
use Redis\Hash;
use Redis\Key;

include 'Autoload.php';

$config = [
    ['host' => '127.0.0.1', 'port' => 6379, 'weight' => 1],
    ['host' => '127.0.0.1', 'port' => 6380],
];

// hash stragety, you can also define your stragety in Hash folder
$hash = new Hash\Consistant();

// key hasher, such as new Md5 or Cr32, you can add it in Key folder
$calculator = new Key\Cr32();
// $calculator = new Key\Md5();

$redis = new WithoutSlavesClient(
    $config,
    $hash,
    $calculator,
    RedisFactory::PHPREDIS // this is optional param, default is PHPREDIS driver
);

// when using the same key, both read & write operation executed in the same server, such as port 6379
$redis->hset('profile', 'name', 'qii44'); // true
$redis->hget('profile', 'name'); // 'qii404'

3.redis cluster with slaves, (*10)

read & write operations executed in the different servers, read from the slave servers, write from the master servers, (*11)

(You should config it right for 'm' & 's', such as 6381 is slave of 6379, 6382 is slave of 6380)., (*12)

use Redis\Drivers\RedisFactory;
use Redis\WithSlavesClient;
use Redis\Hash;
use Redis\Key;

include 'Autoload.php';

$config = [
    'm' =>[
        ['host' => '127.0.0.1', 'port' => 6379, 'weight' => 1],
        ['host' => '127.0.0.1', 'port' => 6380],
    ],
    's' =>[
        ['host' => '127.0.0.1', 'port' => 6381],
        ['host' => '127.0.0.1', 'port' => 6382],
    ]
];

// hash stragety, you can also define your stragety in Hash folder
$hash = new Hash\Consistant();

// key hasher, such as new Md5 or Cr32, you can add it in Key folder
$calculator = new Key\Cr32();
// $calculator = new Key\Md5();

$redis = new WithSlavesClient(
    $config,
    $hash,
    $calculator,
    RedisFactory::PHPREDIS // this is optional param, default is PHPREDIS driver
);

$redis->zadd('key', 99, 'qii404'); // true; executes in master server, such as port 6379
$redis->zscore('key', 'qii404'); // 99; executes in slave server, such as port 6381

Attentions

  • When you use the 'weight' in the config, it works only in the cluster mode, and when in the Master-Slave mode, you should config in the 'm' arrays but not 's' arrays.
  • The different clients are implemented by polymorphism, so it is simple and efficient, but you need to new a client yourself.
  • If you have any questions, do not ask me.

The Versions

25/04 2018

dev-analysis-qv2aMV

dev-analysis-qv2aMV

  Sources   Download

MIT

The Development Requires

by qii404

25/04 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Development Requires

by qii404

09/03 2018

dev-dev

dev-dev

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by qii404

27/02 2018

1.1.2

1.1.2.0

  Sources   Download

MIT

The Development Requires

by qii404

04/08 2017

v1.1.1

1.1.1.0

  Sources   Download

MIT

The Development Requires

by qii404

02/08 2017

v1.1.0

1.1.0.0

  Sources   Download

MIT

The Development Requires

by qii404

09/03 2016

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Development Requires

by qii404