ThinkPHP implements the MemCache distributed cache function

Source: Internet
Author: User
: This article describes how thinkPHP implements the MemCache distributed cache function. if you are interested in the PHP Tutorial, refer to it. This example describes how thinkPHP implements the distributed cache function of MemCache. We will share this with you for your reference. The details are as follows:

Two days after studying the distributed cache of MemCache, we found that ThinkPHP does not actually support the distributed cache function. this can be seen from the official CacheMemcache. class. php file:

if(empty($options)) {  $options = array  (    'host' => '127.0.0.1',    'port' => 11211,    'timeout' => false,    'persistent' => false  );}$func = $options['persistent'] ? 'pconnect' : 'connect';$this->expire = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');$this->handler = new Memcache;$this->connected = $options['timeout'] === false ?$this->handler->$func($options['host'], $options['port']) :$this->handler->$func($options['host'], $options['port'], $options['timeout']);

But it doesn't matter. just modify it a little, that is

If (empty ($ options) {$ options = array ('timeout' => false, 'persistent' => false, 'servers' => array ('IP' => '2017. 0.0.1 ', 'port' => 11211), array ('IP' => '100. 0.0.1 ', 'port' => 11212), array ('IP' => '100. 116.32.4 ', 'port' => 11211),),);} // distributed processing function $ func = "addServer "; $ this-> expire = isset ($ options ['expire '])? $ Options ['expire ']: C ('data _ CACHE_TIME'); $ this-> handler = new Memcache; if ($ options ['timeout'] = false) {foreach ($ options ['servers'] as $ server) {$ this-> handler-> $ func ($ server ['IP'], $ server ['port']) ;}}

So I started two MemCache servers on the machine, and compiled a simple monitoring code (automatically refresh once every time) for testing. If the server is abnormal, use PhpMailer to automatically send an Email to the administrator's mailbox. The test results show that both Memcache servers work normally, and the other fake server cannot be connected. Haha, that's simple enough.

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.