-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for the phpredis extension
Along with the new addition, the code coverage is now up to 88%!
- Loading branch information
1 parent
3b6a5c9
commit 5f3c12e
Showing
4 changed files
with
146 additions
and
17 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
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Presque package. | ||
* | ||
* (c) Justin Rainbow <justin.rainbow@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Presque\Storage; | ||
|
||
use Redis; | ||
|
||
class PhpredisStorage implements StorageInterface | ||
{ | ||
private $prefix; | ||
private $connection; | ||
|
||
public function __construct(Redis $connection, $prefix = null) | ||
{ | ||
$this->prefix = $prefix; | ||
$this->connection = $connection; | ||
} | ||
|
||
public function setPrefix($prefix = null) | ||
{ | ||
$this->prefix = $prefix; | ||
|
||
$this->connection->setOption(Redis::OPT_PREFIX, null === $prefix ? '' : rtrim($prefix, ':') . ':'); | ||
} | ||
|
||
public function getPrefix() | ||
{ | ||
return $this->prefix; | ||
} | ||
|
||
public function push($listName, $payload) | ||
{ | ||
$this->connection->lPush($listName, json_encode($payload)); | ||
} | ||
|
||
public function pop($listName, $waitTimeout = null) | ||
{ | ||
$payload = $this->connection->blPop($listName, $waitTimeout); | ||
|
||
if (is_array($payload) && isset($payload[1])) { | ||
return json_decode($payload[1], true); | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Presque package. | ||
* | ||
* (c) Justin Rainbow <justin.rainbow@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Presque\Tests\Storage; | ||
|
||
use Presque\Tests\TestCase; | ||
use Presque\Storage\PhpredisStorage; | ||
use Mockery as m; | ||
|
||
class PhpredisStorageTest extends TestCase | ||
{ | ||
protected $redis; | ||
protected $storage; | ||
|
||
public function testPushingDataOntoList() | ||
{ | ||
$payload = array( | ||
'class' => 'test', | ||
'args' => array() | ||
); | ||
|
||
$this->redis | ||
->expects($this->once()) | ||
->method('lPush') | ||
->with($this->equalTo('list'), $this->equalTo(json_encode($payload))); | ||
|
||
$this->storage->push('list', $payload); | ||
} | ||
|
||
public function testPopingDataFromList() | ||
{ | ||
$payload = array( | ||
'class' => 'test', | ||
'args' => array() | ||
); | ||
|
||
$this->redis | ||
->expects($this->exactly(3)) | ||
->method('blPop') | ||
->with($this->equalTo('list'), $this->equalTo(5)) | ||
->will($this->onConsecutiveCalls( | ||
array('list', json_encode($payload)), | ||
array(), | ||
false | ||
)); | ||
|
||
$result = $this->storage->pop('list', 5); | ||
$this->assertTrue(is_array($result)); | ||
$this->assertArrayHasKey('class', $result); | ||
$this->assertArrayHasKey('args', $result); | ||
$this->assertEquals('test', $result['class']); | ||
|
||
$this->assertFalse($this->storage->pop('list', 5)); | ||
$this->assertFalse($this->storage->pop('list', 5)); | ||
} | ||
|
||
public function testPrefixingRedisData() | ||
{ | ||
$this->assertEquals(null, $this->storage->getPrefix()); | ||
|
||
$this->redis | ||
->expects($this->once()) | ||
->method('setOption') | ||
->with($this->equalTo(\Redis::OPT_PREFIX), $this->equalTo('presque:')); | ||
|
||
$this->storage->setPrefix('presque'); | ||
} | ||
|
||
protected function setUp() | ||
{ | ||
if (!extension_loaded('redis')) { | ||
$this->markTestSkipped('The phpredis extension is not loaded.'); | ||
} else { | ||
$this->redis = $this->createRedisMock(); | ||
$this->storage = new PhpredisStorage($this->redis); | ||
} | ||
} | ||
|
||
protected function createRedisMock() | ||
{ | ||
return $this->getMock('Redis'); | ||
} | ||
} |