无法使用dalli从memcached中读取密钥

时间:2015-07-17 07:39:52

标签: ruby memcached telnet dalli

dalli拒绝从远程memcached服务器进行连接和读取,但telnet工作正常。

我已通过SSH连接到远程服务器,并将memcached端口转发到我的机器上,如下所示:

Host access.production
  HostName 1.2.3.4
  LocalForward 10001 9.8.7.6:11211

现在我尝试使用memcachedtelnet读取密钥,它可以正常工作:

$ telnet localhost 10001
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

但是,使用dalli的以下Ruby脚本将失败:

require 'dalli'

client = Dalli::Client.new('localhost:10001')
client.get('my_key')

为什么dalli无法连接到memcached,而telnet - 会?

更新即可。 dalli返回此错误:

I, [2015-07-17T11:22:36.839807 #4286]  INFO -- : localhost:10001 failed (count: 0) Timeout::Error: IO timeout: {:host=>"localhost", :port=>10001, :down_retry_delay=>1, :socket_timeout=>0.5, :socket_max_failures=>2, :socket_failure_delay=>0.01, :value_max_bytes=>1048576, :compressor=>Dalli::Compressor, :compression_min_size=>1024, :compression_max_size=>false, :serializer=>Marshal, :username=>nil, :password=>nil, :keepalive=>true}
/Users/gmile/.rvm/gems/ruby-2.1.4@portal/gems/dalli-2.7.2/lib/dalli/ring.rb:45:in `server_for_key': No server available (Dalli::RingError)
    from /Users/gmile/.rvm/gems/ruby-2.1.4@portal/gems/dalli-2.7.2/lib/dalli/client.rb:328:in `perform'
    from /Users/gmile/.rvm/gems/ruby-2.1.4@portal/gems/dalli-2.7.2/lib/dalli/client.rb:53:in `get'
    from dali_all_keys.rb:4:in `<main>'

1 个答案:

答案 0 :(得分:0)

默认Dalli超时为500毫秒。鉴于memcache响应时间通常是一位数毫秒,并且您将memcache实例放在靠近服务器的地方,这通常是充足的。

另一方面,鉴于您通过ssh隧道连接,您可能会看到更大的延迟,您可能希望增加超时:

 Dalli::Client.new("127.0.0.1:10001", socket_timeout: 2.0)