-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from cheprasov/RedisStorage
v1.2.0 Redis storage
- Loading branch information
Showing
11 changed files
with
391 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
|
||
class Parallel { | ||
|
||
const VERSION = '1.1.0'; | ||
const VERSION = '1.2.0'; | ||
|
||
/** | ||
* @var StorageInterface | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <cheprasov.84@ya.ru> | ||
* | ||
* 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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.