Commit bd669584 authored by Taylor Otwell's avatar Taylor Otwell

refactoring.

parent a6d487ec
...@@ -2,13 +2,6 @@ ...@@ -2,13 +2,6 @@
class Redis { class Redis {
/**
* The active Redis connections.
*
* @var array
*/
protected static $connections = array();
/** /**
* The name of the Redis connection. * The name of the Redis connection.
* *
...@@ -28,7 +21,7 @@ class Redis { ...@@ -28,7 +21,7 @@ class Redis {
* *
* @var resource * @var resource
*/ */
protected $connection; protected static $connection;
/** /**
* Create a new Redis connection instance. * Create a new Redis connection instance.
...@@ -58,28 +51,21 @@ class Redis { ...@@ -58,28 +51,21 @@ class Redis {
/** /**
* Create a new Redis connection instance. * Create a new Redis connection instance.
* *
* Redis connections are managed as singletons, so if the connection has * The Redis connection is managed as a singleton, so if the connection has
* already been established, that same connection instance will be returned * already been established, that same connection instance will be returned
* on subsequent requests for the connection. * on subsequent requests for the connection.
* *
* @param string $connection * @param string $connection
* @return Redis * @return Redis
*/ */
public static function connection($name) public static function connection()
{ {
if ( ! array_key_exists($name, static::$connections)) if (is_null(static::$connection))
{ {
$config = Config::get("database.redis.{$name}"); static::$connection = static::make($name, Config::get('database.redis'))->connect();
if (is_null($config))
{
throw new \Exception("Redis connection [$name] has not been configured.");
}
static::$connections[$name] = static::make($name, $config)->connect();
} }
return static::$connections[$name]; return static::$connection;
} }
/** /**
...@@ -91,9 +77,9 @@ class Redis { ...@@ -91,9 +77,9 @@ class Redis {
*/ */
public function connect() public function connect()
{ {
$this->connection = @fsockopen($this->config['host'], $this->config['port'], $error, $message); static::$connection = @fsockopen($this->config['host'], $this->config['port'], $error, $message);
if ($this->connection === false) if (static::$connection === false)
{ {
throw new \Exception("Error establishing Redis connection [{$this->name}]: {$error} - {$message}"); throw new \Exception("Error establishing Redis connection [{$this->name}]: {$error} - {$message}");
} }
...@@ -110,9 +96,9 @@ class Redis { ...@@ -110,9 +96,9 @@ class Redis {
*/ */
public function run($method, $parameters) public function run($method, $parameters)
{ {
fwrite($this->connection, $this->command($method, $parameters)); fwrite(static::$connection, $this->command($method, $parameters));
$reply = trim(fgets($this->connection, 512)); $reply = trim(fgets(static::$connection, 512));
} }
/** /**
...@@ -149,7 +135,7 @@ class Redis { ...@@ -149,7 +135,7 @@ class Redis {
*/ */
public function __destruct() public function __destruct()
{ {
fclose($this->connection); fclose(static::$connection);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment