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

[5.4] Make PhpRedis connection more compatible with Predis (pipeline, ...) #18421

Merged
merged 3 commits into from
Mar 19, 2017
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:
before_install:
- if [[ $TRAVIS_PHP_VERSION != 7.1 ]] ; then phpenv config-rm xdebug.ini; fi
- echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- pecl install -f redis
Copy link
Contributor

@lucasmichot lucasmichot Mar 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you reinstall Redis extension?
It is already installed: https://docs.travis-ci.com/user/languages/php#PHP-7.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucasmichot you're right, but the problem is the installed version. for PHP 7 and 7.1, it's ok, but for PHP 5.6, it has an old version of phpredis extension (< 2.2.7).

- travis_retry composer self-update

install:
Expand Down
41 changes: 41 additions & 0 deletions src/Illuminate/Redis/Connections/PhpRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,47 @@ public function zadd($key, ...$dictionary)
return $this->client->zadd($key, ...$dictionary);
}

/**
* Execute commands in a pipeline.
*
* @param callable $callback
* @return array|\Redis
*/
public function pipeline(callable $callback = null)
{
$pipeline = $this->client()->pipeline();

return is_null($callback)
? $pipeline
: tap($pipeline, $callback)->exec();
}

/**
* Execute commands in a transaction.
*
* @param callable $callback
* @return array|\Redis
*/
public function transaction(callable $callback = null)
{
$transaction = $this->client()->multi();

return is_null($callback)
? $transaction
: tap($transaction, $callback)->exec();
}

/**
* Execute a raw command.
*
* @param array $parameters
* @return mixed
*/
public function executeRaw(array $parameters)
{
return $this->command('rawCommand', $parameters);
}

/**
* Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself.
*
Expand Down
64 changes: 64 additions & 0 deletions tests/Redis/Connections/PhpRedisConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Illuminate\Tests\Redis\Connections;

use PHPUnit\Framework\TestCase;
use Illuminate\Tests\Redis\InteractsWithRedis;

class PhpRedisConnectionTest extends TestCase
{
use InteractsWithRedis;

public function setUp()
{
parent::setUp();
$this->setUpRedis();

if (! isset($this->redis['phpredis'])) {
$this->markTestSkipped('PhpRedis should be enabled to run the tests');
}
}

public function tearDown()
{
parent::tearDown();
$this->tearDownRedis();
}

public function testPhpRedisPipeline()
{
$result = $this->redis['phpredis']->connection()->pipeline(function ($pipe) {
$pipe->set('test:pipeline:1', 1);
$pipe->get('test:pipeline:1');
$pipe->set('test:pipeline:2', 2);
$pipe->get('test:pipeline:2');
});

$this->assertCount(4, $result);
$this->assertEquals(1, $result[1]);
$this->assertEquals(2, $result[3]);
}

public function testPhpRedisTransaction()
{
$result = $this->redis['phpredis']->connection()->transaction(function ($pipe) {
$pipe->set('test:transaction:1', 1);
$pipe->get('test:transaction:1');
$pipe->set('test:transaction:2', 2);
$pipe->get('test:transaction:2');
});

$this->assertCount(4, $result);
$this->assertEquals(1, $result[1]);
$this->assertEquals(2, $result[3]);
}

public function testPhpRedisExecuteRaw()
{
$this->redis['phpredis']->connection()->set('test:raw:1', 1);

$this->assertEquals(
1, $this->redis['phpredis']->connection()->executeRaw(['GET', 'test:raw:1'])
);
}
}