国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

thinkPHP實現MemCache分布式緩存功能

原創(chuàng) 2017-01-06 13:22:37 267
摘要:本文實例講述了thinkPHP實現MemCache分布式緩存功能。分享給大家供大家參考,具體如下:兩天在研究MemCache分布式緩存的問題時,發(fā)現ThinkPHP其實并不支持分布式緩存功能,這可以從官方提供的CacheMemcache.class.php文件中看到:if(empty($options)) {   $options = array

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

兩天在研究MemCache分布式緩存的問題時,發(fā)現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服務器,順手編寫了一段簡單的監(jiān)控代碼(隔一段時間自動刷新一次),進行測試。如果發(fā)現服務器運行不正常,則使用PhpMailer自動發(fā)送一封Email到管理員郵箱。測試結果表明,兩臺Memcache服務器均工作正常,而另外一臺虛假的服務器當然是無法連接到的。哈哈,夠簡單的吧

更多關于thinkPHP實現MemCache分布式緩存功能請關注PHP中文網(www.miracleart.cn)其他文章!  

發(fā)布手記

熱門詞條