Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Implement redis driver #65

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ php:
- 7.0
- hhvm

services:
- redis-server

sudo: false

cache:
Expand Down
135 changes: 135 additions & 0 deletions Drivers/RedisDriver.php
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']);
}
}
5 changes: 5 additions & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ The ttl (time to life) option is optional everywhere, it is used to indicate the
class: Lexik\Bundle\MaintenanceBundle\Drivers\MemCacheDriver # class for MemCache driver
options: {key_name: 'maintenance', host: 127.0.0.1, port: 11211} # need to define a key_name, the host and port

# Redis driver
class: Lexik\Bundle\MaintenanceBundle\Drivers\RedisDriver
# Need to define a key_name and connection_parameters. Any valid redis connection uri, see https://github.com/nrk/predis#connecting-to-redis
options: { key_name: 'maintenance', connection_parameters: 'tcp://127.0.0.1:6379' }

# Database driver:
class: 'Lexik\Bundle\MaintenanceBundle\Drivers\DatabaseDriver' # class for database driver

Expand Down
74 changes: 74 additions & 0 deletions Tests/Maintenance/RedisDriverTest.php
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));
}
}
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
],
"require": {
"php": ">=5.3.2",
"symfony/framework-bundle": "~2.7|~3.0"
"symfony/framework-bundle": "~2.7|~3.0",
"symfony/translation": "~2.7|~3.0",
"predis/predis": "^1.1"
},
"require-dev": {
"symfony/phpunit-bridge": "~2.7|~3.0"
"symfony/phpunit-bridge": "~2.7|~3.0",
"phpunit/phpunit": "~4.8"
},
"autoload": {
"psr-4": { "Lexik\\Bundle\\MaintenanceBundle\\": "" }
Expand Down