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.

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



66
67
68
69
70
# File 'lib/dalli/protocol/response_buffer.rb', line 66

def clear
  @buffer&.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.



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

def ensure_ready
  return if in_progress?

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

#in_progress?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/dalli/protocol/response_buffer.rb', line 72

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.



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

def process_single_getk_response
  response = @response_processor.getk_response_from_buffer(@buffer, @offset)
  @offset += response.pop
  response
end

#readObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dalli/protocol/response_buffer.rb', line 25

def read
  remaining_bytes = @buffer.bytesize - @offset
  if remaining_bytes.zero?
    @offset = 0
    @buffer = @io_source.read_available(@buffer)
  else
    if @offset > COMPACT_THRESHOLD && @offset > (@buffer.bytesize / 2)
      @buffer.bytesplice(0, @offset, '')
      @offset = 0
    end
    @buffer << @io_source.read_available
  end
end

#resetObject

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



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

def reset
  @buffer&.clear
  @buffer ||= ''.b
  @offset = 0
end