账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    laravel memcached 具体连接的哪个服务器
    64
    0

    配置多个服务器,在 Illuminate\Cache\MemcachedConnector 使用addServer 添加进 memcached 具体见文档

    cat config/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...

    public function connect(array $servers)
    {
        $memcached = $this->getMemcached();
        // For each server in the array, we'll just extract the configuration and add
        // the server to the Memcached connection. Once we have added all of these
        // servers we'll verify the connection is successful and return it back.
        foreach ($servers as $server) {
            $memcached->addServer(
                $server['host'], $server['port'], $server['weight']
            );
        }
        $memcachedStatus = $memcached->getVersion();
        //Where a connection has failed to a server the version is returned as '255.255.255'.
        array:2 [▼
      "xxx:11211" => "1.5.5"
      "xxx:11222" => "1.5.5"
    ]
        if (! is_array($memcachedStatus)) {
            throw new RuntimeException('No Memcached servers added.');
        }
        if (in_array('255.255.255', $memcachedStatus) && count(array_unique($memcachedStatus)) === 1) {
            throw new RuntimeException('Could not establish Memcached connection.');
        }
        return $memcached;
    }

    cat Illuminate\Cache\Repository.php

    public function get($key, $default = null)
        {
        //$this->store 来自Illuminate\Cache\MemcachedStore 
      dump($this->store->getmemcached()->getstats());//返回config/cache.php memcached servers数组服务器信息  
            $value = $this->store->get($key);
    
            if (is_null($value))
            {
                $this->fireCacheEvent('missed', [$key]);
    
                $value = value($default);
            }
            else
            {
                $this->fireCacheEvent('hit', [$key, $value]);
            }
    
            return $value;
        }
    
    cat bootstarp/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 缓存的时候具体连接的哪个服务器呢?

    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部