class Dalli::Protocol::Binary

Access point for a single Memcached server, accessed via Memcached’s binary protocol. Contains logic for managing connection state to the server (retries, etc), formatting requests to the server, and unpacking responses.

Constants

NOT_FOUND_EXPIRY

This allows us to special case a nil initial value, and handle it differently than a zero. This special value for expiry causes memcached to return a not found if the key doesn’t already exist, rather than setting the initial value

Public Instance Methods

response_processor() click to toggle source
# File lib/dalli/protocol/binary.rb, line 15
def response_processor
  @response_processor ||= ResponseProcessor.new(@connection_manager, @value_marshaller)
end

Private Instance Methods

add(key, value, ttl, options) click to toggle source
# File lib/dalli/protocol/binary.rb, line 59
def add(key, value, ttl, options)
  opkey = quiet? ? :addq : :add
  storage_req(opkey, key, value, ttl, 0, options)
end
append(key, value) click to toggle source

rubocop:enable Metrics/ParameterLists

# File lib/dalli/protocol/binary.rb, line 82
def append(key, value)
  opkey = quiet? ? :appendq : :append
  write_append_prepend opkey, key, value
end
cas(key) click to toggle source

TODO: This is confusing, as there’s a cas command in memcached and this isn’t it. Maybe rename? Maybe eliminate?

# File lib/dalli/protocol/binary.rb, line 47
def cas(key)
  req = RequestFormatter.standard_request(opkey: :get, key: key)
  write(req)
  response_processor.data_cas_response
end
decr(key, count, ttl, initial) click to toggle source

Arithmetic Commands

# File lib/dalli/protocol/binary.rb, line 106
def decr(key, count, ttl, initial)
  opkey = quiet? ? :decrq : :decr
  decr_incr opkey, key, count, ttl, initial
end
decr_incr(opkey, key, count, ttl, initial) click to toggle source
# File lib/dalli/protocol/binary.rb, line 123
def decr_incr(opkey, key, count, ttl, initial)
  expiry = initial ? TtlSanitizer.sanitize(ttl) : NOT_FOUND_EXPIRY
  initial ||= 0
  write(RequestFormatter.decr_incr_request(opkey: opkey, key: key,
                                           count: count, initial: initial, expiry: expiry))
  response_processor.decr_incr unless quiet?
end
delete(key, cas) click to toggle source

Delete Commands

# File lib/dalli/protocol/binary.rb, line 98
def delete(key, cas)
  opkey = quiet? ? :deleteq : :delete
  req = RequestFormatter.standard_request(opkey: opkey, key: key, cas: cas)
  write(req)
  response_processor.delete unless quiet?
end
flush(ttl = 0) click to toggle source

Other Commands

# File lib/dalli/protocol/binary.rb, line 132
def flush(ttl = 0)
  opkey = quiet? ? :flushq : :flush
  write(RequestFormatter.standard_request(opkey: opkey, ttl: ttl))
  response_processor.no_body_response unless quiet?
end
gat(key, ttl, options = nil) click to toggle source
# File lib/dalli/protocol/binary.rb, line 32
def gat(key, ttl, options = nil)
  ttl = TtlSanitizer.sanitize(ttl)
  req = RequestFormatter.standard_request(opkey: :gat, key: key, ttl: ttl)
  write(req)
  response_processor.get(cache_nils: cache_nils?(options))
end
get(key, options = nil) click to toggle source

Retrieval Commands

# File lib/dalli/protocol/binary.rb, line 22
def get(key, options = nil)
  req = RequestFormatter.standard_request(opkey: :get, key: key)
  write(req)
  response_processor.get(cache_nils: cache_nils?(options))
end
incr(key, count, ttl, initial) click to toggle source
# File lib/dalli/protocol/binary.rb, line 111
def incr(key, count, ttl, initial)
  opkey = quiet? ? :incrq : :incr
  decr_incr opkey, key, count, ttl, initial
end
noop() click to toggle source

Noop is a keepalive operation but also used to demarcate the end of a set of pipelined commands. We need to read all the responses at once.

# File lib/dalli/protocol/binary.rb, line 140
def noop
  write_noop
  response_processor.consume_all_responses_until_noop
end
prepend(key, value) click to toggle source
# File lib/dalli/protocol/binary.rb, line 87
def prepend(key, value)
  opkey = quiet? ? :prependq : :prepend
  write_append_prepend opkey, key, value
end
quiet_get_request(key) click to toggle source
# File lib/dalli/protocol/binary.rb, line 28
def quiet_get_request(key)
  RequestFormatter.standard_request(opkey: :getkq, key: key)
end
replace(key, value, ttl, cas, options) click to toggle source
# File lib/dalli/protocol/binary.rb, line 64
def replace(key, value, ttl, cas, options)
  opkey = quiet? ? :replaceq : :replace
  storage_req(opkey, key, value, ttl, cas, options)
end
reset_stats() click to toggle source
# File lib/dalli/protocol/binary.rb, line 151
def reset_stats
  write(RequestFormatter.standard_request(opkey: :stat, key: 'reset'))
  response_processor.reset
end
set(key, value, ttl, cas, options) click to toggle source

Storage Commands

# File lib/dalli/protocol/binary.rb, line 54
def set(key, value, ttl, cas, options)
  opkey = quiet? ? :setq : :set
  storage_req(opkey, key, value, ttl, cas, options)
end
stats(info = '') click to toggle source
# File lib/dalli/protocol/binary.rb, line 145
def stats(info = '')
  req = RequestFormatter.standard_request(opkey: :stat, key: info)
  write(req)
  response_processor.stats
end
storage_req(opkey, key, value, ttl, cas, options) click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/dalli/protocol/binary.rb, line 70
def storage_req(opkey, key, value, ttl, cas, options)
  (value, bitflags) = @value_marshaller.store(key, value, options)
  ttl = TtlSanitizer.sanitize(ttl)

  req = RequestFormatter.standard_request(opkey: opkey, key: key,
                                          value: value, bitflags: bitflags,
                                          ttl: ttl, cas: cas)
  write(req)
  response_processor.storage_response unless quiet?
end
touch(key, ttl) click to toggle source
# File lib/dalli/protocol/binary.rb, line 39
def touch(key, ttl)
  ttl = TtlSanitizer.sanitize(ttl)
  write(RequestFormatter.standard_request(opkey: :touch, key: key, ttl: ttl))
  response_processor.generic_response
end
version() click to toggle source
# File lib/dalli/protocol/binary.rb, line 156
def version
  write(RequestFormatter.standard_request(opkey: :version))
  response_processor.version
end
write_append_prepend(opkey, key, value) click to toggle source
# File lib/dalli/protocol/binary.rb, line 92
def write_append_prepend(opkey, key, value)
  write(RequestFormatter.standard_request(opkey: opkey, key: key, value: value))
  response_processor.no_body_response unless quiet?
end
write_noop() click to toggle source
# File lib/dalli/protocol/binary.rb, line 161
def write_noop
  req = RequestFormatter.standard_request(opkey: :noop)
  write(req)
end