This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Implement redis driver #65
Open
Bruce17
wants to merge
8
commits into
lexik:master
Choose a base branch
from
Bruce17:feature/redis-driver
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8d2e803
update .gitignore
Bruce17 ab523ae
add missing package "symfony/translation"
Bruce17 1609686
add packages "predis" and "phpunit" (dev)
Bruce17 a96e58b
implement RedisDriver
Bruce17 036bf7d
update doc
Bruce17 2440280
update doc
Bruce17 68299fb
add missing redis connect
Bruce17 f09790f
Update .gitignore
Bruce17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -5,3 +5,6 @@ phpunit.xml | |
.settings | ||
vendor | ||
composer.lock | ||
|
||
# IDE | ||
.idea |
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 |
---|---|---|
|
@@ -8,6 +8,9 @@ php: | |
- 7.0 | ||
- hhvm | ||
|
||
services: | ||
- redis-server | ||
|
||
sudo: false | ||
|
||
cache: | ||
|
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,135 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\MaintenanceBundle\Drivers; | ||
|
||
/** | ||
* Class RedisDriver | ||
* | ||
* @package Lexik\Bundle\MaintenanceBundle\Drivers | ||
*/ | ||
class RedisDriver extends AbstractDriver implements DriverTtlInterface | ||
{ | ||
/** | ||
* The key to store in redis. | ||
* | ||
* @var string | ||
*/ | ||
protected $keyName; | ||
|
||
/** | ||
* @var \Predis\Client | ||
*/ | ||
protected $redisInstance; | ||
|
||
|
||
/** | ||
* RedisDriver constructor. | ||
* | ||
* @param array $options | ||
*/ | ||
public function __construct(array $options = array()) | ||
{ | ||
parent::__construct($options); | ||
|
||
if (!isset($options['key_name'])) { | ||
throw new \InvalidArgumentException('$options[\'key_name\'] must be defined if Driver Redis configuration is used'); | ||
} | ||
|
||
if (!isset($options['connection_parameters'])) { | ||
throw new \InvalidArgumentException('$options[\'connection_parameters\'] must be defined if Driver Redis configuration is used'); | ||
} | ||
|
||
if (null !== $options) { | ||
if (!isset($options['ttl']) || !is_numeric($options['ttl'])) { | ||
$options['ttl'] = 0; | ||
} else { | ||
$options['ttl'] = intval($options['ttl']); | ||
} | ||
|
||
$this->keyName = $options['key_name']; | ||
|
||
$this->redisInstance = new \Predis\Client($options['connection_parameters']); | ||
$this->redisInstance->connect(); | ||
} | ||
|
||
$this->options = $options; | ||
} | ||
|
||
function __destruct() | ||
{ | ||
if (isset($this->redisInstance)) { | ||
$this->redisInstance->disconnect(); | ||
} | ||
|
||
unset($this->redisInstance); | ||
} | ||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createLock() | ||
{ | ||
return ($this->redisInstance->setex($this->keyName, $this->options['ttl'], true) === 'OK'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createUnlock() | ||
{ | ||
return $this->redisInstance->del(array($this->keyName)) > 0; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isExists() | ||
{ | ||
return $this->redisInstance->exists($this->keyName); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMessageLock($resultTest) | ||
{ | ||
$key = $resultTest ? 'lexik_maintenance.success_lock_memc' : 'lexik_maintenance.not_success_lock'; | ||
|
||
return $this->translator->trans($key, array(), 'maintenance'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMessageUnlock($resultTest) | ||
{ | ||
$key = $resultTest ? 'lexik_maintenance.success_unlock' : 'lexik_maintenance.not_success_unlock'; | ||
|
||
return $this->translator->trans($key, array(), 'maintenance'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setTtl($value) | ||
{ | ||
$this->options['ttl'] = $value; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTtl() | ||
{ | ||
return $this->options['ttl']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasTtl() | ||
{ | ||
return isset($this->options['ttl']); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\MaintenanceBundle\Tests\Maintenance; | ||
|
||
use Lexik\Bundle\MaintenanceBundle\Drivers\RedisDriver; | ||
|
||
/** | ||
* Class RedisDriverTest | ||
* | ||
* @package Lexik\Bundle\MaintenanceBundle\Tests\Maintenance | ||
*/ | ||
class RedisDriverTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @expectedException InvalidArgumentException | ||
*/ | ||
public function testConstructWithoutKeyName() | ||
{ | ||
$redis = new RedisDriver(array()); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
*/ | ||
public function testConstructWithoutConnectionParameters() | ||
{ | ||
$redis = new RedisDriver(array('key_name' => 'foo')); | ||
} | ||
|
||
public function testTtlIsSetWithDefaultValue() | ||
{ | ||
$redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost')); | ||
|
||
$this->assertTrue($redis->hasTtl()); | ||
$this->assertEquals(0, $redis->getTtl()); | ||
} | ||
|
||
public function testTtlIsSetWithCustomValue() | ||
{ | ||
$redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost', 'ttl' => 1234)); | ||
|
||
$this->assertTrue($redis->hasTtl()); | ||
$this->assertEquals(1234, $redis->getTtl()); | ||
} | ||
|
||
public function testTtlIsSetWithCustomValueAsString() | ||
{ | ||
$redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost', 'ttl' => '1234')); | ||
|
||
$this->assertTrue($redis->hasTtl()); | ||
$this->assertEquals(1234, $redis->getTtl()); | ||
} | ||
|
||
public function testKeyName() | ||
{ | ||
$redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost')); | ||
|
||
$property = new \ReflectionProperty($redis, 'keyName'); | ||
$property->setAccessible(true); | ||
|
||
$this->assertEquals('foo', $property->getValue($redis)); | ||
} | ||
|
||
public function testRedisInstance() | ||
{ | ||
$redis = new RedisDriver(array('key_name' => 'foo', 'connection_parameters' => 'localhost')); | ||
|
||
$property = new \ReflectionProperty($redis, 'redisInstance'); | ||
$property->setAccessible(true); | ||
|
||
$this->assertNotEmpty($property->getValue($redis)); | ||
$this->assertInstanceOf('\Predis\Client', $property->getValue($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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this lines. Add instead on your global git