欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

thinkPHP實(shí)現(xiàn)MemCache分布式緩存功能

 更新時(shí)間:2016年03月23日 11:34:07   作者:haiwei.sun  
這篇文章主要介紹了thinkPHP實(shí)現(xiàn)MemCache分布式緩存功能的方法,結(jié)合實(shí)例形式分析了thinkPHP通過(guò)修改CacheMemcache.class.php源文件實(shí)現(xiàn)分布式緩存功能的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了thinkPHP實(shí)現(xiàn)MemCache分布式緩存功能。分享給大家供大家參考,具體如下:

兩天在研究MemCache分布式緩存的問(wèn)題時(shí),發(fā)現(xiàn)ThinkPHP其實(shí)并不支持分布式緩存功能,這可以從官方提供的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']);

不過(guò)不要緊,稍微修改下就行了,即

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),
    ),
  );
}
//分布式處理函數(shù)
$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']);
  }
}

閑來(lái)無(wú)事,于是就在本機(jī)上啟動(dòng)了兩個(gè)MemCache服務(wù)器,順手編寫了一段簡(jiǎn)單的監(jiān)控代碼(隔一段時(shí)間自動(dòng)刷新一次),進(jìn)行測(cè)試。如果發(fā)現(xiàn)服務(wù)器運(yùn)行不正常,則使用PhpMailer自動(dòng)發(fā)送一封Email到管理員郵箱。測(cè)試結(jié)果表明,兩臺(tái)Memcache服務(wù)器均工作正常,而另外一臺(tái)虛假的服務(wù)器當(dāng)然是無(wú)法連接到的。哈哈,夠簡(jiǎn)單的吧

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論