Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add phpredis #29

Merged
merged 1 commit into from
May 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.php.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
extension=memcache.so
extension=memcached.so
extension=redis.so
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"predis/predis": "1.0.*",
"ext-memcache": "*",
"ext-memcached": "*",
"ext-pdo_mysql": "*"
"ext-pdo_mysql": "*",
"ext-redis": "*"
},
"suggest" : {
"predis/predis" : "Create mutex using Predis (client library for Redis)",
Expand Down
79 changes: 79 additions & 0 deletions src/NinjaMutex/Lock/PhpRedisLock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* This file is part of ninja-mutex.
*
* (C) leo108 <root@leo108.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NinjaMutex\Lock;

use \Redis;

/**
* Lock implementor using PHPRedis
*
* @author leo108 <root@leo108.com>
*/
class PhpRedisLock extends LockAbstract
{
/**
* phpredis connection
*
* @var
*/
protected $client;

/**
* @param $client Redis
*/
public function __construct(Redis $client)
{
parent::__construct();

$this->client = $client;
}

/**
* @param string $name
* @param bool $blocking
* @return bool
*/
protected function getLock($name, $blocking)
{
if (!$this->client->setnx($name, serialize($this->getLockInformation()))) {
return false;
}

return true;
}

/**
* Release lock
*
* @param string $name name of lock
* @return bool
*/
public function releaseLock($name)
{
if (isset($this->locks[$name]) && $this->client->del($name)) {
unset($this->locks[$name]);

return true;
}

return false;
}

/**
* Check if lock is locked
*
* @param string $name name of lock
* @return bool
*/
public function isLocked($name)
{
return false !== $this->client->get($name);
}
}
26 changes: 26 additions & 0 deletions tests/NinjaMutex/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
use NinjaMutex\Mock\MockMemcache;
use NinjaMutex\Mock\MockMemcached;
use NinjaMutex\Mock\MockPredisClient;
use NinjaMutex\Mock\MockPhpRedisClient;
use NinjaMutex\Lock\PredisRedisLock;
use NinjaMutex\Lock\PhpRedisLock;
use Predis;
use \Redis;
use org\bovigo\vfs;

abstract class AbstractTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -63,12 +66,14 @@ public function lockImplementorProvider()
$this->provideMemcachedMockLock(),
$this->provideMysqlMockLock(),
$this->providePredisRedisMockLock(),
$this->providePhpRedisMockLock(),
// Real locks
$this->provideFlockLock(),
array($memcacheLockFabric->create()),
array($memcachedLockFabric->create()),
$this->provideMysqlLock(),
$this->providePredisRedisLock(),
$this->providePhpRedisLock(),
);

return $data;
Expand All @@ -84,6 +89,7 @@ public function lockImplementorWithBackendProvider()
$this->provideMemcacheMockLock(),
$this->provideMemcachedMockLock(),
$this->providePredisRedisMockLock(),
$this->providePhpRedisMockLock(),
);

return $data;
Expand Down Expand Up @@ -151,6 +157,16 @@ protected function providePredisRedisMockLock()
return array(new PredisRedisLock($predisMock), $predisMock);
}

/**
* @return array
*/
protected function providePhpRedisMockLock()
{
$predisMock = new MockPhpRedisClient();

return array(new PhpRedisLock($predisMock), $predisMock);
}

/**
* @return array
*/
Expand All @@ -174,4 +190,14 @@ protected function providePredisRedisLock()
{
return array(new PredisRedisLock(new Predis\Client()));
}

/**
* @return array
*/
protected function providePhpRedisLock()
{
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
return array(new PhpRedisLock($redis));
}
}
95 changes: 95 additions & 0 deletions tests/NinjaMutex/Mock/MockPhpRedisClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* This file is part of ninja-mutex.
*
* (C) leo108 <root@leo108.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NinjaMutex\Mock;

use \Redis;

/**
* Mock \Redis to mimic PhpRedis functionality
*
* @author leo108 <root@leo108.com>
*/
class MockPhpRedisClient extends Redis implements PermanentServiceInterface
{
/**
* @var string[]
*/
protected static $data = array();

/**
* Whether the service is available
* @var boolean
*/
protected $available = true;

public function __construct()
{
}

/**
* @param string $key
* @param mixed $value
* @return bool
*/
public function setnx($key, $value)
{
if (!$this->available) {
return false;
}

if (false === $this->get($key)) {
self::$data[$key] = (string) $value;

return true;
}

return false;
}

/**
* @param string $key
* @return mixed
*/
public function get($key)
{
if (!$this->available) {
return false;
}

if (!isset(self::$data[$key])) {
return false;
}

return (string) self::$data[$key];
}

/**
* @param string $key
* @return bool
*/
public function del($key)
{
if (!$this->available) {
return false;
}

unset(self::$data[$key]);

return true;
}

/**
* @param bool $available
*/
public function setAvailable($available)
{
$this->available = (bool) $available;
}
}