Class: Dalli::Protocol::ResponseBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/dalli/protocol/response_buffer.rb

Overview

Manages the buffer for responses from memcached. Uses an offset-based approach to avoid string allocations when advancing through parsed responses.

Constant Summary collapse

COMPACT_THRESHOLD =

Compact the buffer when the consumed portion exceeds this threshold and represents more than half the buffer

4096

Instance Method Summary collapse

Constructor Details

#initialize(io_source, response_processor) ⇒ ResponseBuffer

Returns a new instance of ResponseBuffer.



18
19
20
21
22
23
# File 'lib/dalli/protocol/response_buffer.rb', line 18

def initialize(io_source, response_processor)
  @io_source = io_source
  @response_processor = response_processor
  @buffer = nil
  @offset = 0
end

Instance Method Details

#clearObject

Clear the internal response buffer



56
57
58
59
# File 'lib/dalli/protocol/response_buffer.rb', line 56

def clear
  @buffer = nil
  @offset = 0
end

#ensure_readyObject

Ensures the buffer is initialized for reading without discarding existing data. Used by interleaved pipelined get which may have already buffered partial responses during the send phase.



48
49
50
51
52
53
# File 'lib/dalli/protocol/response_buffer.rb', line 48

def ensure_ready
  return if in_progress?

  @buffer = ''.b
  @offset = 0
end

#in_progress?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/dalli/protocol/response_buffer.rb', line 61

def in_progress?
  !@buffer.nil?
end

#process_single_getk_responseObject

Attempts to process a single response from the buffer, advancing the offset past the consumed bytes.



31
32
33
34
35
36
# File 'lib/dalli/protocol/response_buffer.rb', line 31

def process_single_getk_response
  bytes, status, cas, key, value = @response_processor.getk_response_from_buffer(@buffer, @offset)
  @offset += bytes
  compact_if_needed
  [status, cas, key, value]
end

#readObject



25
26
27
# File 'lib/dalli/protocol/response_buffer.rb', line 25

def read
  @buffer << @io_source.read_nonblock
end

#resetObject

Resets the internal buffer to an empty state, so that we’re ready to read pipelined responses



40
41
42
43
# File 'lib/dalli/protocol/response_buffer.rb', line 40

def reset
  @buffer = ''.b
  @offset = 0
end