Files
spa-api/laravel/redis.php

141 lines
2.7 KiB
PHP
Raw Normal View History

2011-10-19 22:44:02 -05:00
<?php namespace Laravel;
class Redis {
/**
* The name of the Redis connection.
*
* @var string
*/
public $name;
/**
* The configuration array for the Redis connection.
*
* @var array
*/
public $config = array();
/**
* The connection to the Redis database.
*
* @var resource
*/
2011-10-24 23:24:06 -05:00
protected static $connection;
2011-10-19 22:44:02 -05:00
/**
* Create a new Redis connection instance.
*
* @param string $name
* @param array $config
* @return void
*/
public function __construct($name, $config)
{
$this->name = $name;
$this->config = $config;
}
/**
* Create a new Redis connection instance.
*
* @param string $connection
* @param array $config
* @return Redis
*/
public static function make($name, $config)
{
return new static($name, $config);
}
/**
* Create a new Redis connection instance.
*
2011-10-24 23:24:06 -05:00
* The Redis connection is managed as a singleton, so if the connection has
2011-10-19 22:44:02 -05:00
* already been established, that same connection instance will be returned
* on subsequent requests for the connection.
*
* @param string $connection
* @return Redis
*/
2011-10-24 23:24:06 -05:00
public static function connection()
2011-10-19 22:44:02 -05:00
{
2011-10-24 23:24:06 -05:00
if (is_null(static::$connection))
2011-10-19 22:44:02 -05:00
{
2011-10-24 23:24:06 -05:00
static::$connection = static::make($name, Config::get('database.redis'))->connect();
2011-10-19 22:44:02 -05:00
}
2011-10-24 23:24:06 -05:00
return static::$connection;
2011-10-19 22:44:02 -05:00
}
/**
* Connect to the Redis database.
*
* The Redis instance itself will be returned by the method.
*
* @return Redis
*/
public function connect()
{
2011-10-24 23:24:06 -05:00
static::$connection = @fsockopen($this->config['host'], $this->config['port'], $error, $message);
2011-10-19 22:44:02 -05:00
2011-10-24 23:24:06 -05:00
if (static::$connection === false)
2011-10-19 22:44:02 -05:00
{
throw new \Exception("Error establishing Redis connection [{$this->name}]: {$error} - {$message}");
}
return $this;
}
/**
* Execute a command agaisnt the Redis database.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
2011-10-24 23:15:29 -05:00
public function run($method, $parameters)
2011-10-19 22:44:02 -05:00
{
2011-10-24 23:24:06 -05:00
fwrite(static::$connection, $this->command($method, $parameters));
2011-10-19 22:44:02 -05:00
2011-10-24 23:24:06 -05:00
$reply = trim(fgets(static::$connection, 512));
2011-10-19 22:44:02 -05:00
}
/**
* Build the Redis command based from a given method and parameters.
*
* @param string $method
* @param array $parameters
* @return string
*/
protected function command($method, $parameters)
{
$command = '*'.(count($parameters) + 1).CRLF.'$'.strlen($method).CRLF.strtoupper($method).CRLF;
foreach ($parameters as $parameter)
{
$command .= '$'.strlen($parameter).CRLF.$parameter.CRLF;
}
return $command;
}
/**
* Dynamically make calls to the Redis database.
*/
public function __call($method, $parameters)
{
2011-10-24 23:15:29 -05:00
return $this->run($method, $parameters);
2011-10-19 22:44:02 -05:00
}
/**
* Close the connection to the Redis database.
*
* @return void
*/
public function __destruct()
{
2011-10-24 23:24:06 -05:00
fclose(static::$connection);
2011-10-19 22:44:02 -05:00
}
}