本文執行個體講述了thinkPHP實現MemCache分布式緩衝功能。分享給大家供大家參考,具體如下:
兩天在研究MemCache分布式緩衝的問題時,發現ThinkPHP其實並不支援分布式緩衝功能,這可以從官方提供的CacheMemcache.class.php檔案中看到:
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']);
不過不要緊,稍微修改下就行了,即
if(empty($options)) { $options = array ( 'timeout' => false, 'persistent' => false, 'servers'=>array( array('ip'=>'127.0.0.1','port'=>11211), array('ip'=>'127.0.0.1','port'=>11212), array('ip'=>'202.116.32.4','port'=>11211), ), );}//分散式處理函數$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']); }}
閑來無事,於是就在本機上啟動了兩個MemCache伺服器,順手編寫了一段簡單的監控代碼(隔一段時間自動重新整理一次),進行測試。如果探索服務器運行不正常,則使用PhpMailer自動發送一封Email到管理員郵箱。測試結果表明,兩台Memcache伺服器均工作正常,而另外一台虛假的伺服器當然是無法串連到的。哈哈,夠簡單的吧
更多關於thinkPHP相關內容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《smarty模板入門基礎教程》及《PHP模板技術總結》。
希望本文所述對大家基於ThinkPHP架構的PHP程式設計有所協助。