Skip to content

Commit

Permalink
Rewrite Redis layer.
Browse files Browse the repository at this point in the history
This rewrite the Redis layer to function more like database or cache
drivers… a new Redis factory is introduced to retrieve Redis connection
instances. Different Redis connection instances are introduced to
account for differences between the two drivers (Predis and PhpRedis).
  • Loading branch information
taylorotwell committed Dec 21, 2016
1 parent 0fd8b63 commit 1ef8b9c
Show file tree
Hide file tree
Showing 26 changed files with 526 additions and 572 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Contracts\Redis\Database as RedisDatabase;
use Illuminate\Contracts\Redis\Factory as Redis;
use Symfony\Component\HttpKernel\Exception\HttpException;

class RedisBroadcaster extends Broadcaster
{
/**
* The Redis instance.
*
* @var \Illuminate\Contracts\Redis\Database
* @var \Illuminate\Contracts\Redis\Factory
*/
protected $redis;

Expand All @@ -26,11 +26,11 @@ class RedisBroadcaster extends Broadcaster
/**
* Create a new broadcaster instance.
*
* @param \Illuminate\Contracts\Redis\Database $redis
* @param \Illuminate\Contracts\Redis\Factory $redis
* @param string $connection
* @return void
*/
public function __construct(RedisDatabase $redis, $connection = null)
public function __construct(Redis $redis, $connection = null)
{
$this->redis = $redis;
$this->connection = $connection;
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Cache/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Illuminate\Cache;

use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Redis\Database;
use Illuminate\Contracts\Redis\Factory as Redis;

class RedisStore extends TaggableStore implements Store
{
/**
* The Redis database connection.
* The Redis factory implementation.
*
* @var \Illuminate\Contracts\Redis\Database
* @var \Illuminate\Contracts\Redis\Factory
*/
protected $redis;

Expand All @@ -31,12 +31,12 @@ class RedisStore extends TaggableStore implements Store
/**
* Create a new Redis store.
*
* @param \Illuminate\Contracts\Redis\Database $redis
* @param \Illuminate\Contracts\Redis\Factory $redis
* @param string $prefix
* @param string $connection
* @return void
*/
public function __construct(Database $redis, $prefix = '', $connection = 'default')
public function __construct(Redis $redis, $prefix = '', $connection = 'default')
{
$this->redis = $redis;
$this->setPrefix($prefix);
Expand Down
15 changes: 0 additions & 15 deletions src/Illuminate/Contracts/Redis/Database.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/Illuminate/Contracts/Redis/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Illuminate\Contracts\Redis;

interface Factory
{
/**
* Get a Redis connection by name.
*
* @param string $name
* @return \Illuminate\Redis\Connections\Connection
*/
public function connection($name = null);
}
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ public function registerCoreContainerAliases()
'queue.connection' => ['Illuminate\Contracts\Queue\Queue'],
'queue.failer' => ['Illuminate\Queue\Failed\FailedJobProviderInterface'],
'redirect' => ['Illuminate\Routing\Redirector'],
'redis' => ['Illuminate\Redis\Database', 'Illuminate\Contracts\Redis\Database'],
'redis' => ['Illuminate\Redis\RedisManager', 'Illuminate\Contracts\Redis\Factory'],
'request' => ['Illuminate\Http\Request', 'Symfony\Component\HttpFoundation\Request'],
'router' => ['Illuminate\Routing\Router', 'Illuminate\Contracts\Routing\Registrar', 'Illuminate\Contracts\Routing\BindingRegistrar'],
'session' => ['Illuminate\Session\SessionManager'],
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Queue/Connectors/RedisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Illuminate\Support\Arr;
use Illuminate\Queue\RedisQueue;
use Illuminate\Contracts\Redis\Database;
use Illuminate\Contracts\Redis\Factory as Redis;

class RedisConnector implements ConnectorInterface
{
/**
* The Redis database instance.
*
* @var \Illuminate\Contracts\Redis\Database
* @var \Illuminate\Contracts\Redis\Factory
*/
protected $redis;

Expand All @@ -25,11 +25,11 @@ class RedisConnector implements ConnectorInterface
/**
* Create a new Redis queue connector instance.
*
* @param \Illuminate\Contracts\Redis\Database $redis
* @param \Illuminate\Contracts\Redis\Factory $redis
* @param string|null $connection
* @return void
*/
public function __construct(Database $redis, $connection = null)
public function __construct(Redis $redis, $connection = null)
{
$this->redis = $redis;
$this->connection = $connection;
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/Jobs/RedisJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public function getJobId()
}

/**
* Get the underlying queue driver instance.
* Get the underlying Redis factory implementation.
*
* @return \Illuminate\Contracts\Redis\Database
* @return \Illuminate\Contracts\Redis\Factory
*/
public function getRedisQueue()
{
Expand Down
11 changes: 5 additions & 6 deletions src/Illuminate/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Queue\Jobs\RedisJob;
use Illuminate\Contracts\Redis\Database;
use Illuminate\Contracts\Redis\Factory as Redis;
use Illuminate\Contracts\Queue\Queue as QueueContract;

class RedisQueue extends Queue implements QueueContract
{
/**
* The Redis database instance.
* The Redis factory implementation.
*
* @var \Illuminate\Contracts\Redis\Database
* @var \Illuminate\Contracts\Redis\Factory
*/
protected $redis;

Expand Down Expand Up @@ -41,14 +41,13 @@ class RedisQueue extends Queue implements QueueContract
/**
* Create a new Redis queue instance.
*
* @param \Illuminate\Contracts\Redis\Database $redis
* @param \Illuminate\Contracts\Redis\Factory $redis
* @param string $default
* @param string $connection
* @param int $expire
* @return void
*/
public function __construct(Database $redis, $default = 'default',
$connection = null, $expire = 60)
public function __construct(Redis $redis, $default = 'default', $connection = null, $expire = 60)
{
$this->redis = $redis;
$this->expire = $expire;
Expand Down
84 changes: 84 additions & 0 deletions src/Illuminate/Redis/Connections/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Illuminate\Redis\Connections;

use Closure;

abstract class Connection
{
/**
* The Predis client.
*
* @var \Predis\Client
*/
protected $client;

/**
* Subscribe to a set of given channels for messages.
*
* @param array|string $channels
* @param \Closure $callback
* @param string $method
* @return void
*/
abstract public function createSubscription($channels, Closure $callback, $method = 'subscribe');

/**
* Get the underlying Redis client.
*
* @return mixed
*/
public function client()
{
return $this->client;
}

/**
* Subscribe to a set of given channels for messages.
*
* @param array|string $channels
* @param \Closure $callback
* @param string $method
* @return void
*/
public function subscribe($channels, Closure $callback)
{
return $this->createSubscription($channels, $callback, __FUNCTION__);
}

/**
* Subscribe to a set of given channels with wildcards.
*
* @param array|string $channels
* @param \Closure $callback
* @return void
*/
public function psubscribe($channels, Closure $callback)
{
return $this->createSubscription($channels, $callback, __FUNCTION__);
}

/**
* Run a command against the Redis database.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function command($method, array $parameters = [])
{
return $this->client->{$method}(...$parameters);
}

/**
* Pass other method calls down to the underlying client.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->command($method, $parameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Redis\Connections;

class PhpRedisClusterConnection extends PhpRedisConnection
{
//
}
73 changes: 73 additions & 0 deletions src/Illuminate/Redis/Connections/PhpRedisConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Illuminate\Redis\Connections;

use Closure;

class PhpRedisConnection extends Connection
{
/**
* Create a new Predis connection.
*
* @param \Predis\Client $client
* @return void
*/
public function __construct($client)
{
$this->client = $client;
}

/**
* Evaluate a Lua script and return the result.
*
* @param string $script
* @param int $numberOfKeys
* @param dynamic $arguments
* @return mixed
*/
public function eval($script, $numberOfKeys, ...$arguments)
{
return $this->client->eval($script, $arguments, $numberOfKeys);
}

/**
* Subscribe to a set of given channels for messages.
*
* @param array|string $channels
* @param \Closure $callback
* @return void
*/
public function subscribe($channels, Closure $callback)
{
$this->client->subscribe((array) $channels, function ($redis, $channel, $message) use ($callback) {
$callback($message, $channel);
});
}

/**
* Subscribe to a set of given channels with wildcards.
*
* @param array|string $channels
* @param \Closure $callback
* @return void
*/
public function psubscribe($channels, Closure $callback)
{
$this->client->psubscribe((array) $channels, function ($redis, $pattern, $channel, $message) use ($callback) {
$callback($message, $channel);
});
}

/**
* Subscribe to a set of given channels for messages.
*
* @param array|string $channels
* @param \Closure $callback
* @param string $method
* @return void
*/
public function createSubscription($channels, Closure $callback, $method = 'subscribe')
{
//
}
}
8 changes: 8 additions & 0 deletions src/Illuminate/Redis/Connections/PredisClusterConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Redis\Connections;

class PredisClusterConnection extends PredisConnection
{
//
}
Loading

0 comments on commit 1ef8b9c

Please sign in to comment.