/********************************************************************************* * InitPHP 2.0 domestic PHP development Framework Dao-nosql-redis *-------------------------------------------------------------------------------* All rights reserved: Copyright by initphp.com * You are free to use the source code, but in the process of use, please keep the author information. Respect for the fruits of others ' work is respect for yourself *-------------------------------------------------------------------------------* $Author: Zhuli * $ Dtime:2011-10-09 ***********************************************************************************/Class Redisinit {private $redis;//redis Object/** * Initialize Redis * $config = Array (* ' Server ' = > ' 127.0.0.1 ' server * ' port ' = ' 6379 ' Port number *) * @param array $config */Public function I NIT ($config = Array ()) {if ($config [' server '] = = ') $config [' server '] = ' 127.0.0.1 '; if ($config [' port '] = = ') $config [' port '] = ' 6379 '; $this->redis = new Redis (); $this->redis->connect ($config [' Server '],$config [' Port ']; return $this->redis; }/** * Set value * @param string $key Key name * @param string|array $value Get the data * @param int $t Imeout time */Public function set ($key, $value, $timeOut = 0) {$value = Json_encode ($value, TRUE); $retRes = $this->redis->set ($key, $value); if ($timeOut > 0) $this->redis->settimeout ($key, $timeOut); return $retRes; /** * Gets data through key * @param string $key Key Name */Public function get ($key) {$result = $this->redis->get ($key); Return Json_decode ($result, TRUE); }/** * Delete a data * @param string $key Key Name */Public Function Delete ($key) {return $this->redis->delete ($key); }/** * Clear data */Public Function Flushall () {return $this->redis->flushall (); }/** * data into queue * @param string $key Key Name * @param string|array $value Get the data * @param bool $right from the right into the */public function push ($key, $value, $right = True) {$value = Json_encode ($value); Return $right? $this->redis->rpush ($key, $value): $this->redis->lpush ($key, $value); }/** * Data out queue * @param string $key Key name * @param bool $left whether to start data from the left */public func tion Pop ($key, $left = True) {$val = $left? $this->redis->lpop ($key): $this->redis->rpop ($key); Return Json_decode ($val); }/** * Data self-increment * @param string $key Key Name */Public function increment ($key) {retur n $this->redis->incr ($key); }/** * Data auto minus * @param string $key Key Name */Public function decrement ($key) {return $t HIS->REDIS->DECR ($key); }/** * Key exists, there is a return ture * @param string $key Key Name */publicfunction exists ($key) {return $this->redis->exists ($key); /** * Return Redis Object * Redis has a lot of ways to do it, we just encapsulate a part * Take this object and you can call the Redis own method directly */public functi On Redis () {return $this->redis; } }
PHP Redis class Operations