Class: Memcached::Client

Inherits:
Connection
  • Object
show all
Defined in:
lib/remcached/client.rb

Constant Summary

Constants inherited from Connection

Memcached::Connection::KEEPALIVE_INTERVAL, Memcached::Connection::RECEIVE_TIMEOUT, Memcached::Connection::RECONNECT_DELAY, Memcached::Connection::RECONNECT_JITTER

Instance Method Summary collapse

Methods inherited from Connection

connect, #connected?, #connection_completed, #keepalive, #receive_data, #reconnect, #send_packet

Instance Method Details

#post_initObject



93
94
95
96
97
# File 'lib/remcached/client.rb', line 93

def post_init
  super
  @opaque_counter = 0
  @pending = []
end

#receive_packet(response) ⇒ Object

memcached responses possess the same order as their corresponding requests. Therefore quiet requests that have not yielded responses will be dropped silently to free memory from @pending

When a callback has been fired and returned :proceed without a succeeding packet, we still keep it referenced around for commands such as STAT which has multiple response packets.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/remcached/client.rb', line 127

def receive_packet(response)
  pending_pos = nil
  pending_callback = nil
  @pending.each_with_index do |(pending_opaque,pending_cb),i|
    if response[:opaque] == pending_opaque
      pending_pos = i
      pending_callback = pending_cb
      break
    end
  end

  if pending_pos
    @pending = @pending[pending_pos..-1]
    begin
      if pending_callback.call(response) != :proceed
        @pending.shift
      end
    rescue Exception => e
      $stderr.puts "#{e.class}: #{e}\n" + e.backtrace.join("\n")
    end
  end
end

#send_keepaliveObject



150
151
152
# File 'lib/remcached/client.rb', line 150

def send_keepalive
  send_request Request::NoOp.new
end

#send_request(pkt, &callback) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/remcached/client.rb', line 107

def send_request(pkt, &callback)
  @opaque_counter += 1
  @opaque_counter %= 1 << 32
  pkt[:opaque] = @opaque_counter
  send_packet pkt

  if callback
    @pending << [@opaque_counter, callback]
  end
end

#stats(contents = {}, &callback) ⇒ Object

Callback will be called multiple times



155
156
157
158
159
160
161
162
163
# File 'lib/remcached/client.rb', line 155

def stats(contents={}, &callback)
  send_request Request::Stats.new(contents) do |result|
    callback.call result

    if result[:status] == Errors::NO_ERROR && result[:key] != ''
      :proceed
    end
  end
end

#unbindObject



99
100
101
102
103
104
105
# File 'lib/remcached/client.rb', line 99

def unbind
  super
  @pending.each do |opaque, callback|
    callback.call :status => Errors::DISCONNECTED
  end
  @pending = []
end