Class: Dalli::Protocol::Meta::RequestFormatter

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

Overview

Class that encapsulates logic for formatting meta protocol requests to memcached.

Class Method Summary collapse

Class Method Details

.flush(delay: nil, quiet: false) ⇒ Object



76
77
78
79
80
81
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 76

def self.flush(delay: nil, quiet: false)
  cmd = +'flush_all'
  cmd << " #{delay}" if delay
  cmd << ' noreply' if quiet
  cmd + TERMINATOR
end

.meta_arithmetic(key:, delta:, initial:, incr: true, cas: nil, ttl: nil, base64: false, quiet: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 52

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
  cmd << ' q' if quiet
  cmd << " M#{incr ? 'I' : 'D'}"
  cmd + TERMINATOR
end

.meta_delete(key:, cas: nil, ttl: nil, base64: false, quiet: false) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 43

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 << " T#{ttl}" if ttl
  cmd << ' q' if quiet
  cmd + TERMINATOR
end

.meta_get(key:, value: true, return_cas: false, ttl: nil, base64: false, quiet: false) ⇒ Object

Since these are string construction methods, we’re going to disable these Rubocop directives. We really can’t make this construction much simpler, and introducing an intermediate object seems like overkill.

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists rubocop:disable Metrics/PerceivedComplexity



19
20
21
22
23
24
25
26
27
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 19

def self.meta_get(key:, value: true, return_cas: false, ttl: nil, base64: false, quiet: false)
  cmd = "mg #{key}"
  cmd << ' v f' if value
  cmd << ' c' if return_cas
  cmd << ' b' if base64
  cmd << " T#{ttl}" if ttl
  cmd << ' k q s' if quiet # Return the key in the response if quiet
  cmd + TERMINATOR
end

.meta_noopObject

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength rubocop:enable Metrics/ParameterLists rubocop:enable Metrics/PerceivedComplexity



68
69
70
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 68

def self.meta_noop
  "mn#{TERMINATOR}"
end

.meta_set(key:, value:, bitflags: nil, cas: nil, ttl: nil, mode: :set, base64: false, quiet: false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 29

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 << " T#{ttl}" if ttl
  cmd << " M#{mode_to_token(mode)}"
  cmd << ' q' if quiet
  cmd << TERMINATOR
  cmd << value
  cmd + TERMINATOR
end

.mode_to_token(mode) ⇒ Object

rubocop:disable Metrics/MethodLength



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 90

def self.mode_to_token(mode)
  case mode
  when :add
    'E'
  when :replace
    'R'
  when :append
    'A'
  when :prepend
    'P'
  else
    'S'
  end
end

.stats(arg = nil) ⇒ Object



83
84
85
86
87
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 83

def self.stats(arg = nil)
  cmd = +'stats'
  cmd << " #{arg}" if arg
  cmd + TERMINATOR
end

.versionObject



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

def self.version
  "version#{TERMINATOR}"
end