Skip to content

Commit

Permalink
v180 Added config for connection
Browse files Browse the repository at this point in the history
  • Loading branch information
cheprasov committed Mar 8, 2018
1 parent 93fd646 commit a1b33c0
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 12 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ php:
- '5.6'
- '7.0'
- '7.1'
- '7.2'
- hhvm

matrix:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## CHANGELOG

### v1.8.0 (2018-03-08)
- Added configuration for connection: timeout & flags.

### v1.7.2 (2017-08-19)
- Fixed bug of ClientFactory with default client version

Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[![Latest Stable Version](https://poser.pugx.org/cheprasov/php-redis-client/v/stable)](https://packagist.org/packages/cheprasov/php-redis-client)
[![Total Downloads](https://poser.pugx.org/cheprasov/php-redis-client/downloads)](https://packagist.org/packages/cheprasov/php-redis-client)
# RedisClient v1.7.2 for PHP >= 5.5
# RedisClient v1.8.0 for PHP >= 5.5

## About
RedisClient is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from __2.6__ to __4.0__
Expand All @@ -17,7 +17,7 @@ RedisClient is a fast, fully-functional and user-friendly client for Redis, opti
- Easy to use with IDE, client has PHPDocs for all supported versions.
- By default, the client works with the latest stable version of Redis (4.0).
- The client was tested on the next latest versions of Redis: 2.6.17, 2.8.24, 3.0.7, 3.2.8, 4.0.8.
- Also, the client was tested on PHP 5.5, 5.6, 7.0, 7.1, HHVM.
- Also, the client was tested on PHP 5.5, 5.6, 7.0, 7.1, 7.2, HHVM.

## Usage

Expand All @@ -29,8 +29,23 @@ $config = [
'server' => '127.0.0.1:6379',

// Optional. Default = 1
// The timeout for reading/writing data over the socket
'timeout' => 2,

// Optional. Default = null
// See more here: http://php.net/manual/en/function.stream-socket-client.php
'connection' => [
// Optional. Default = ini_get("default_socket_timeout")
// The timeout only applies while making connecting the socket
'timeout' => 2,

// Optional. Default = STREAM_CLIENT_CONNECT
// Bitmask field which may be set to any combination of connection flags.
// Currently the select of connection flags is limited to STREAM_CLIENT_CONNECT (default),
// STREAM_CLIENT_ASYNC_CONNECT and STREAM_CLIENT_PERSISTENT.
'flags' => STREAM_CLIENT_CONNECT
],

// Optional. Specify version to avoid some unexpected errors.
'version' => '3.2.8',

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cheprasov/php-redis-client",
"version": "1.7.2",
"version": "1.8.0",
"description": "Php client for Redis. It is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from 2.6 to 4.0",
"homepage": "http://github.com/cheprasov/php-redis-client",
"minimum-stability": "stable",
Expand Down
12 changes: 7 additions & 5 deletions src/RedisClient/Client/AbstractRedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@

abstract class AbstractRedisClient {

const VERSION = '1.7.2';
const VERSION = '1.8.0';

const CONFIG_SERVER = 'server';
const CONFIG_TIMEOUT = 'timeout';
const CONFIG_SERVER = 'server';
const CONFIG_TIMEOUT = 'timeout';
const CONFIG_DATABASE = 'database';
const CONFIG_PASSWORD = 'password';
const CONFIG_CLUSTER = 'cluster';
const CONFIG_VERSION = 'version';
const CONFIG_CLUSTER = 'cluster';
const CONFIG_VERSION = 'version';
const CONFIG_CONNECTION = 'connection';

/**
* Default configuration
Expand All @@ -50,6 +51,7 @@ abstract class AbstractRedisClient {
'init_on_error_moved' => false,
'timeout_on_error_tryagain' => 0.05,
],
self::CONFIG_CONNECTION => null,
];

/**
Expand Down
5 changes: 3 additions & 2 deletions src/RedisClient/Connection/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ class ConnectionFactory {
/**
* @param string $server
* @param int $timeout
* @param array|null $connection
* @param callable $onConnect
* @return StreamConnection
*/
public static function createStreamConnection($server, $timeout, $onConnect = null) {
$Connection = new StreamConnection($server, $timeout);
public static function createStreamConnection($server, $timeout, $connection = null, $onConnect = null) {
$Connection = new StreamConnection($server, $timeout, $connection);
if ($onConnect) {
$Connection->onConnect($onConnect);
}
Expand Down
42 changes: 40 additions & 2 deletions src/RedisClient/Connection/StreamConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class StreamConnection implements ConnectionInterface {
*/
protected $timeout;

/**
* @var int|float
*/
protected $connection_timeout;

/**
* @var int
*/
protected $connection_flags;

/**
* @var callable
*/
Expand All @@ -37,10 +47,12 @@ class StreamConnection implements ConnectionInterface {
/**
* @param string $server
* @param int|float|null $timeout
* @param array|null $connection
*/
public function __construct($server, $timeout = null) {
public function __construct($server, $timeout = null, array $connection = null) {
$this->setServer($server);
$this->setTimeout($timeout);
$this->setConnection($connection);
}

/**
Expand All @@ -61,6 +73,23 @@ protected function setTimeout($timeout = null) {
$this->timeout = $timeout ? ceil($timeout * 1000000) : null;
}

/**
* @param null|array $connection
*/
protected function setConnection(array $connection = null) {
if (isset($connection['timeout'])) {
$this->connection_timeout = $connection['timeout'];
} else {
$this->connection_timeout = ini_get('default_socket_timeout');
}

if (isset($connection['flags'])) {
$this->connection_flags = $connection['flags'];
} else {
$this->connection_flags = STREAM_CLIENT_CONNECT;
}
}

/**
*
*/
Expand Down Expand Up @@ -92,7 +121,16 @@ protected function getResource() {
if (!$this->resource) {
$errno = null;
$errstr = null;
if (!$this->resource = stream_socket_client($this->server, $errno, $errstr)) {

$this->resource = stream_socket_client(
$this->server,
$errno,
$errstr,
$this->connection_timeout,
$this->connection_flags
);

if (!$this->resource) {
throw new ConnectionException('Unable to connect to '. $this->server . ' ('. $errstr .')');
}
if (isset($this->timeout)) {
Expand Down
1 change: 1 addition & 0 deletions src/RedisClient/Protocol/ProtocolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static function createRedisProtocol(AbstractRedisClient $RedisClient, $co
ConnectionFactory::createStreamConnection(
$config[AbstractRedisClient::CONFIG_SERVER],
$config[AbstractRedisClient::CONFIG_TIMEOUT],
$config[AbstractRedisClient::CONFIG_CONNECTION],
$onConnect
)
);
Expand Down

0 comments on commit a1b33c0

Please sign in to comment.