TP+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服务器均工作正常,而另外一台虚假的服务器当然是无法连接到的。哈哈,够简单的吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值