当前位置:Gxlcms > php框架 > thinkPHP实现MemCache分布式缓存功能

thinkPHP实现MemCache分布式缓存功能

时间:2021-07-01 10:21:17 帮助过:14人阅读

本文实例讲述了thinkPHP实现MemCache分布式缓存功能。分享给大家供大家参考,具体如下:

两天在研究MemCache分布式缓存的问题时,发现ThinkPHP其实并不支持分布式缓存功能,这可以从官方提供的CacheMemcache.class.php文件中看到:

  1. if(empty($options)) {
  2. $options = array
  3. (
  4. 'host' => '127.0.0.1',
  5. 'port' => 11211,
  6. 'timeout' => false,
  7. 'persistent' => false
  8. );
  9. }
  10. $func = $options['persistent'] ? 'pconnect' : 'connect';
  11. $this->expire = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
  12. $this->handler = new Memcache;
  13. $this->connected = $options['timeout'] === false ?
  14. $this->handler->$func($options['host'], $options['port']) :
  15. $this->handler->$func($options['host'], $options['port'], $options['timeout']);

不过不要紧,稍微修改下就行了,即

  1. if(empty($options)) {
  2. $options = array
  3. (
  4. 'timeout' => false,
  5. 'persistent' => false,
  6. 'servers'=>array(
  7. array('ip'=>'127.0.0.1','port'=>11211),
  8. array('ip'=>'127.0.0.1','port'=>11212),
  9. array('ip'=>'202.116.32.4','port'=>11211),
  10. ),
  11. );
  12. }
  13. //分布式处理函数
  14. $func="addServer";
  15. $this->expire = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
  16. $this->handler = new Memcache;
  17. if($options['timeout']===false)
  18. {
  19. foreach($options['servers'] as $server)
  20. {
  21. $this->handler->$func($server['ip'],$server['port']);
  22. }
  23. }

闲来无事,于是就在本机上启动了两个MemCache服务器,顺手编写了一段简单的监控代码(隔一段时间自动刷新一次),进行测试。如果发现服务器运行不正常,则使用PhpMailer自动发送一封Email到管理员邮箱。测试结果表明,两台Memcache服务器均工作正常,而另外一台虚假的服务器当然是无法连接到的。哈哈,够简单的吧

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

人气教程排行