程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> thinkPHP實現MemCache分布式緩存功能,thinkphpmemcache

thinkPHP實現MemCache分布式緩存功能,thinkphpmemcache

編輯:關於PHP編程

thinkPHP實現MemCache分布式緩存功能,thinkphpmemcache


本文實例講述了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程序設計有所幫助。

您可能感興趣的文章:

  • thinkphp的靜態緩存用法分析
  • 淺析ThinkPHP緩存之快速緩存(F方法)和動態緩存(S方法)(日常整理)
  • Thinkphp關閉緩存的方法
  • ThinkPHP文件緩存類代碼分享
  • thinkphp緩存技術詳解
  • ThinkPHP實現一鍵清除緩存方法
  • 修改ThinkPHP緩存為Memcache的方法
  • ThinkPHP緩存方法S()概述
  • 采用ThinkPHP中F方法實現快速緩存實例
  • ThinkPHP靜態緩存簡單配置和使用方法詳解

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved