PHP Operation Redis Caching method

Source: Internet
Author: User
Tags delete cache
  ObjectPrivate $sId = 1; Servier Service ID Private $con = null;//Link Resource/** * Initialize Redis * * @return Object */Public function __constr UCT () {if (!class_exists (' Redis ')) {throw new QException(' PHP extension does not exist:redis ');  } $this->redis = new Redis ();  }/** * Link MEMCAHCE service * * @access private * @param string $key keyword * @param string $value Cache contents * @return Array */Private Function Connect ($sid) {$file = $this->cachefile ();require$file; if (! isset ($cache)) {throw new QException(' Cache configuration file does not exist '. $file);    } $server = $cache [$this->cacheid]; $sid = Isset ($sid) = = 0? $this->sid: $sid;//memcache Service Select if (! $server [$sid]) {throw new QException(' The cache server configuration file for the current operation does not exist ');    } $host = $server [$sid] [' Host '];    $port = $server [$sid] [' Port '];    try {$this->redis->connect ($host, $port); } catch (Exception $e) {exit (' Memecache connection failed, error message: '. $e->getmessage ()); }}/** * Write Cache * * @access private * @param string $key keyword * @param string $value Cache contents * @return Arra    Y */Public function set ($key, $value, $sid, $expire = 0) {$data = $this->get ($key, $SID);//If a key value already exists    if ($data) {return $this->redis->getset ($key, $value);    } else {return $this->redis->set ($key, $value); }}/** * Read Cache * * @access private * @param string $key keyword * @param int $sid Select memcache Server * @re    Turn array */Public function get ($key, $sid) {$this->connect ($SID);  return $this->redis->get ($key); }/** * Clean (delete) All elements that have been stored * * @access Private * @return Array */Public function flush () {$this->co    Nnect ();  return $this->redis->flushall (); }/** * Delete cache * * @access private * @param string $key keyword * @param int $sid Select memcache Server * @return AR Ray  */Public Function remove ($key, $sid) {$this->connect ();  return $this->redis->del ($key); }/** * destructor * Last Close memcache */Public Function __destruct () {if ($this->redis) {$this->redi    S->close (); }  }}

The above describes the PHP operation of the Redis cache method, including Require,exception, object aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.