Skip to content

Commit

Permalink
Added Support for PhpRedis
Browse files Browse the repository at this point in the history
  • Loading branch information
tillkruss committed Aug 30, 2016
1 parent 51a85f1 commit ec34852
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 124 deletions.
14 changes: 8 additions & 6 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 as Redis;
use Illuminate\Contracts\Redis\Database;

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

Expand All @@ -31,12 +31,12 @@ class RedisStore extends TaggableStore implements Store
/**
* Create a new Redis store.
*
* @param \Illuminate\Redis\Database $redis
* @param \Illuminate\Contracts\Redis\Database $redis
* @param string $prefix
* @param string $connection
* @return void
*/
public function __construct(Redis $redis, $prefix = '', $connection = 'default')
public function __construct(Database $redis, $prefix = '', $connection = 'default')
{
$this->redis = $redis;
$this->setPrefix($prefix);
Expand All @@ -51,7 +51,9 @@ public function __construct(Redis $redis, $prefix = '', $connection = 'default')
*/
public function get($key)
{
if (! is_null($value = $this->connection()->get($this->prefix.$key))) {
$value = $this->connection()->get($this->prefix.$key);

if (! is_null($value) && $value !== false) {
return is_numeric($value) ? $value : unserialize($value);
}
}
Expand Down Expand Up @@ -208,7 +210,7 @@ public function setConnection($connection)
/**
* Get the Redis database instance.
*
* @return \Illuminate\Redis\Database
* @return \Illuminate\Contracts\Redis\Database
*/
public function getRedis()
{
Expand Down
20 changes: 14 additions & 6 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ protected function fireCacheEvent($event, $payload)
*/
public function has($key)
{
return ! is_null($this->get($key));
$value = $this->get($key);

return ! is_null($value) && $value !== false;
}

/**
Expand All @@ -128,7 +130,7 @@ public function get($key, $default = null)

$value = $this->store->get($this->itemKey($key));

if (is_null($value)) {
if (is_null($value) || $value === false) {
$this->fireCacheEvent('missed', [$key]);

$value = value($default);
Expand Down Expand Up @@ -158,7 +160,7 @@ public function many(array $keys)
$values = $this->store->many($normalizedKeys);

foreach ($values as $key => &$value) {
if (is_null($value)) {
if (is_null($value) || $value === false) {
$this->fireCacheEvent('missed', [$key]);

$value = isset($keys[$key]) ? value($keys[$key]) : null;
Expand Down Expand Up @@ -249,7 +251,9 @@ public function add($key, $value, $minutes)
return $this->store->add($this->itemKey($key), $value, $minutes);
}

if (is_null($this->get($key))) {
$exists = $this->get($key);

if (is_null($exists) || $exists === false) {
$this->put($key, $value, $minutes);

return true;
Expand Down Expand Up @@ -306,10 +310,12 @@ public function forever($key, $value)
*/
public function remember($key, $minutes, Closure $callback)
{
$value = $this->get($key);

// If the item exists in the cache we will just return this immediately
// otherwise we will execute the given Closure and cache the result
// of that execution for the given number of minutes in storage.
if (! is_null($value = $this->get($key))) {
if (! is_null($value) && $value !== false) {
return $value;
}

Expand Down Expand Up @@ -339,10 +345,12 @@ public function sear($key, Closure $callback)
*/
public function rememberForever($key, Closure $callback)
{
$value = $this->get($key);

// If the item exists in the cache we will just return this immediately
// otherwise we will execute the given Closure and cache the result
// of that execution for the given number of minutes. It's easy.
if (! is_null($value = $this->get($key))) {
if (! is_null($value) && $value !== false) {
return $value;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Queue/Connectors/RedisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Illuminate\Queue\Connectors;

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

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

Expand All @@ -25,7 +25,7 @@ class RedisConnector implements ConnectorInterface
/**
* Create a new Redis queue connector instance.
*
* @param \Illuminate\Redis\Database $redis
* @param \Illuminate\Contracts\Redis\Database $redis
* @param string|null $connection
* @return void
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Jobs/RedisJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getJobId()
/**
* Get the underlying queue driver instance.
*
* @return \Illuminate\Redis\Database
* @return \Illuminate\Contracts\Redis\Database
*/
public function getRedisQueue()
{
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Redis\Database;
use Illuminate\Queue\Jobs\RedisJob;
use Illuminate\Contracts\Redis\Database;
use Illuminate\Contracts\Queue\Queue as QueueContract;

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

Expand Down Expand Up @@ -41,7 +41,7 @@ class RedisQueue extends Queue implements QueueContract
/**
* Create a new Redis queue instance.
*
* @param \Illuminate\Redis\Database $redis
* @param \Illuminate\Contracts\Redis\Database $redis
* @param string $default
* @param string $connection
* @param int $expire
Expand Down Expand Up @@ -239,7 +239,7 @@ protected function getConnection()
/**
* Get the underlying Redis instance.
*
* @return \Illuminate\Redis\Database
* @return \Illuminate\Contracts\Redis\Database
*/
public function getRedis()
{
Expand Down
97 changes: 1 addition & 96 deletions src/Illuminate/Redis/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,11 @@

namespace Illuminate\Redis;

use Closure;
use Predis\Client;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Redis\Database as DatabaseContract;

class Database implements DatabaseContract
abstract class Database implements DatabaseContract
{
/**
* The host address of the database.
*
* @var array
*/
protected $clients;

/**
* Create a new Redis connection instance.
*
* @param array $servers
* @return void
*/
public function __construct(array $servers = [])
{
$cluster = Arr::pull($servers, 'cluster');

$options = array_merge(['timeout' => 10.0], (array) Arr::pull($servers, 'options'));

if ($cluster) {
$this->clients = $this->createAggregateClient($servers, $options);
} else {
$this->clients = $this->createSingleClients($servers, $options);
}
}

/**
* Create a new aggregate client supporting sharding.
*
* @param array $servers
* @param array $options
* @return array
*/
protected function createAggregateClient(array $servers, array $options = [])
{
return ['default' => new Client(array_values($servers), $options)];
}

/**
* Create an array of single connection clients.
*
* @param array $servers
* @param array $options
* @return array
*/
protected function createSingleClients(array $servers, array $options = [])
{
$clients = [];

foreach ($servers as $key => $server) {
$clients[$key] = new Client($server, $options);
}

return $clients;
}

/**
* Get a specific Redis connection instance.
*
Expand All @@ -88,43 +30,6 @@ public function command($method, array $parameters = [])
return call_user_func_array([$this->clients['default'], $method], $parameters);
}

/**
* Subscribe to a set of given channels for messages.
*
* @param array|string $channels
* @param \Closure $callback
* @param string $connection
* @param string $method
* @return void
*/
public function subscribe($channels, Closure $callback, $connection = null, $method = 'subscribe')
{
$loop = $this->connection($connection)->pubSubLoop();

call_user_func_array([$loop, $method], (array) $channels);

foreach ($loop as $message) {
if ($message->kind === 'message' || $message->kind === 'pmessage') {
call_user_func($callback, $message->payload, $message->channel);
}
}

unset($loop);
}

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

/**
* Dynamically make a Redis command.
*
Expand Down
Loading

0 comments on commit ec34852

Please sign in to comment.