thinkPHP实现MemCache分布式缓存功能。具体如下:
两天在研究MemCache分布式缓存的问题时,发现ThinkPHP其实并不支持分布式缓存功能,这可以从官方提供的CacheMemcache.class.php文件中看到:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 = newMemcache;$this->connected = $options['timeout'] === false ?$this->handler->$func($options['host'], $options['port']) :$this->handler->$func($options['host'], $options['port'], $options['timeout']); | 
不过不要紧,稍微修改下就行了,即
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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 = newMemcache;if($options['timeout']===false){  foreach($options['servers'] as$server)  {    $this->handler->$func($server['ip'],$server['port']);  }} | 
闲来无事,于是就在本机上启动了两个MemCache服务器,顺手编写了一段简单的监控代码(隔一段时间自动刷新一次),进行测试。如果发现服务器运行不正常,则使用PhpMailer自动发送一封Email到管理员邮箱。测试结果表明,两台Memcache服务器均工作正常,而另外一台虚假的服务器当然是无法连接到的。
© 版权声明
本文刊载的所有内容,包括文字、图片、音频、视频、软件、程序、以及网页版式设计等部门来源于互联网,版权均归原作者所有!本网站提供的内容服务于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯本网站及相关权利人的合法权利。
联系信息:邮箱aoxolcom@163.com或见网站底部。
联系信息:邮箱aoxolcom@163.com或见网站底部。
THE END
    




















请登录后发表评论
注册
社交帐号登录