Organizing PHP operations Memcache Cache base method

Source: Internet
Author: User

PHP operation Memcache Caching method sharing


The premise of using Memcache is to configure the Memcahche environment on the service side first! Confirm that the MEMCAHCE can be properly connected and can be used in the program!


<?php/** * Memcache Cache operation * @update http://www.lai18.com */class Mcache extends Object implements cacheface{private $me m = null;  Mem Object Private $sId = 1; Servier Service ID/** * Initialize memcache * * @return Object */Public Function __construct () {if (!class_exists (    ' Memcache ') {throw new qexception (' PHP extension does not exist:memcache ');  } $this->mem = new Memcache (); }/** * Link MEMCAHCE service * * @access private * @param string $key keyword * @param string $value Cache contents * @return    Array * @update http://www.lai18.com */Private Function Connect ($sid) {$file = $this->cachefile ();    Require $file;    if (! isset ($cache)) {throw new qexception (' cache config 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 (' Cache server configuration file for current operation does not exist ');    } $host = $server [$sid] [' Host ']; $port = $server [$sid] ' PORT '];    try {$this->mem->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 Array    */Public function set ($key, $value, $sid, $expire = 0) {$data = $this->get ($key, $SID);//If a key value already exists    if ($data) {return $this->mem->set ($key, $value, memcache_compressed, $expire);    } else {return $this->mem->add ($key, $value, memcache_compressed, $expire); }}/** * Read Cache * * @access private * @param string $key keyword * @param int $sid Select memcache Server * @ret    Urn Array */public function get ($key, $sid) {$this->connect ($SID);  return $this->mem->get ($key); }/** * Clean (delete) All elements that have been stored * * @access Private * @return Array */Public function flush () {$this->con    Nect (); return $this->mem->flush (); }/** * 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->mem->delete ($key); }/** * destructor * Last Close memcache */Public Function __destruct () {/*if (! $this->mem) {$this->mem    ->close (); }*/  }}


Organizing PHP operations Memcache Cache base method

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.