Skip to content

Instantly share code, notes, and snippets.

@phpnode
Created January 24, 2011 17:19
Show Gist options
  • Save phpnode/793552 to your computer and use it in GitHub Desktop.
Save phpnode/793552 to your computer and use it in GitHub Desktop.
<?php
if (!class_exists("Redis")) {
class Redis extends RedisPredis {
}
class RedisException extends RedisPredisException {
}
}
class RedisPredis {
const BEFORE = 5;
const AFTER = 6;
const OPT_SERIALIZER = 1;
const OPT_PREFIX = 2;
const SERIALIZER_NONE = 0;
const SERIALIZER_PHP = 1;
const SERIALIZER_IGBINARY = 2;
const ATOMIC = 0;
const MULTI = 1;
const PIPELINE = 2;
const REDIS_NOT_FOUND = 0;
const REDIS_STRING = 1;
const REDIS_SET = 2;
const REDIS_LIST = 3;
const REDIS_ZSET = 4;
const REDIS_HASH = 5;
protected $mode = 0; // atomic by default
protected $options = array();
protected $instance;
public $multiInstance;
protected $persistConnections = false;
/**
* Get the Predis instance
* @param string $host can be a host, or the path to a unix domain socket
* @param integer $port optional
* @param float $timeout value in seconds (optionsal, default is 0 meaning unlimited)
* @return Predis\Client
*/
protected function getPredis($host = "localhost", $port = 6379, $timeout = 0) {
if (!is_object($this->instance)) {
$serverProfile = Predis\RedisServerProfile::get('dev');
$this->instance = new Predis\Client(array(
"host" => $host,
"port" => $port,
"timeout" => $timeout,
),$serverProfile);
}
if ($this->mode == self::MULTI) {
return $this->instance;
}
elseif ($this->mode == self::PIPELINE) {
return $this->instance->pipeline();
}
return $this->instance;
}
/**
* Creates a Redis client
*/
public function __construct() {
$this->options[self::OPT_SERIALIZER] = self::SERIALIZER_NONE;
}
/**
*
* Connects to a Redis instance
* @param string $host can be a host, or the path to a unix domain socket
* @param integer $port optional
* @param float $timeout value in seconds (optionsal, default is 0 meaning unlimited)
* @return boolean TRUE on success FALSE on error
*/
protected function connect($host = "localhost", $port = 6379, $timeout = 0) {
$predis = $this->getPredis($host,$port,$timeout);
$this->persistConnections = false;
if (!$predis->isConnected()) {
try {
$predis->setupConnection(array(
"host" => $host,
"port" => $port,
"timeout" => $timeout,
));
$predis->connect();
}
catch (Exception $e) {
return false;
}
}
return true;
}
/**
* @see connect()
*/
protected function open($host = "localhost",$port = 6379, $timeout = 0) {
return $this->connect($host,$port,$timeout);
}
/**
*
* Connects to a Redis instance or reuse a connection already established with pconnect/popen.
* The connection will not be closed on close or end of request until the php process ends. So be patient on to many open FD's (specially on redis server side) when using persistent connections on many servers connecting to one redis server.
* Also more than one persistent connection can be made identified by either host + port + timeout or unix socket + timeout.
* This feature is not available in threaded versions. pconnect and popen then working like their non persistent equivalents.
* @param string $host can be a host, or the path to a unix domain socket
* @param integer $port optional
* @param float $timeout value in seconds (optionsal, default is 0 meaning unlimited)
* @return boolean TRUE on success FALSE on error
*/
protected function pconnect($host = "localhost", $port = 6379, $timeout = 0) {
$predis = $this->getPredis($host,$port,$timeout);
$this->persistConnections = true;
if (!$predis->isConnected()) {
try {
$predis->setupConnection(array(
"host" => $host,
"port" => $port,
"timeout" => $timeout,
));
$predis->connect();
}
catch (Exception $e) {
return false;
}
}
return true;
}
protected function popen($host = "localhost",$port = 6379, $timeout = 0) {
return $this->pconnect($host,$port,$timeout);
}
/**
* Disconnects from the Redis instance, except when pconnect is used.
*
*/
protected function close() {
if ($this->persistConnections) {
return false;
}
$predis = $this->getPredis();
if ($predis->isConnected()) {
try{
$predis->disconnect();
return true;
}
catch (Exception $e) {
return false;
}
}
return false;
}
/**
* Set client option
* @param mixed $name
* @param mixed $value
*/
protected function setOption($name,$value) {
$this->options[$name] = $value;
return true;
}
/**
* Get client option
* @param mixed $name
*/
protected function getOption($name) {
return $this->options[$name];
}
/**
* Check the current connection status
* @return string "PONG" on success. Throws a RedisException object on connectivity error.
*/
protected function ping() {
$predis = $this->getPredis();
try {
if ($predis->ping()) {
return "+PONG";
}
else {
throw new RedisException("Couldn't Connect To Redis Server");
}
}
catch (Exception $e) {
throw new RedisException("Couldn't Connect To Redis Server");
}
}
/**
* Get the value related to the specified key
* @param string $key
* @return string or boolean - If key didn't exist, FALSE is returned. Otherwise, the value related to this key is returned.
*/
protected function get($key) {
try {
return $this->getPredis()->get($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Sets the string value in argument as value of the key
* @param string $key
* @param mixed $value
* @param float $timeout (optional). Calling SETEX is preferred if you want a timeout
* @return boolean true if the command is successful
*/
protected function set($key,$value,$timeout = null) {
try {
if ($timeout === null) {
return $this->getPredis()->set($key,$value);
}
else {
return $this->getPredis()->setex($key,$timeout,$value);
}
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Set the string value in argument as value of the key, with a time to live.
* @param string $key
* @param float $timeout
* @param mixed $value
* @return boolean true if the command is successful
*/
protected function setex($key,$timeout,$value) {
try {
return $this->getPredis()->setex($key,$timeout,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Set the string value in argument as value of the key if the key doesn't already exist in the database.
* @param string $key
* @param mixed $value
* @return boolean true if the command is successful
*/
protected function setnx($key,$value) {
try {
return $this->getPredis()->setnx($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Remove specified keys
* @param An array of keys, or an undefined number of parameters, each a key: key1 key2 key3 ... keyN
* @return integer number of keys deleted
*/
protected function delete(/* arguments */) {
try {
return call_user_func_array(array($this->getPredis(),"delete"),func_get_args());
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see delete()
*/
protected function del(/* arguments */) {
return call_user_func_array(array($this,"delete"),func_get_args());
}
/**
* Enter transactional mode
* @param int $mode Redis::MULTI or Redis::PIPELINE, defaults to Redis::MULTI
* @return returns the Redis instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object until exec() is called.
*/
public function multi($mode = self::MULTI) {
$this->mode = $mode;
if ($mode === self::MULTI) {
$this->getPredis()->multi();
}
return $this;
}
/**
* Execute all the commands in this transaction and exit transactional mode
* @return array An array of response
*/
public function exec() {
try {
if ($this->mode === self::MULTI) {
$response = $this->getPredis()->exec();
}
else {
$response = $this->getPredis()->execute();
}
$this->mode = self::ATOMIC;
return $response ? $response : false;
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
/**
* Discard all transactions and exit transactional mode
*/
protected function discard() {
try {
$response = $this->getPredis()->discard();
$this->mode = self::ATOMIC;
return $response;
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Watches a key for modifications by another client. If the key is modified between WATCH and EXEC, the MULTI/EXEC transaction will fail (return FALSE).
* @params mixed a list of keys
* @throws RedisException
*/
protected function watch(/* arguments */) {
try {
return call_user_func_array(array($this->getPredis(),"watch"),func_get_args());
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Cancels all the watching of all keys by this client
* @params mixed a list of keys
* @throws RedisException
*/
protected function unwatch(/* arguments */) {
try {
return call_user_func_array(array($this->getPredis(),"unwatch"),func_get_args());
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Subscribe to channels. Warning: this function will probably change in the future.
* @param array $channels an array of channels to subscribe to
* @param mixed $callback either a string or an array($instance, 'method_name'). The callback function receives 3 parameters: the redis instance, the channel name, and the message.
*/
protected function subscribe($channels,$callback) {
try {
return $this->getPredis()->subscribe($channels,$callback);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Publish messages to channels. Warning: this function will probably change in the future.
* @param string $channel a channel to publish to
* @param string $message a message to publish
* @throws RedisException
*/
protected function publish($channel,$message) {
try {
return $this->getPredis()->publish($channel,$message);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Verify if the specified key exists
* @param string $key
* @throws RedisException
* @return boolean true if the key exists, otherwise false
*/
protected function exists($key) {
try {
return $this->getPredis()->exists($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Increment the number stored at key by one
* @param string $key
* @param string $value optional
* @throws RedisException
* @return integer the new value
*/
protected function incr($key,$value = 1) {
try {
if ($value === 1) {
return $this->getPredis()->incr($key);
}
else {
return $this->getPredis()->incrby($key,$value);
}
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Increment the number stored at key by the specified value
* @param string $key
* @param string $value
* @throws RedisException
* @return integer the new value
*/
protected function incrBy($key,$value) {
try {
return $this->getPredis()->incrby($key,$value);
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
/**
* Decrement the number stored at key by one
* @param string $key
* @throws RedisException
* @return integer the new value
*/
protected function decr($key,$value = 1) {
try {
if ($value === 1) {
return $this->getPredis()->decr($key);
}
else {
return $this->getPredis()->decrby($key,$value);
}
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Decrement the number stored at key by the specified value
* @param string $key
* @param string $value
* @throws RedisException
* @return integer the new value
*/
protected function decrBy($key,$value) {
try {
return $this->getPredis()->decrby($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Get the values of all the specified keys. If one or more keys dont exist, the array will contain FALSE at the position of the key.
* @param array $keys
* @throws RedisException
* @return array containing the values related to keys in argument
*/
protected function mget($keys) {
try {
return $this->getPredis()->mget($keys);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
protected function getMultiple($keys) {
return $this->mget($keys);
}
/**
* Adds the string value to the head (left) of the list. Creates the list if the key didn't exist. If the key exists and is not a list, FALSE is returned.
* @param string $key
* @param string $value
* @throws RedisException
* @return long The new length of the list in case of success, false in case of failure
*/
protected function lPush($key,$value) {
try {
return $this->getPredis()->lpush($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Adds the string value to the tail (right) of the list. Creates the list if the key didn't exist. If the key exists and is not a list, FALSE is returned.
* @param string $key
* @param string $value
* @throws RedisException
* @return long The new length of the list in case of success, false in case of failure
*/
protected function rPush($key,$value) {
try {
return $this->getPredis()->rpush($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Adds the string value to the head (left) of the list if the list exists
* @param string $key
* @param string $value
* @throws RedisException
* @return long The new length of the list in case of success, false in case of failure
*/
protected function lPushx($key,$value) {
try {
return $this->getPredis()->lpushx($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Adds the string value to the tail (right) of the list if the list exists
* @param string $key
* @param string $value
* @throws RedisException
* @return long The new length of the list in case of success, false in case of failure
*/
protected function rPushx($key,$value) {
try {
return $this->getPredis()->rpushx($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Return and remove the first element of the list.
* @param string $key
* @throws RedisException
* @return mixed string if the command completed successfully, otherwise false
*/
protected function lpop($key) {
try {
return $this->getPredis()->lpop($key);
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
/**
* Return and remove the last element of the list.
* @param string $key
* @throws RedisException
* @return mixed string if the command completed successfully, otherwise false
*/
protected function rpop($key) {
try {
return $this->getPredis()->rpop($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Is a blocking lPop primitive. If at least one of the lists contains at least one element,
* the element will be popped from the head of the list and returned to the caller.
* If all the list identified by the keys passed in arguments are empty, blPop will block during the
* specified timeout until an element is pushed to one of those lists. This element will be popped.
* @param string $key
* @throws RedisException
* @return mixed string if the command completed successfully, otherwise false
*/
protected function blpop(/* arguments */) {
try {
$arguments = func_get_args();
if (is_array($arguments[0])) {
$arguments = array_merge(array_shift($arguments),$arguments);
}
$response = call_user_func_array(array($this->getPredis(),"blpop"),$arguments);
if ($response === null) {
return array();
}
return $response;
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Is a blocking rPop primitive. If at least one of the lists contains at least one element,
* the element will be popped from the tail of the list and returned to the caller.
* If all the list identified by the keys passed in arguments are empty, brPop will block during the
* specified timeout until an element is pushed to one of those lists. This element will be popped.
* @param string $key
* @throws RedisException
* @return mixed string if the command completed successfully, otherwise false
*/
protected function brpop(/* arguments */) {
try {
$arguments = func_get_args();
if (is_array($arguments[0])) {
$arguments = array_merge(array_shift($arguments),$arguments);
}
$response = call_user_func_array(array($this->getPredis(),"brpop"),$arguments);
if ($response === null) {
return array();
}
return $response;
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the size of a list identified by Key.
* If the list didn't exist or is empty, the command returns 0. If the data type identified by Key is not a list, the command return FALSE.
* @param string $key
* @throws RedisException
* @return integer the size of the list, or false if the specified value is not a list
*/
protected function lSize($key) {
try {
return $this->getPredis()->llen($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Return the specified element of the list stored at the specified key. 0 the first element, 1 the second ... -1 the last element, -2 the penultimate ... Return FALSE in case of a bad index or a key that doesn't point to a list.
* @param string $key
* @param integer $index
* @throws RedisException
* @return string the element at this index or false if it doesn't exist
*/
protected function lget($key,$index) {
try {
return $this->getPredis()->lindex($key,$index);
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see lget()
*/
protected function lindex($key,$index) {
return $this->lget($key,$index);
}
/**
* Set the list at index with the new value.
* @param string $key
* @param integer $index
* @param string $value
* @return boolean true if the value was set, false otherwise
*/
protected function lset($key,$index,$value) {
try {
return $this->getPredis()->lset($key,$index,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the specified elements of the list stored at the specified key in the range [start, end].
* start and stop are interpretated as indices: 0 the first element, 1 the second ... -1 the last element, -2 the penultimate ...
* @param string $key
* @param integer $start
* @param integer $end
* @throws RedisException
* @return array array containing the values in specified string
*/
protected function lrange($key,$start,$end) {
try {
return $this->getPredis()->lrange($key,$start,$end);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see lrange()
*/
protected function lgetrange($key,$start,$end) {
return $this->lrange($key,$start,$end);
}
/**
* Trims an existing list so that it will contain only a specified range of elements.
* @param string $key
* @param integer $start
* @param integer $end
* @throws RedisException
* @return array array containing the values in specified range or false if the key identifies a non-list value
*/
protected function ltrim($key,$start,$end) {
try {
return $this->getPredis()->ltrim($key,$start,$end);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see ltrim()
*/
protected function listTrim($key,$start,$end) {
return $this->ltrim($key,$start,$end);
}
/**
* Removes the first count occurences of the value element from the list. If count is zero, all the matching elements are removed.
* If count is negative, elements are removed from tail to head.
* @param string $key
* @param string $value
* @param integer $count
*/
protected function lRem($key,$value,$count = 0) {
try {
return $this->getPredis()->lrem($key,$count,$value);
return abs($count); // this is a guess at expected functionality
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see lrem()
*/
protected function lRemove($key,$value,$count = 0) {
return $this->lrem($key,$value,$count);
}
/**
* Insert value in the list before or after the pivot value. the parameter options specify the position of the insert (before or after). If the list didn't exists, or the pivot didn't exists, the value is not inserted.
* @param string $key
* @param integer $position Redis::BEFORE or Redis::AFTER
* @param string $pivot
* @param string $value
* @return The number of the elements in the list, -1 if the pivot didn't exist
*/
protected function lInsert($key,$position,$pivot,$value) {
try {
if ($position == self::AFTER) {
$position = "after";
}
else {
$position = "before";
}
return $this->getPredis()->linsert($key,$position,$pivot,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Adds a value to the set value stored at key. If this value is already in the set, FALSE is returned.
* @param string $key
* @param string $value
* @throws RedisException
*/
protected function sAdd($key,$value) {
try {
return $this->getPredis()->sadd($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Removes the specified member from the set value stored at key.
* @param string $key
* @param string $value
* @throws RedisException
*/
protected function srem($key,$value) {
try {
return $this->getPredis()->srem($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see srem()
*/
protected function sremove($key,$value) {
return $this->srem($key,$value);
}
/**
* Moves the specified member from the set at srcKey to the set at dstKey.
* @param string $srcKey
* @param string $dstKey
* @param string $member
* @return boolean If the operation is successful, return TRUE. If the srcKey and/or dstKey didn't exist, and/or the member didn't exist in srcKey, FALSE is returned.
*/
protected function smove($srcKey,$dstKey,$member) {
try {
return $this->getPredis()->smove($srcKey,$dstKey,$member);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Checks if value is a member of the set stored at the key key.
* @param string $key
* @param string $value
* @throws RedisException
* @return boolean TRUE if value is a member of the set at key key, FALSE otherwise.
*/
protected function sIsMember($key,$value) {
try {
return $this->getPredis()->sismember($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see sIsMember()
*/
protected function sContains($key,$value) {
return $this->sIsMember($key, $value);
}
/**
* Returns the cardinality of the set identified by key.
* @param string $key
* @throws RedisException
* @return long the cardinality of the set identified by key, 0 if the set doesn't exist.
*/
protected function sCard($key) {
try {
return $this->getPredis()->scard($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see sCard()
*/
protected function sSize($key) {
return $this->sCard($key);
}
/**
* Removes and returns a random element from the set value at Key.
* @param string $key
* @throws RedisException
* @return string "popped" value or false if the set is empty or doesn't exist
*/
protected function sPop($key) {
try {
$response = $this->getPredis()->spop($key);
if (!$response) {
return false;
}
return $response;
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns a random element from the set value at Key, without removing it.
* @param string $key
* @throws RedisException
* @return string "popped" value or false if the set is empty or doesn't exist
*/
protected function sRandMember($key) {
try {
$response = $this->getPredis()->srandmember($key);
if (!$response) {
return false;
}
return $response;
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the members of a set resulting from the intersection of all the sets held at the specified keys. If just a single key is specified, then this command produces the members of this set. If one of the keys is missing, FALSE is returned.
* @param key1, key2, keyN: keys identifying the different sets on which we will apply the intersection.
* @throws RedisException
* @return array containing the result of the intersection between those keys. If the intersection beteen the different sets is empty, the return value will be empty array.
*/
protected function sInter(/* arguments */) {
try {
return call_user_func_array(array($this->getPredis(),"sinter"),func_get_args());
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the members of a set resulting from the intersection of all the sets held at the specified keys. If just a single key is specified, then this command produces the members of this set. If one of the keys is missing, FALSE is returned.
* @param $dstkey the key to store the diff into.
* @param key1, key2, keyN: keys identifying the different sets on which we will apply the intersection.
* @throws RedisException
* @return integer The cardinality of the resulting set, or FALSE in case of a missing key.
*/
protected function sInterStore(/* arguments */) {
try {
return call_user_func_array(array($this->getPredis(),"sinterstore"),func_get_args());
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Performs the union between N sets and returns it.
* @param key1, key2, keyN: Any number of keys corresponding to sets in redis.
* @throws RedisException
* @return array The union of all these sets.
*/
protected function sUnion(/* arguments */) {
try {
return call_user_func_array(array($this->getPredis(),"sunion"),func_get_args());
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Performs the same action as sUnion, but stores the result in the first key
* @param $dstkey the key to store the union into.
* @param key1, key2, keyN: keys identifying the different sets on which we will apply the intersection.
* @throws RedisException
* @return integer The cardinality of the resulting set, or FALSE in case of a missing key.
*/
protected function sUnionStore(/* arguments */) {
try {
return call_user_func_array(array($this->getPredis(),"sunionstore"),func_get_args());
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Performs the difference between N sets and returns it
* @param key1, key2, keyN: Any number of keys corresponding to sets in redis.
* @throws RedisException
* @return array The difference of the first set will all the others.
*/
protected function sDiff(/* arguments */) {
try {
return call_user_func_array(array($this->getPredis(),"sdiff"),func_get_args());
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Performs the same action as sDiff, but stores the result in the first key
* @param $dstkey the key to store the diff into.
* @param key1, key2, keyN: keys identifying the different sets on which we will apply the intersection.
* @throws RedisException
* @return integer The cardinality of the resulting set, or FALSE in case of a missing key.
*/
protected function sDiffStore(/* arguments */) {
try {
return call_user_func_array(array($this->getPredis(),"sdiffstore"),func_get_args());
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the contents of a set.
* @param string $key
* @return array of elements, the contents of the set.
*/
protected function sMembers($key) {
try {
return $this->getPredis()->smembers($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see sMembers()
*/
protected function sGetMembers($key) {
return $this->sMembers($key);
}
/**
* Sets a value and returns the previous entry at that key.
* @param string $key
* @param string $value
* @throws RedisException
* @return string the previous value located at this key.
*/
protected function getSet($key,$value) {
try {
return $this->getPredis()->getset($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns a random key
* @throws RedisException
* @return string an existing key in redis
*/
protected function randomKey() {
try {
return $this->getPredis()->randomkey();
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Switches to a given database
* @param integer $dbIndex
* @throws RedisException
* @return boolean true in case of success, false in case of failure
*/
protected function select($dbIndex) {
try {
return $this->getPredis()->select($dbIndex);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Moves a key to a different database.
* @param string $key
* @param integer $dbIndex
* @throws RedisException
* @return boolean true in case of success, false in case of failure
*/
protected function move($key,$dbIndex) {
try {
return $this->getPredis()->move($key,$dbIndex);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Renames a key
* @param string $srcKey
* @param string $dstKey
* @throws RedisException
* @return boolean true in case of success, false in case of failure
*/
protected function rename($srcKey,$dstKey) {
try {
return $this->getPredis()->rename($srcKey,$dstKey);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see rename()
*/
protected function renameKey($srcKey,$dstKey) {
return $this->rename($srcKey,$dstKey);
}
/**
* Same as rename, but will not replace a key if the destination already exists. This is the same behaviour as setNx.
* @param string $srcKey
* @param string $dstKey
* @throws RedisException
* @return boolean true in case of success, false in case of failure
*/
protected function renameNx($srcKey,$dstKey) {
try {
return $this->getPredis()->renamenx($srcKey,$dstKey);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Sets an expiration date (a timeout) on an item.
* @param string $key The key that will disappear.
* @param integer $ttl The key's remaining Time To Live, in seconds.
* @throws RedisException
* @return boolean true in case of success, false in case of failure
*/
protected function setTimeout($key,$ttl) {
try {
return $this->getPredis()->expire($key,$ttl);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see setTimeout()
*/
protected function expire($key,$ttl) {
return $this->setTimeout($key, $ttl);
}
/**
* Sets an expiration date (a timestamp) on an item.
* @param string $key The key that will disappear.
* @param integer $timestamp Unix timestamp. The key's date of death, in seconds from Epoch time.
* @throws RedisException
* @return boolean true in case of success, false in case of failure
*/
protected function expireAt($key,$timestamp) {
try {
return $this->getPredis()->expireAt($key,$timestamp);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the keys that match a certain pattern.
* @param string $pattern pattern, using '*' as a wildcard.
* @throws RedisException
* @return array The keys that match a certain pattern.
*/
protected function keys($pattern) {
try {
return $this->getPredis()->keys($pattern);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* see keys()
*/
protected function getKeys($pattern) {
return $this->keys($pattern);
}
/**
* Returns the current database's size
* @return integer DB size, in number of keys
*/
protected function dbSize() {
try {
return $this->getPredis()->dbsize();
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Authenticate the connection using a password. Warning: The password is sent in plain-text over the network.
* @param string $password
* @throws RedisException
* @return boolean TRUE if the connection is authenticated, FALSE otherwise.
*/
protected function auth($password) {
try {
return $this->getPredis()->auth($password);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Starts the background rewrite of AOF (Append-Only File)
* @return boolean TRUE in case of success, FALSE in case of failure.
*/
protected function bgrewriteaof() {
try {
return $this->getPredis()->bgrewriteaof();
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Changes the slave status
* Either host (string) and port (int), or no parameter to stop being a slave.
* @param string $host
* @param integer $port
* @throws RedisException
* @return boolean TRUE in case of success, FALSE in case of failure.
*/
protected function slaveof($host = null, $port = null) {
try {
if ($host === null) {
return $this->getPredis()->slaveof();
}
else {
return $this->getPredis()->slaveof($host,$port);
}
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Performs a synchronous save.
* @throws RedisException
* @return boolean TRUE in case of success, FALSE in case of failure. If a save is already running, this command will fail and return FALSE.
*/
protected function save() {
try {
return $this->getPredis()->save();
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Performs a background save.
* @throws RedisException
* @return boolean TRUE in case of success, FALSE in case of failure. If a save is already running, this command will fail and return FALSE.
*/
protected function bgsave() {
try {
return $this->getPredis()->bgsave();
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the timestamp of the last disk save.
* @throws RedisException
* @return integer timestamp
*/
protected function lastSave() {
try {
return $this->getPredis()->lastsave();
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the type of data pointed by a given key.
* @param string $key
* @throws RedisException
* @return integer Redis::REDIS_STRING, Redis::REDIS_SET, Redis::REDIS_LIST, Redis::REDIS_ZSET, Redis::REDIS_HASH or Redis::REDIS_NOT_FOUND
*/
protected function type($key) {
try {
$types = array(
"list" => self::REDIS_LIST,
"string" => self::REDIS_STRING,
"set" => self::REDIS_SET,
"hash" => self::REDIS_HASH,
"zset" => self::REDIS_ZSET,
"none" => self::REDIS_NOT_FOUND,
);
return $types[$this->getPredis()->type($key)];
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Append specified string to the string stored in specified key.
* @param string $key
* @param string $value
* @throws RedisException
* @return integer Size of the value after the append
*/
protected function append($key,$value) {
try {
return $this->getPredis()->append($key,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Return a substring of a larger string
* @param string $key
* @param integer $start
* @param integer $end
* @throws RedisException
* @return string the substring
*/
protected function getRange($key,$start,$end) {
try {
return $this->getPredis()->getrange($key,$start,$end);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see getRange()
*/
protected function substr($key,$start,$end) {
return $this->getRange($key, $start, $end);
}
/**
* Changes a substring of a larger string.
* @param string $key
* @param integer $offset
* @param string $value
* @throws RedisException
* @return string the length of the string after it was modified.
*/
protected function setRange($key,$offset,$value) {
try {
return $this->getPredis()->setrange($key,$offset,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Get the length of a string value.
* @param string $key
* @throws RedisException
* @return integer the length of the value
*/
protected function strlen($key) {
try {
return $this->getPredis()->strlen($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Return a single bit out of a larger string
* @param $key
* @param $offset
* @return integer the bit value 0 or 1
*/
protected function getBit($key,$offset) {
if ($offset < 0) {
return false;
}
try {
$response = $this->getPredis()->getbit($key,$offset);
return $response;
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Changes a single bit of a string.
* @param $key
* @param $offset
* @param $value
* @return integer 0 or 1, the value of the bit before it was set.
*/
protected function setBit($key,$offset,$value) {
try {
$response = $this->getPredis()->setbit($key,$offset,$value);
return $response;
}
catch (Exception $e) {
if ($e->getMessage() == "bit is not an integer or out of range") {
return 0;
}
return false; // throw new RedisException("Redis Error");
}
}
/**
* Removes all entries from the current database.
* @throws RedisException
* @return boolean
*/
protected function flushDB() {
try {
return $this->getPredis()->flushdb();
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Removes all entries from all databases.
* @throws RedisException
* @return boolean
*/
protected function flushAll() {
try {
return $this->getPredis()->flushall();
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Sort a set
* @param string $key
* @param array $options
* @throws RedisException
* @return array An array of values, or a number corresponding to the number of elements stored if that was used.
*/
protected function sort($key,$options = array()) {
try {
return $this->getPredis()->sort($key,$options);
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
protected function sortAsc($key,$pattern = null,$get = null,$start = null,$end = null) {
if ($key === null) {
return false;
}
$options = array();
if ($pattern != null) {
$options['by'] = $pattern;
}
if ($get !== null) {
$options['get'] = $get;
}
if ($start === null) {
$start = 0;
}
$options['limit'] = array();
$options['limit'][] = $start;
if ($end !== null) {
$options['limit'][] = $end;
}
$options['sort'] = "asc";
// check for null null for the sake of test compliance
if (func_num_args() == 5 && func_get_arg(3) == null && func_get_arg(4) == null){
return array();
}
return $this->sort($key,$options);
}
protected function sortAscAlpha($key,$pattern = null,$get = null,$start = null,$end = null) {
if ($key === null) {
return false;
}
$options = array();
if ($pattern != null) {
$options['by'] = $pattern;
}
if ($get !== null) {
$options['get'] = $get;
}
if ($start === null) {
$start = 0;
}
$options['limit'] = array();
$options['limit'][] = $start;
if ($end !== null) {
$options['limit'][] = $end;
}
$options['sort'] = "asc";
$options['alpha'] = true;
// check for null null for the sake of test compliance
if (func_num_args() == 5 && func_get_arg(3) == null && func_get_arg(4) == null){
return array();
}
return $this->sort($key,$options);
}
protected function sortDesc($key,$pattern = null,$get = null,$start = null,$end = null) {
if ($key === null) {
return false;
}
$options = array();
if ($pattern != null) {
$options['by'] = $pattern;
}
if ($get !== null) {
$options['get'] = $get;
}
if ($start === null) {
$start = 0;
}
$options['limit'] = array();
$options['limit'][] = $start;
if ($end !== null) {
$options['limit'][] = $end;
}
$options['sort'] = "desc";
// check for null null for the sake of test compliance
if (func_num_args() == 5 && func_get_arg(3) == null && func_get_arg(4) == null){
return array();
}
return $this->sort($key,$options);
}
protected function sortDescAlpha($key,$pattern = null,$get = null,$start = null,$end = null) {
if ($key === null) {
return false;
}
$options = array();
if ($pattern != null) {
$options['pattern'] = $pattern;
}
if ($get !== null) {
$options['get'] = $get;
}
if ($start === null) {
$start = 0;
}
$options['limit'] = array();
$options['limit'][] = $start;
if ($end !== null) {
$options['limit'][] = $end;
}
$options['sort'] = "desc";
$options['alpha'] = true;
// check for null null for the sake of test compliance
if (func_num_args() == 5 && func_get_arg(3) == null && func_get_arg(4) == null){
return array();
}
return $this->sort($key,$options);
}
/**
* Returns an associative array of strings and integers
* @throws RedisException
* @return array
*/
protected function info() {
try {
$response = $this->getPredis()->info();
return $response;
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the time to live left for a given key, in seconds. If the key doesn't exist, FALSE is returned.
* @param string $key
* @throws RedisException
* @return integer the time left to live in seconds.
*/
protected function ttl($key) {
try {
return $this->getPredis()->ttl($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Remove the expiration timer from a key.
* @param string $key
* @throws RedisException
* @return boolean TRUE if a timeout was removed, FALSE if the key didn’t exist or didn’t have an expiration timer.
*/
protected function persist($key) {
try {
return $this->getPredis()->persist($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Sets multiple key-value pairs in one atomic command.
* @param array $pairs key => value
* @throws RedisException
* @return boolean true in case of success, false in case of failure
*/
protected function mset($pairs) {
try {
return $this->getPredis()->mset($pairs);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Sets multiple key-value pairs in one atomic command only returns true if all keys were set.
* @param array $pairs key => value
* @throws RedisException
* @return boolean true in case of success, false in case of failure
*/
protected function msetnx($pairs) {
try {
return $this->getPredis()->msetnx($pairs);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Pops a value from the tail of a list, and pushes it to the front of another list. Also return this value.
* @param string $srcKey
* @param string $dstKey
* @throws RedisException
* @return string The element that was moved in case of success, FALSE in case of failure.
*/
protected function rpoplpush($srcKey,$dstKey) {
try {
$response = $this->getPredis()->rpoplpush($srcKey,$dstKey);
if (!$response) {
return false;
}
return $response;
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Adds the specified member with a given score to the sorted set stored at key.
* @param string $key
* @param float $score
* @param string $value
* @throws RedisException
* @return boolean whether the element was added or not
*/
protected function zAdd($key,$score,$value) {
try {
return $this->getPredis()->zadd($key,$score,$value) ? 1 : 0;
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns a range of elements from the ordered set stored at the specified key, with values in the range [start, end]. start and stop are interpreted as zero-based indices: 0 the first element, 1 the second ... -1 the last element, -2 the penultimate ...
* @param string $key
* @param integer $start
* @param integer $end
* @param boolean $withScores
* @throws RedisException
* @return array containing the values in specified range.
*/
protected function zRange($key,$start,$end,$withScores = false) {
try {
if ($withScores) {
$return = array();
foreach($this->getPredis()->zrange($key,$start,$end,"withscores") as $pair) {
$return[$pair[0]] = $pair[1];
}
return $return;
}
else {
return $this->getPredis()->zrange($key,$start,$end);
}
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Deletes a specified member from the ordered set.
* @param string $key
* @param string $member
* @throws RedisException
* @return boolean true on sucess, false on failure
*/
protected function zDelete($key,$member) {
try {
return $this->getPredis()->zrem($key,$member) ? 1 : 0;
}
catch (Exception $e) {
return 0; // throw new RedisException("Redis Error");
}
}
/**
* @see zDelete()
*/
protected function zRem($key,$member) {
$this->zDelete($key, $member);
}
/**
* @see zDelete()
*/
protected function zRemove($key,$member) {
$this->zDelete($key, $member);
}
/**
* Returns the elements of the sorted set stored at the specified key in the range [start, end] in reverse order. start and stop are interpretated as zero-based indices: 0 the first element, 1 the second ... -1 the last element, -2 the penultimate ...
* @param string $key
* @param integer $start
* @param integer $end
* @param boolean $withScores
* @throws RedisException
* @return array containing the values in specified range.
*/
protected function zRevRange($key,$start,$end,$withScores = false) {
try {
return $this->getPredis()->zrevrange($key,$start,$end,$withScores);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before start or end excludes it from the range. +inf and -inf are also valid limits.
* @param string $key
* @param integer $start
* @param integer $end
* @param array $options Two options are available: withscores => TRUE, and limit => array($offset, $count)
* @throws RedisException
* @return array containing the values in specified range.
*/
protected function zRangeByScore($key,$start,$end,$options = array()) {
try {
if (!is_array($options)) {
$options[strtolower($options)] = true;
}
if (isset($options['withscores']) && $options['withscores']) {
$return = array();
foreach($this->getPredis()->zrangebyscore($key,$start,$end,$options) as $pair) {
if (isset($pair[1])) {
$return[$pair[0]] = $pair[1];
}
else {
$return[] = $pair;
}
}
return $return;
}
return $this->getPredis()->zrangebyscore($key,$start,$end,$options);
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the elements of the sorted set stored at the specified key in reverse order which have scores in the range [start,end]. Adding a parenthesis before start or end excludes it from the range. +inf and -inf are also valid limits.
* @param string $key
* @param integer $start
* @param integer $end
* @param array $options Two options are available: withscores => TRUE, and limit => array($offset, $count)
* @throws RedisException
* @return array containing the values in specified range.
*/
protected function zRevRangeByScore($key,$start,$end,$options = array()) {
try {
return $this->getPredis()->zrevrangebyscore($key,$start,$end,$options);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the number of elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before start or end excludes it from the range. +inf and -inf are also valid limits.
* @param string $key
* @param integer $start
* @param integer $end
* @throws RedisException
* @return integer the size of a corresponding zRangeByScore.
*/
protected function zCount($key,$start,$end) {
try {
return $this->getPredis()->zcount($key,$start,$end);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Deletes the elements of the sorted set stored at the specified key which have scores in the range [start,end].
* @param string $key
* @param integer $start
* @param integer $end
* @throws RedisException
* @return integer The number of values deleted from the sorted set
*/
protected function zRemRangeByScore($key,$start,$end) {
try {
return $this->getPredis()->zremrangebyscore($key,$start,$end);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see zRemRangeByScore()
*/
protected function zDeleteRangeByScore($key,$start,$end) {
return $this->zRemRangeByScore($key, $start, $end);
}
/**
* Returns the cardinality of an ordered set.
* @param string $key
* @throws RedisException
* @return integer the set's cardinality
*/
protected function zSize($key) {
try {
return $this->getPredis()->zcard($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* @see zSize()
*/
protected function zCard($key) {
return $this->zSize($key);
}
/**
* Returns the score of a given member in the specified sorted set.
* @param string $key
* @param string $member
* @throws RedisException
* @return float the score
*/
protected function zScore($key,$member) {
try {
$response = $this->getPredis()->zscore($key,$member);
if ($response === null) {
return false;
}
return $response;
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the rank of a given member in the specified sorted set, starting at 0 for the item with the smallest score.
* @param string $key
* @param string $member
* @throws RedisException
* @return float the score
*/
protected function zRank($key,$member) {
try {
return $this->getPredis()->zrank($key,$member);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the rank of a given member in the specified sorted set, starting at 0 for the item with the highest score.
* @param string $key
* @param string $member
* @throws RedisException
* @return float the score
*/
protected function zRevRank($key,$member) {
try {
return $this->getPredis()->zrevrank($key,$member);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Increments the score of a member from a sorted set by a given amount.
* @param string $key
* @param float $value
* @param string $member
* @throws RedisException
* @return float the new value
*/
protected function zIncrBy($key,$value,$member) {
try {
return $this->getPredis()->zincrby($key,$value,$member);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Creates an union of sorted sets given in second argument. The result of the union will be stored in the sorted set defined by the first argument. The third optionnel argument defines weights to apply to the sorted sets in input. In this case, the weights will be multiplied by the score of each element in the sorted set before applying the aggregation. The forth argument defines the AGGREGATE option which specify how the results of the union are aggregated.
* @param string $keyOutput
* @param array $zsetKeys
* @param array $weights
* @param string $aggregateFunction Either "SUM", "MIN", or "MAX": defines the behaviour to use on duplicate entries during the zUnion.
* @throws RedisException
* @return integer The number of values in the new sorted set
*/
protected function zUnion($keyOutput,$zsetKeys,$weights = array(),$aggregateFunction = "SUM") {
try {
$options = array();
$params = array();
$params[0] = $keyOutput;
$params[1] = count($zsetKeys);
$params = array_merge($params,$zsetKeys);
if (count($weights)) {
$options['weights'] = $weights;
}
$options['aggregate'] = $aggregateFunction;
$params[] = $options;
return call_user_func_array(array($this->getPredis(),"zunionstore"), $params);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Creates an intersection of sorted sets given in second argument. The result of the union will be stored in the sorted set defined by the first argument. The third optionnel argument defines weights to apply to the sorted sets in input. In this case, the weights will be multiplied by the score of each element in the sorted set before applying the aggregation. The forth argument defines the AGGREGATE option which specify how the results of the union are aggregated.
* @param string $keyOutput
* @param array $zsetKeys
* @param array $weights
* @param string $aggregateFunction Either "SUM", "MIN", or "MAX": defines the behaviour to use on duplicate entries during the zUnion.
* @throws RedisException
* @return integer The number of values in the new sorted set
*/
protected function zInter($keyOutput,$zsetKeys,$weights = array(),$aggregateFunction = "SUM") {
try {
$options = array();
$params = array();
$params[0] = $keyOutput;
$params[1] = count($zsetKeys);
$params = array_merge($params,$zsetKeys);
if (count($weights)) {
$options['weights'] = $weights;
}
$options['aggregate'] = $aggregateFunction;
$params[] = $options;
return call_user_func_array(array($this->getPredis(),"zinterstore"), $params);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Adds a value to the hash stored at key. If this value is already in the hash, FALSE is returned.
* @param string $key
* @param string $hashKey
* @param string $value
* @throws RedisException
* @return 1 if value didn't exist and was added successfully, 0 if the value was already present and was replaced, FALSE if there was an error.
*/
protected function hSet($key,$hashKey,$value) {
try {
return $this->getPredis()->hset($key,$hashKey,$value) ? 1 : 0;
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Gets a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, FALSE is returned.
* @param string $key
* @param string $hashKey
* @throws RedisException
* @return string The value, if the command executed successfully FALSE in case of failure
*/
protected function hGet($key,$hashKey) {
try {
$response = $this->getPredis()->hget($key,$hashKey);
if ($response === null) {
return false;
}
return $response;
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the length of a hash, in number of items
* @param string $key
* @throws RedisException
* @return integer the number of items in the hash
*/
protected function hLen($key) {
try {
return $this->getPredis()->hlen($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Removes a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, FALSE is returned.
* @param string $key
* @param string $hashKey
* @throws RedisException
* @return boolean TRUE in case of success, FALSE in case of failure
*/
protected function hDel($key,$hashKey) {
try {
return $this->getPredis()->hdel($key,$hashKey);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the keys in a hash, as an array of strings.
* @param string $key
* @throws RedisException
* @return array An array of elements, the keys of the hash. This works like PHP's array_keys().
*/
protected function hKeys($key) {
try {
return $this->getPredis()->hkeys($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the values in a hash, as an array of strings.
* @param string $key
* @throws RedisException
* @return array An array of elements, the values of the hash. This works like PHP's array_values().
*/
protected function hVals($key) {
try {
return $this->getPredis()->hvals($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Returns the whole hash, as an array of strings indexed by strings.
* @param string $key
* @throws RedisException
* @return array An array of elements, the contents of the hash.
*/
protected function hGetAll($key) {
try {
return $this->getPredis()->hgetall($key);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Verify if the specified member exists in a key.
* @param string $key
* @param string $memberKey
* @throws RedisException
* @return boolean If the member exists in the hash table, return TRUE, otherwise return FALSE.
*/
protected function hExists($key,$memberKey) {
try {
return $this->getPredis()->hexists($key,$memberKey);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Increments the value of a member from a hash by a given amount.
* @param string $key
* @param string $member
* @param string $value
* @throws RedisException
* @return integer The new valu
*/
protected function hIncrBy($key,$member,$value) {
try {
return $this->getPredis()->hincrby($key,$member,$value);
}
catch (Exception $e) {
return false; // throw new RedisException("Redis Error");
}
}
/**
* Fills in a whole hash. Non-string values are converted to string, using the standard (string) cast. NULL values are stored as empty strings.
* @param string $key
* @param array $members key => value
* @throws RedisException
* @return boolean TRUE in case of success, FALSE in case of failure
*/
protected function hMSet($key,$members) {
try {
foreach($members as $i => $member) {
if (is_array($member)) {
$members[$i] = "Array";
}
elseif (is_object($member)) {
$members[$i] = "Object";
}
}
return $this->getPredis()->hmset($key,$members);
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
/**
* Retrieve the values associated to the specified fields in the hash.
* @param string $key
* @param array $memberKeys
* @throws RedisException
* @return array An array of elements, the values of the specified fields in the hash, with the hash keys as array keys.
*/
protected function hMGet($key,$memberKeys) {
try {
$response = array();
foreach($this->getPredis()->hmget($key,$memberKeys) as $n => $value) {
if ($value === null) {
$value = false;
}
$response[$memberKeys[$n]] = $value;
}
return $response;
}
catch (Exception $e) {
throw $e;
return false; // throw new RedisException("Redis Error");
}
}
public function __call($method,$params) {
#echo $method."\n";
$return = call_user_func_array(array($this,$method), $params);
if ($this->mode === self::ATOMIC) {
return $return;
}
// we're in multi / pipeline
return $this;
}
}
class RedisPredisException extends Exception {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment