Skip to content

Commit

Permalink
Merge pull request #12 from cheprasov/RedisStorage
Browse files Browse the repository at this point in the history
v1.2.0 Redis storage
  • Loading branch information
cheprasov authored Jul 27, 2016
2 parents 4f312d3 + fc808b6 commit 5473371
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 82 deletions.
42 changes: 42 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
language: php
php:
- '5.5'
- '5.6'
- '7.0'
- '7.1'
- hhvm

matrix:
allow_failures:
- php: '7.1'
- php: hhvm

before_install:
- PHP_INI=~/.phpenv/versions/$(phpenv version-name)/etc/php.ini;
- |
if [[ $TRAVIS_PHP_VERSION =~ 5.[56] ]]; then
echo "extension = memcached.so" >> $PHP_INI;
echo yes | pecl install apcu-4.0.10;
echo "apc.enabled = 1" >> $PHP_INI;
echo "apc.enable_cli = 1" >> $PHP_INI;
phpenv config-rm xdebug.ini;
fi;
- |
if [[ $TRAVIS_PHP_VERSION =~ 7.[10] ]]; then
apt-get install -y php-memcached;
echo "extension = memcached.so" >> $PHP_INI;
echo yes | pecl install apcu-5.1.2;
echo "apc.enabled = 1" >> $PHP_INI;
echo "apc.enable_cli = 1" >> $PHP_INI;
fi;
- |
if [[ $TRAVIS_PHP_VERSION =~ 7.[0] ]]; then
phpenv config-rm xdebug.ini;
fi;
install:
- composer install

services:
- memcached
- redis
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
# Parallel v1.1.0 for PHP >= 5.5
# Parallel v1.2.0 for PHP >= 5.5

The class allows you to run multiple operations parallel in different processes and send results to the main process. Useful if you need to run multiple independent operations simultaneously, instead of sequential execution, or if you run several independent queries, for example, queries to different data bases.

## Communications

Communications of data between processes is via one (or more) of storages:

- APCu
- Memcached
- Redis

## Using

