lib/dalli/protocol/meta/request_formatter.rb in dalli-3.2.2 vs lib/dalli/protocol/meta/request_formatter.rb in dalli-3.2.3
- old
+ new
@@ -29,11 +29,11 @@
def self.meta_set(key:, value:, bitflags: nil, cas: nil, ttl: nil, mode: :set, base64: false, quiet: false)
cmd = "ms #{key} #{value.bytesize}"
cmd << ' c' unless %i[append prepend].include?(mode)
cmd << ' b' if base64
cmd << " F#{bitflags}" if bitflags
- cmd << " C#{cas}" if cas && !cas.zero?
+ cmd << cas_string(cas)
cmd << " T#{ttl}" if ttl
cmd << " M#{mode_to_token(mode)}"
cmd << ' q' if quiet
cmd << TERMINATOR
cmd << value
@@ -41,23 +41,24 @@
end
def self.meta_delete(key:, cas: nil, ttl: nil, base64: false, quiet: false)
cmd = "md #{key}"
cmd << ' b' if base64
- cmd << " C#{cas}" if cas && !cas.zero?
+ cmd << cas_string(cas)
cmd << " T#{ttl}" if ttl
cmd << ' q' if quiet
cmd + TERMINATOR
end
def self.meta_arithmetic(key:, delta:, initial:, incr: true, cas: nil, ttl: nil, base64: false, quiet: false)
cmd = "ma #{key} v"
cmd << ' b' if base64
cmd << " D#{delta}" if delta
cmd << " J#{initial}" if initial
- cmd << " C#{cas}" if cas && !cas.zero?
- cmd << " N#{ttl}" if ttl
+ # Always set a TTL if an initial value is specified
+ cmd << " N#{ttl || 0}" if ttl || initial
+ cmd << cas_string(cas)
cmd << ' q' if quiet
cmd << " M#{incr ? 'I' : 'D'}"
cmd + TERMINATOR
end
# rubocop:enable Metrics/CyclomaticComplexity
@@ -73,11 +74,11 @@
"version#{TERMINATOR}"
end
def self.flush(delay: nil, quiet: false)
cmd = +'flush_all'
- cmd << " #{delay}" if delay
+ cmd << " #{parse_to_64_bit_int(delay, 0)}" if delay
cmd << ' noreply' if quiet
cmd + TERMINATOR
end
def self.stats(arg = nil)
@@ -100,9 +101,21 @@
else
'S'
end
end
# rubocop:enable Metrics/MethodLength
+
+ def self.cas_string(cas)
+ cas = parse_to_64_bit_int(cas, nil)
+ cas.nil? || cas.zero? ? '' : " C#{cas}"
+ end
+
+ def self.parse_to_64_bit_int(val, default)
+ val.nil? ? nil : Integer(val)
+ rescue ArgumentError
+ # Sanitize to default if it isn't parsable as an integer
+ default
+ end
end
end
end
end