Skip to content

Commit

Permalink
code style and documentation for CRedisCache
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed May 1, 2013
1 parent f076a9e commit 312be72
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 49 deletions.
2 changes: 1 addition & 1 deletion framework/caching/CApcCache.php
Expand Up @@ -39,7 +39,7 @@ public function init()
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
Expand Down
2 changes: 1 addition & 1 deletion framework/caching/CCache.php
Expand Up @@ -238,7 +238,7 @@ public function flush()
* in {@link get()} already. So only the implementation of data retrieval
* is needed.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
* @throws CException if this method is not overridden by child classes
*/
protected function getValue($key)
Expand Down
2 changes: 1 addition & 1 deletion framework/caching/CDbCache.php
Expand Up @@ -176,7 +176,7 @@ public function setDbConnection($value)
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
Expand Down
2 changes: 1 addition & 1 deletion framework/caching/CEAcceleratorCache.php
Expand Up @@ -40,7 +40,7 @@ public function init()
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
Expand Down
2 changes: 1 addition & 1 deletion framework/caching/CFileCache.php
Expand Up @@ -98,7 +98,7 @@ protected function flushValues()
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
Expand Down
2 changes: 1 addition & 1 deletion framework/caching/CMemCache.php
Expand Up @@ -142,7 +142,7 @@ public function setServers($config)
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
Expand Down
86 changes: 46 additions & 40 deletions framework/caching/CRedisCache.php
Expand Up @@ -28,7 +28,7 @@
* array(
* 'components'=>array(
* 'cache'=>array(
* 'class'=>'RedisCache',
* 'class'=>'CRedisCache',
* 'hostname'=>'localhost',
* 'port'=>6379,
* 'database'=>0,
Expand All @@ -48,11 +48,11 @@ class CRedisCache extends CCache
*/
public $hostname='localhost';
/**
* @var int the to use for connecting to the redis server. Default port is 6379.
* @var int the port to use for connecting to the redis server. Default port is 6379.
*/
public $port=6379;
/**
* @var string the password to use to identify with the redis server. If not set, no AUTH command will be sent.
* @var string the password to use to authenticate with the redis server. If not set, no AUTH command will be sent.
*/
public $password;
/**
Expand Down Expand Up @@ -88,16 +88,27 @@ protected function connect()
$this->executeCommand('SELECT',array($this->database));
}
else
throw new CException('failed to connect to redis: '.$errorDescription,(int)$errorNumber);
throw new CException('Failed to connect to redis: '.$errorDescription,(int)$errorNumber);
}

/**
* Execute a redis command
* Executes a redis command.
* For a list of available commands and their parameters see {@link http://redis.io/commands}.
*
* @see http://redis.io/commands
* @param $name
* @param $params
* @return array|bool|null|string
* @param string $name the name of the command
* @param array $params list of parameters for the command
* @return array|bool|null|string Dependend on the executed command this method
* will return different data types:
* <ul>
* <li><code>true</code> for commands that return "status reply".</li>
* <li><code>string</code> for commands that return "integer reply"
* as the value is in the range of a signed 64 bit integer.</li>
* <li><code>string</code> or <code>null</code> for commands that return "bulk reply".</li>
* <li><code>array</code> for commands that return "Multi-bulk replies".</li>
* </ul>
* See {@link http://redis.io/topics/protocol redis protocol description}
* for details on the mentioned reply types.
* @trows CException for commands that return {@link http://redis.io/topics/protocol#error-reply error reply}.
*/
public function executeCommand($name,$params=array())
{
Expand All @@ -109,9 +120,9 @@ public function executeCommand($name,$params=array())
foreach($params as $arg)
$command.='$'.strlen($arg)."\r\n".$arg."\r\n";

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

return $this->parseResponse(implode(' ', $params));
return $this->parseResponse(implode(' ',$params));
}

/**
Expand All @@ -121,24 +132,23 @@ public function executeCommand($name,$params=array())
*/
private function parseResponse()
{
if(($line=fgets($this->_socket))===false) {
if(($line=fgets($this->_socket))===false)
throw new CException('Failed reading data from redis connection socket.');
}
$type=$line[0];
$line=substr($line,1,-2);
switch($type)
{
case '+': // Status reply
return true;
case '-': // Error reply
throw new CException("Redis error: " . $line);
throw new CException('Redis error: '.$line);
case ':': // Integer reply
// no cast to int as it is in the range of a signed 64 bit integer
return $line;
case '$': // Bulk replies
if($line=='-1')
return null;
if(($data = fread($this->_socket,$line+2))===false)
if(($data=fread($this->_socket,$line+2))===false)
throw new CException('Failed reading data from redis connection socket.');
return substr($data,0,-2);
case '*': // Multi-bulk replies
Expand All @@ -156,11 +166,11 @@ private function parseResponse()
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return $this->executeCommand('GET', array($key));
return $this->executeCommand('GET',array($key));
}

/**
Expand All @@ -170,12 +180,11 @@ protected function getValue($key)
*/
protected function getValues($keys)
{
$response = $this->executeCommand('MGET', $keys);
$result = array();
$i = 0;
foreach($keys as $key) {
$result[$key] = $response[$i++];
}
$response=$this->executeCommand('MGET',$keys);
$result=array();
$i=0;
foreach($keys as $key)
$result[$key]=$response[$i++];
return $result;
}

Expand All @@ -190,12 +199,10 @@ protected function getValues($keys)
*/
protected function setValue($key,$value,$expire)
{
if ($expire == 0) {
return (bool) $this->executeCommand('SET', array($key, $value));
} else {
$expire = (int) ($expire * 1000);
return (bool) $this->executeCommand('PSETEX', array($key, $expire, $value));
}
if ($expire==0)
return (bool)$this->executeCommand('SET',array($key,$value));
$expire=(int)($expire*1000);
return (bool)$this->executeCommand('PSETEX',array($key,$expire,$value));
}

/**
Expand All @@ -209,16 +216,15 @@ protected function setValue($key,$value,$expire)
*/
protected function addValue($key,$value,$expire)
{
if ($expire == 0) {
return (bool) $this->executeCommand('SETNX', array($key, $value));
} else {
$expire = (int) ($expire * 1000);
$this->executeCommand('MULTI');
$this->executeCommand('SETNX', array($key, $value));
$this->executeCommand('PEXPIRE', array($key, $expire));
$response = $this->executeCommand('EXEC');
return (bool) $response[0];
}
if ($expire == 0)
return (bool)$this->executeCommand('SETNX',array($key,$value));

$expire=(int)($expire*1000);
$this->executeCommand('MULTI');
$this->executeCommand('SETNX',array($key,$value));
$this->executeCommand('PEXPIRE',array($key,$expire));
$response = $this->executeCommand('EXEC');
return (bool)$response[0];
}

/**
Expand All @@ -229,7 +235,7 @@ protected function addValue($key,$value,$expire)
*/
protected function deleteValue($key)
{
return (bool) $this->executeCommand('DEL', array($key));
return (bool)$this->executeCommand('DEL',array($key));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion framework/caching/CWinCache.php
Expand Up @@ -39,7 +39,7 @@ public function init()
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
Expand Down
2 changes: 1 addition & 1 deletion framework/caching/CXCache.php
Expand Up @@ -38,7 +38,7 @@ public function init()
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
Expand Down
2 changes: 1 addition & 1 deletion framework/caching/CZendDataCache.php
Expand Up @@ -38,7 +38,7 @@ public function init()
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
Expand Down

0 comments on commit 312be72

Please sign in to comment.