ThinkPHP implements the MemCache distributed cache function, thinkphpmemcache
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.