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

配置多个服务器,在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.php
publicfunctionget($key,$default=null)
{
//$this->store来自Illuminate\Cache\MemcachedStore
dump($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缓存的时候具体连接的哪个服务器呢?
米琪卡哇伊
浏览 438回答 2
2回答

临摹微笑

如果客户端不提供,那么没办法知道。memcached把key经过hash以后,结合weight等参数来选择实例,而且不同的客户端的hash函数还可能不一样。不知道你为什么需要知道具体来自哪个实例
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript