Class: Dalli::Protocol::Meta::RequestFormatter
- Inherits:
-
Object
- Object
- Dalli::Protocol::Meta::RequestFormatter
- Defined in:
- lib/dalli/protocol/meta/request_formatter.rb
Overview
Class that encapsulates logic for formatting meta protocol requests to memcached.
Constant Summary collapse
- ALLOWED_STATS_ARGS =
[nil, '', 'items', 'slabs', 'settings', 'reset'].freeze
Class Method Summary collapse
- .cas_string(cas) ⇒ Object
- .flush(delay: nil, quiet: false) ⇒ Object
- .meta_arithmetic(key:, delta:, initial:, incr: true, cas: nil, ttl: nil, base64: false, quiet: false) ⇒ Object
- .meta_delete(key:, cas: nil, ttl: nil, base64: false, quiet: false) ⇒ Object
-
.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.
-
.meta_noop ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/ParameterLists rubocop:enable Metrics/PerceivedComplexity.
- .meta_set(key:, value:, bitflags: nil, cas: nil, ttl: nil, mode: :set, base64: false, quiet: false) ⇒ Object
- .mode_to_token(mode) ⇒ Object
- .parse_to_64_bit_int(val, default) ⇒ Object
- .stats(arg = nil) ⇒ Object
- .version ⇒ Object
Class Method Details
.cas_string(cas) ⇒ Object
105 106 107 108 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 105 def self.cas_string(cas) cas = parse_to_64_bit_int(cas, nil) cas.nil? || cas.zero? ? '' : " C#{cas}" end |
.flush(delay: nil, quiet: false) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 73 def self.flush(delay: nil, quiet: false) cmd = +'flush_all' cmd << " #{parse_to_64_bit_int(delay, 0)}" 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
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 49 def self.(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 # 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 |
.meta_delete(key:, cas: nil, ttl: nil, base64: false, quiet: false) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 40 def self.(key:, cas: nil, ttl: nil, base64: false, quiet: false) cmd = "md #{key}" cmd << ' b' if base64 cmd << cas_string(cas) 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/ParameterLists rubocop:disable Metrics/PerceivedComplexity
18 19 20 21 22 23 24 25 26 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 18 def self.(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_noop ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/ParameterLists rubocop:enable Metrics/PerceivedComplexity
65 66 67 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 65 def self. "mn#{TERMINATOR}" end |
.meta_set(key:, value:, bitflags: nil, cas: nil, ttl: nil, mode: :set, base64: false, quiet: false) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 28 def self.(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 << cas_string(cas) cmd << " T#{ttl}" if ttl cmd << " M#{mode_to_token(mode)}" cmd << ' q' if quiet cmd << TERMINATOR end |
.mode_to_token(mode) ⇒ Object
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 |
.parse_to_64_bit_int(val, default) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 110 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 |
.stats(arg = nil) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 82 def self.stats(arg = nil) raise ArgumentError, "Invalid stats argument: #{arg.inspect}" unless ALLOWED_STATS_ARGS.include?(arg) cmd = +'stats' cmd << " #{arg}" if arg && !arg.empty? cmd + TERMINATOR end |
.version ⇒ Object
69 70 71 |
# File 'lib/dalli/protocol/meta/request_formatter.rb', line 69 def self.version "version#{TERMINATOR}" end |