thinkPHP实现MemCache分布式缓存功能 thinkPHP实现MemCache分布式缓存功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

thinkPHP实现MemCache分布式缓存功能 thinkPHP实现MemCache分布式缓存功能

haiwei.sun   2021-03-23 我要评论
想了解thinkPHP实现MemCache分布式缓存功能的相关内容吗,haiwei.sun在本文为您仔细讲解thinkPHP实现MemCache分布式缓存功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:thinkPHP,MemCache,分布式,缓存,下面大家一起来学习吧。

本文实例讲述了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框架的PHP程序设计有所帮助。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们