This article mainly introduces how thinkPHP implements the distributed cache function of MemCache, and analyzes thinkPHP by modifying CacheMemcache in the form of instances. class. php source files can be used to implement the distributed cache function. For more information, see the example in this article. 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.