mako\Redis


Description


Redis client based on protocol specification at http://redis.io/topics/protocol.


Class methods


Toggle source

public __construct($name = NULL)


Constructor.


Parameters

Type Description
string (optional) Redis configuration name
Return value

NULL

public function __construct($name = null)
{
	$config = Config::get('redis');

	$name = ($name === null) ? $config['default'] : $name;

	if(isset($config['configurations'][$name]) === false)
	{
		throw new RuntimeException(vsprintf("%s(): '%s' has not been defined in the redis configuration.", array(__METHOD__, $name)));
	}

	$this->config = $config['configurations'][$name];

	$this->connect();
}

Toggle source

public static factory($name = NULL)


Factory method making method chaining possible right off the bat.


Parameters

Type Description
string (optional) Redis configuration name
Return value

mako\Redis

public static function factory($name = null)
{
	return new static($name);
}

Toggle source

public __destruct()


Destructor.

Return value

NULL

public function __destruct()
{
	$this->disconnect();
}

Toggle source

protected connect()


Connects to the Redis server.

Return value

NULL

protected function connect()
{
	$this->connection = @fsockopen('tcp://' . $this->config['host'], $this->config['port'], $errNo, $errStr);

	if(!$this->connection)
	{
		throw new RuntimeException(vsprintf("%s(): %s", array(__METHOD__, $errStr)));
	}

	if(!empty($this->config['password']))
	{
		$this->auth($this->config['password']);
	}

	if(!empty($this->config['database']) && $this->config['database'] !== 0)
	{
		$this->select($this->config['database']);
	}
}

Toggle source

protected disconnect()


Closes connection to the Redis server.

Return value

NULL

protected function disconnect()
{
	if(is_resource($this->connection))
	{
		fclose($this->connection);	
	}
}

Toggle source

protected response()


Returns response from redis server.

Return value

mixed

protected function response()
{
	$response = trim(fgets($this->connection));

	switch(substr($response, 0, 1))
	{
		case '-': // error reply
			throw new RuntimeException(vsprintf("%s(): %s.", array(__METHOD__, substr($response, 5))));
		break;
		case '+': // status reply
			return trim(substr($response, 1));
		break;
		case ':': // integer reply
			return (int) trim(substr($response, 1));
		break;
		case '$': // bulk reply
			if($response === '$-1')
			{
				return null;
			}

			$length = (int) substr($response, 1);

			return substr(fread($this->connection, $length + strlen(static::CRLF)), 0, - strlen(static::CRLF));
		break;
		case '*': // multi-bulk reply
			if($response === '*-1')
			{
				return null;
			}

			$data = array();

			$count = substr($response, 1);

			for($i = 0; $i < $count; $i++)
			{
				$data[] = $this->response();
			}

			return $data;
		break;
		default:
			throw new RuntimeException(vsprintf("%s(): Unable to handle server response.", array(__METHOD__)));
	}
}

Toggle source

public __call($name, $args)


Sends command to Redis server and returns response.


Parameters

Type Description
string Command name
array Command parameters
Return value

mixed

public function __call($name, $args)
{
	// Build command

	array_unshift($args, strtoupper($name));

	$command = '*' . count($args) . static::CRLF;

	foreach($args as $arg)
	{
		$command .= '$' . strlen($arg) . static::CRLF . $arg . static::CRLF;
	}

	// Send command to server

	fwrite($this->connection, $command);

	// Return response

	return $this->response();
}

Toggle source

public static __callStatic($name, $arguments)


Magic shortcut to the default redis configuration.


Parameters

Type Description
string Method name
array Method arguments
Return value

mixed

public static function __callStatic($name, $arguments)
{
	return call_user_func_array(array(static::factory(), $name), $arguments);
}