为了账号安全,请及时绑定邮箱和手机立即绑定

laravel memcached 具体连接的哪个服务器

laravel memcached 具体连接的哪个服务器

米琪卡哇伊 2019-05-12 09:10:35
配置多个服务器,在Illuminate\Cache\MemcachedConnector使用addServer添加进memcached具体见文档catconfig/cache.php'memcached'=>['driver'=>'memcached','servers'=>[['host'=>'127.0.0.1','port'=>11211,'weight'=>80],['host'=>'127.0.0.1','port'=>11212,'weight'=>20],],https://laravel-china.org/top...publicfunctionconnect(array$servers){$memcached=$this->getMemcached();//Foreachserverinthearray,we'lljustextracttheconfigurationandadd//theservertotheMemcachedconnection.Oncewehaveaddedallofthese//serverswe'llverifytheconnectionissuccessfulandreturnitback.foreach($serversas$server){$memcached->addServer($server['host'],$server['port'],$server['weight']);}$memcachedStatus=$memcached->getVersion();//Whereaconnectionhasfailedtoaservertheversionisreturnedas'255.255.255'.array:2[▼"xxx:11211"=>"1.5.5""xxx:11222"=>"1.5.5"]if(!is_array($memcachedStatus)){thrownewRuntimeException('NoMemcachedserversadded.');}if(in_array('255.255.255',$memcachedStatus)&&count(array_unique($memcachedStatus))===1){thrownewRuntimeException('CouldnotestablishMemcachedconnection.');}return$memcached;}catIlluminate\Cache\Repository.phppublicfunctionget($key,$default=null){//$this->store来自Illuminate\Cache\MemcachedStoredump($this->store->getmemcached()->getstats());//返回config/cache.phpmemcachedservers数组服务器信息$value=$this->store->get($key);if(is_null($value)){$this->fireCacheEvent('missed',[$key]);$value=value($default);}else{$this->fireCacheEvent('hit',[$key,$value]);}return$value;}catbootstarp/app.php$app['events']->listen('cache.write',function($key,$value,$time)use($app){dump(app('cache')->store(),app('cache.store')->getStore()->getMemcached()->getstats());});测试echo\Cache::get('test');//调用函数里$this->store->getmemcached()->getstats()输出的还是2个服务器信息["127.0.0.1:11221"=>[],"127.0.0.1:11222"=>[],],怎么知道获取test缓存的时候具体连接的哪个服务器呢?
查看完整描述

2 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

如果客户端不提供,那么没办法知道。memcached把key经过hash以后,结合weight等参数来选择实例,而且不同的客户端的hash函数还可能不一样。不知道你为什么需要知道具体来自哪个实例
                            
查看完整回答
反对 回复 2019-05-12
  • 2 回答
  • 0 关注
  • 437 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信