Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iolloyd committed Sep 7, 2014
1 parent 828c642 commit bc3799f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 37 deletions.
93 changes: 58 additions & 35 deletions redis.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
<?php

define('NL', sprintf('%s%s', chr(13), chr(10)));
class Redis {
private $socket;
public $host;
public $port;

public function __construct($host='localhost', $port = 6379) {
/**
* Class Rdis
*/
class Rdis {

protected $socket;
protected $host;
protected $port;

/**
* @param string $host
* @param int $port
*/
public function __construct($host='localhost', $port = 6379) {
$this->host = $host;
$this->port = $port;
if (!$this->socket = fsockopen($this->host, $this->port, $errno, $errstr)) {
throw new Exception("{$errno} - {$errstr}");
}
}

public function __destruct() {
/**
*
*/
public function __destruct() {
fclose($this->socket);
}

/**
* Todos la llamadas de redis entran aqui. Para ver un explicacion como funciona
* __call mira el docs in http://www.php.net
*/
public function __call($name, $args) {
/**
* @param $name
* @param $args
* @return mixed
* @throws Exception
*/
public function __call($name, $args) {
array_unshift($args, strtoupper($name));
$cmd = sprintf('*%d%s%s%s', count($args), NL, $this->argsWithLengths($args), NL);
$done = 0;
$cmdlen = strlen($cmd);
for ($w = 0; $w < $cmdlen; $w += $done) {
$cmd = sprintf('*%d%s%s%s', count($args), NL, $this->argsWithLengths($args), NL);
$commandLength = strlen($cmd);
for ($w = 0; $w < $commandLength; $w += $done) {
$done = fwrite($this->socket, substr($cmd, $w));
if ($done === FALSE) {
throw new Exception('Failed to write entire command to stream');
Expand All @@ -38,49 +51,49 @@ public function __call($name, $args) {
return $response;
}

private function handleReply(){
/**
* @return mixed
*/
protected function handleReply(){
$reply = trim(fgets($this->socket, 512));
$responses = array(
'+' => 'replySingle' , '-' => 'replyError' , ':' => 'replyInt' ,
'$' => 'replyBulk' , '*' => 'replyMultiBulk'
);
$response_code = substr($reply, 0, 1);
try {
$func = array($this, $responses[$response_code]);
$response = call_user_func_array($func, array($reply));
$func = [$this, $responses[$response_code]];
$response = call_user_func_array($func, [$reply]);
return $response;
} catch (Exception $e) {
die("server response makes no sense to me: {$reply}");
}
}

/******************
* Reply Handlers *
******************/
private function replySingle($reply){
protected function replySingle($reply){
$response = substr(trim($reply), 1);
return $response;
}

private function replyError($reply){
protected function replyError($reply){
throw new Exception(substr(trim($reply), 4));
}

private function replyInt($reply){
protected function replyInt($reply){
$response = intval(substr(trim($reply), 1));
return $response;
}

private function replyBulk($reply){
protected function replyBulk($reply){
$response = null;
if ($reply == '$-1') {
break;
return null;
}
$response = $this->readSocketStream($reply);
return $response;
}

private function replyMultiBulk($reply){
protected function replyMultiBulk($reply){
$count = substr($reply, 1);
if ($count == '-1') {
return null;
Expand All @@ -99,15 +112,24 @@ private function replyMultiBulk($reply){
/*******************
* Utility methods *
*******************/
private function argsWithLengths($args){

/**
* @param $args
* @return string
*/
protected function argsWithLengths($args){
$cmd_parts = array_map(function($x){
return sprintf("$%d%s%s", strlen($x), NL, $x); },
$args
);
return implode(NL, $cmd_parts).NL;
}

private function asHashArray(array $values){
/**
* @param array $values
* @return array
*/
protected function asHashArray(array $values){
$pairs = array_chunk($values, 2);
$out = array();
foreach ($pairs as $pair){
Expand All @@ -116,11 +138,12 @@ private function asHashArray(array $values){
return $out;
}

private function inChunks($array){
return array_chunk($array, $this->getChunkSize());
}

private function readSocketStream($read, $so_far=0){
/**
* @param $read
* @param int $so_far
* @return string
*/
protected function readSocketStream($read, $so_far=0){
$response = "";
$size = substr($read, 1);
do {
Expand Down
9 changes: 7 additions & 2 deletions test_redis.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

include 'redis.php';
$r = new Redis('localhost', 6379);

$r = new Rdis('localhost', 6379);

/** @var $r Re*/
$r->hmset('myhmset', 'foo', 'yep', 'bar', 'pey');
$r->lpush('mylist', 'foobar');
$r->hgetall('myhmset');
print_r($r->hgetall('myhmset'));

0 comments on commit bc3799f

Please sign in to comment.