Skip to content

Commit

Permalink
[5.5] phpredis Fixes (#20316)
Browse files Browse the repository at this point in the history
*        more fixes for the phpredis driver

*    fix style
  • Loading branch information
themsaid authored and taylorotwell committed Jul 29, 2017
1 parent 6bb98f7 commit ca19e22
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 6 deletions.
44 changes: 44 additions & 0 deletions src/Illuminate/Redis/Connections/PhpRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Redis\Connections;

use Redis;
use Closure;

/**
Expand Down Expand Up @@ -41,9 +42,38 @@ public function get($key)
*/
public function exists(...$keys)
{
$keys = collect($keys)->map(function ($key) {
return $this->applyPrefix($key);
})->all();

return $this->executeRaw(array_merge(['exists'], $keys));
}

/**
* Set the given key if it doesn't exist.
*
* @param string $key
* @param string $value
* @return int
*/
public function setnx($key, $value)
{
return (int) $this->client->setnx($key, $value);
}

/**
* Set the given hash field if it doesn't exist.
*
* @param string $hash
* @param string $key
* @param string $value
* @return int
*/
public function hsetnx($hash, $key, $value)
{
return (int) $this->client->hsetnx($hash, $key, $value);
}

/**
* Get the values of all the given keys.
*
Expand Down Expand Up @@ -117,6 +147,8 @@ public function zadd($key, ...$dictionary)
}
}

$key = $this->applyPrefix($key);

return $this->executeRaw(array_merge(['zadd', $key], $dictionary));
}

Expand Down Expand Up @@ -350,6 +382,18 @@ public function disconnect()
$this->client->close();
}

/**
* Apply prefix to the given key if necessary.
*
* @param $key
*/
private function applyPrefix($key)
{
$prefix = (string) $this->client->getOption(Redis::OPT_PREFIX);

return $prefix.$key;
}

/**
* Pass other method calls down to the underlying client.
*
Expand Down
66 changes: 60 additions & 6 deletions tests/Redis/RedisConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Redis;

use PHPUnit\Framework\TestCase;
use Illuminate\Redis\RedisManager;

class RedisConnectionTest extends TestCase
{
Expand Down Expand Up @@ -213,6 +214,42 @@ public function it_increments_score_of_sorted_set()
}
}

/**
* @test
*/
public function it_sets_key_if_not_exists()
{
foreach ($this->connections() as $redis) {
$redis->set('name', 'mohamed');

$this->assertSame(0, $redis->setnx('name', 'taylor'));
$this->assertEquals('mohamed', $redis->get('name'));

$this->assertSame(1, $redis->setnx('boss', 'taylor'));
$this->assertEquals('taylor', $redis->get('boss'));

$redis->flushall();
}
}

/**
* @test
*/
public function it_sets_hash_field_if_not_exists()
{
foreach ($this->connections() as $redis) {
$redis->hset('person', 'name', 'mohamed');

$this->assertSame(0, $redis->hsetnx('person', 'name', 'taylor'));
$this->assertEquals('mohamed', $redis->hget('person', 'name'));

$this->assertSame(1, $redis->hsetnx('person', 'boss', 'taylor'));
$this->assertEquals('taylor', $redis->hget('person', 'boss'));

$redis->flushall();
}
}

/**
* @test
*/
Expand Down Expand Up @@ -470,12 +507,9 @@ public function it_gets_multiple_hash_fields()
public function it_runs_eval()
{
foreach ($this->connections() as $redis) {
$redis->eval('redis.call("set", "name", "mohamed")', 0);
$redis->eval('redis.call("set", KEYS[1], ARGV[1])', 1, 'name', 'mohamed');
$this->assertEquals('mohamed', $redis->get('name'));

$redis->eval('redis.call(KEYS[1], KEYS[2], ARGV[1])', 2, 'set', 'name2', 'taylor');
$this->assertEquals('taylor', $redis->get('name2'));

$redis->flushall();
}
}
Expand Down Expand Up @@ -528,7 +562,7 @@ public function it_runs_transactions()
public function it_runs_raw_command()
{
foreach ($this->connections() as $redis) {
$redis->set('test:raw:1', 1);
$redis->executeRaw(['SET', 'test:raw:1', '1']);

$this->assertEquals(
1, $redis->executeRaw(['GET', 'test:raw:1'])
Expand All @@ -540,9 +574,29 @@ public function it_runs_raw_command()

public function connections()
{
return [
$connections = [
$this->redis['predis']->connection(),
$this->redis['phpredis']->connection(),
];

if (extension_loaded('redis')) {
$host = getenv('REDIS_HOST') ?: '127.0.0.1';
$port = getenv('REDIS_PORT') ?: 6379;

$prefixedPhpredis = new RedisManager('phpredis', [
'cluster' => false,
'default' => [
'host' => $host,
'port' => $port,
'database' => 5,
'options' => ['prefix' => 'laravel:'],
'timeout' => 0.5,
],
]);

$connections[] = $prefixedPhpredis->connection();
}

return $connections;
}
}

0 comments on commit ca19e22

Please sign in to comment.