```php
<?php

require (dirname(__DIR__).'/vendor/autoload.php');

use Parallel\Parallel;
use Parallel\Storage\ApcuStorage;

// EXAMPLE, how to run parallel 3 operations.

// Using Parallel via ApcuStorage (APCu, see http://php.net/manual/ru/book.apcu.php)
$Parallel = new Parallel(new \Parallel\Storage\ApcuStorage());
$Parallel = new Parallel(new ApcuStorage());

// if you have not APCu, you can use Memcached or Redis as Storage.
// Note: you can't store objects in Memcached or Redis and you can't store binary strings (use <base64> functions)

// if you have not APCu, you can use Memcached as Storage.
// Note: you can't store objects in Memcached and you can't store binary strings (use <base64> functions)
// $Parallel = new Parallel(new \Parallel\Storage\MemcachedStorage([
// 'servers' => [['127.0.0.1', 11211]]
// ]));

// $Parallel = new Parallel(new \Parallel\Storage\RedisStorage([
// 'server' => 'tcp://127.0.0.1:6379'
// ]));

$time = microtime(true);

// 1st operation
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cheprasov/php-parallel",
"version": "1.1.0",
"version": "1.2.0",
"description": "The class allows you to run multiple operations parallel in different processes and send results to the main process. Useful if you need to run multiple independent operations simultaneously, instead of sequential execution, or if you run several independent queries, for example, queries to different data bases",
"homepage": "http://github.com/cheprasov/php-parallel",
"minimum-stability": "stable",
Expand All @@ -17,7 +17,8 @@
}
},
"require": {
"php": ">=5.5"
"php": ">=5.5",
"cheprasov/php-redis-client": "^1.4"
},
"require-dev": {
"phpunit/phpunit": "4.8.*"
Expand Down
13 changes: 10 additions & 3 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@
* file that was distributed with this source code.
*/
require (dirname(__DIR__).'/vendor/autoload.php');

use Parallel\Parallel;
use Parallel\Storage\ApcuStorage;

// EXAMPLE, how to run parallel 3 operations.

// Using Parallel via ApcuStorage (APCu, see http://php.net/manual/ru/book.apcu.php)
$Parallel = new Parallel(new \Parallel\Storage\ApcuStorage());
$Parallel = new Parallel(new ApcuStorage());

// if you have not APCu, you can use Memcached or Redis as Storage.
// Note: you can't store objects in Memcached or Redis and you can't store binary strings (use <base64> functions)

// if you have not APCu, you can use Memcached as Storage.
// Note: you can't store objects in Memcached and you can't store binary strings (use <base64> functions)
// $Parallel = new Parallel(new \Parallel\Storage\MemcachedStorage([
// 'servers' => [['127.0.0.1', 11211]]
// ]));

// $Parallel = new Parallel(new \Parallel\Storage\RedisStorage([
// 'server' => 'tcp://127.0.0.1:6379'
// ]));

$time = microtime(true);

// 1st operation
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<php>
<const name="TEST_MEMCACHED_SERVER" value="127.0.0.1:11211" />
<const name="TEST_REDIS_SERVER" value="tcp://127.0.0.1:6379" />
</php>

</phpunit>
2 changes: 1 addition & 1 deletion src/Parallel/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class Parallel {

const VERSION = '1.1.0';
const VERSION = '1.2.0';

/**
* @var StorageInterface
Expand Down
109 changes: 109 additions & 0 deletions src/Parallel/Storage/RedisStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* This file is part of Parallel.
* git: https://github.com/cheprasov/php-parallel
*
* (C) Alexander Cheprasov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Parallel\Storage;

use RedisClient\ClientFactory;
use RedisClient\RedisClient;

class RedisStorage implements StorageInterface {

/**
* @var RedisClient
*/
protected $Redis;

/**
* @var array
*/
protected $options;

/**
* @param array $options
*/
public function __construct(array $options = []) {
$this->options = $options;
}

/**
* @param RedisClient|null $Redis
*/
public function setRedis(RedisClient $Redis = null) {
$this->Redis = $Redis;
}

/**
* @return RedisClient
*/
public function getRedis() {
if (!$this->Redis) {
$this->Redis = ClientFactory::create($this->options);
}
return $this->Redis;
}

/**
* @inheritdoc
*/
public function setup() {
$this->setRedis(null);
}

/**
* @inheritdoc
*/
public function set($key, $field, $value, $expire = 0) {
$serialized = $this->serialize($value);
$result = $this->getRedis()->hset($key, $field, $serialized);
if ($expire) {
$this->getRedis()->expire($key, $expire);
}
return (bool) $result;
}

/**
* @inheritdoc
*/
public function get($key, $fields) {
if (is_string($fields)) {
$data = $this->getRedis()->hget($key, $fields);
return $this->unserialize($data);
}
$result = array_combine($fields, $this->getRedis()->hmget($key, $fields));
foreach ($result as $field => $value) {
$result[$field] = $this->unserialize($value);
}
return $result;
}

/**
* @inheritdoc
*/
public function del($key, $fields) {
return $this->getRedis()->hdel($key, (array) $fields);
}

/**
* @param mixed $data
* @return string
*/
protected function serialize($data) {
return json_encode($data);
}

/**
* @param string $data
* @return mixed
*/
protected function unserialize($data) {
return json_decode($data, true);
}

}
3 changes: 3 additions & 0 deletions tests/Integration/MemcachedStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function getMemcached() {
*/
public function test_set() {
$Memcached = $this->getMemcached();
$Memcached->flush();
$Storage = new MemcachedStorage(['servers' => $this->getTestServers()]);

$this->assertSame(true, $Storage->set('foo', 'bar1', 'hello world'));
Expand All @@ -64,6 +65,7 @@ public function test_set() {
*/
public function test_get() {
$Memcached = $this->getMemcached();
$Memcached->flush();
$Storage = new MemcachedStorage(['servers' => $this->getTestServers()]);

$this->assertSame(true, $Memcached->set('foo1:bar0', ''));
Expand Down Expand Up @@ -111,6 +113,7 @@ public function test_get() {
*/
public function test_del() {
$Memcached = $this->getMemcached();
$Memcached->flush();
$Storage = new MemcachedStorage(['servers' => $this->getTestServers()]);

$this->assertSame(true, $Memcached->set('foo2:bar0', 'foo'));
Expand Down
Loading

0 comments on commit 5473371

Please sign in to comment.