PHP Redis class Operations

Source: Internet
Author: User
Tags php redis


/********************************************************************************* * 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.