dalli 3.2.8 → 5.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +305 -0
  3. data/Gemfile +16 -2
  4. data/README.md +96 -2
  5. data/lib/dalli/client.rb +289 -28
  6. data/lib/dalli/flags.rb +15 -0
  7. data/lib/dalli/instrumentation.rb +153 -0
  8. data/lib/dalli/key_manager.rb +23 -8
  9. data/lib/dalli/options.rb +1 -1
  10. data/lib/dalli/pid_cache.rb +1 -1
  11. data/lib/dalli/pipelined_deleter.rb +82 -0
  12. data/lib/dalli/pipelined_getter.rb +44 -20
  13. data/lib/dalli/pipelined_setter.rb +87 -0
  14. data/lib/dalli/protocol/base.rb +95 -25
  15. data/lib/dalli/protocol/connection_manager.rb +37 -14
  16. data/lib/dalli/protocol/{meta/key_regularizer.rb → key_regularizer.rb} +1 -1
  17. data/lib/dalli/protocol/meta.rb +185 -20
  18. data/lib/dalli/protocol/{meta/request_formatter.rb → request_formatter.rb} +49 -16
  19. data/lib/dalli/protocol/response_buffer.rb +36 -12
  20. data/lib/dalli/protocol/{meta/response_processor.rb → response_processor.rb} +93 -37
  21. data/lib/dalli/protocol/server_config_parser.rb +2 -2
  22. data/lib/dalli/protocol/string_marshaller.rb +65 -0
  23. data/lib/dalli/protocol/ttl_sanitizer.rb +1 -1
  24. data/lib/dalli/protocol/value_compressor.rb +3 -16
  25. data/lib/dalli/protocol/value_marshaller.rb +1 -1
  26. data/lib/dalli/protocol/value_serializer.rb +61 -44
  27. data/lib/dalli/protocol.rb +10 -0
  28. data/lib/dalli/ring.rb +2 -2
  29. data/lib/dalli/servers_arg_normalizer.rb +1 -1
  30. data/lib/dalli/socket.rb +79 -14
  31. data/lib/dalli/version.rb +2 -2
  32. data/lib/dalli.rb +13 -5
  33. data/lib/rack/session/dalli.rb +43 -8
  34. metadata +27 -17
  35. data/lib/dalli/protocol/binary/request_formatter.rb +0 -117
  36. data/lib/dalli/protocol/binary/response_header.rb +0 -36
  37. data/lib/dalli/protocol/binary/response_processor.rb +0 -239
  38. data/lib/dalli/protocol/binary/sasl_authentication.rb +0 -60
  39. data/lib/dalli/protocol/binary.rb +0 -173
  40. data/lib/dalli/server.rb +0 -6
data/lib/dalli/socket.rb CHANGED
@@ -52,7 +52,7 @@ module Dalli
52
52
 
53
53
  FILTERED_OUT_OPTIONS = %i[username password].freeze
54
54
  def logged_options
55
- options.reject { |k, _| FILTERED_OUT_OPTIONS.include? k }
55
+ options.except(*FILTERED_OUT_OPTIONS)
56
56
  end
57
57
  end
58
58
 
@@ -63,6 +63,7 @@ module Dalli
63
63
  ##
64
64
  class SSLSocket < ::OpenSSL::SSL::SSLSocket
65
65
  include Dalli::Socket::InstanceMethods
66
+
66
67
  def options
67
68
  io.options
68
69
  end
@@ -85,9 +86,16 @@ module Dalli
85
86
  ##
86
87
  class TCP < TCPSocket
87
88
  include Dalli::Socket::InstanceMethods
89
+
88
90
  # options - supports enhanced logging in the case of a timeout
89
91
  attr_accessor :options
90
92
 
93
+ # Expected parameter signature for unmodified TCPSocket#initialize.
94
+ # Used to detect when gems like socksify or resolv-replace have monkey-patched
95
+ # TCPSocket, which breaks the connect_timeout: keyword argument.
96
+ TCPSOCKET_NATIVE_PARAMETERS = [[:rest]].freeze
97
+ private_constant :TCPSOCKET_NATIVE_PARAMETERS
98
+
91
99
  def self.open(host, port, options = {})
92
100
  create_socket_with_timeout(host, port, options) do |sock|
93
101
  sock.options = { host: host, port: port }.merge(options)
@@ -97,15 +105,25 @@ module Dalli
97
105
  end
98
106
  end
99
107
 
108
+ # Detect and cache whether TCPSocket supports the connect_timeout: keyword argument.
109
+ # Returns true for an unmodified TCPSocket on Ruby 3.0+, or for resolv-replace >= 0.2.0
110
+ # which forwards keyword arguments through its patch.
111
+ # Returns false when monkey-patched by gems like socksify or resolv-replace < 0.2.0.
112
+ # rubocop:disable ThreadSafety/ClassInstanceVariable
113
+ def self.supports_connect_timeout?
114
+ return @supports_connect_timeout if defined?(@supports_connect_timeout)
115
+
116
+ @supports_connect_timeout = RUBY_ENGINE == 'ruby' && RUBY_VERSION >= '3.0' &&
117
+ ::TCPSocket.instance_method(:initialize).parameters.then do |params|
118
+ params == TCPSOCKET_NATIVE_PARAMETERS || params.any? do |type, _|
119
+ type == :keyrest
120
+ end
121
+ end
122
+ end
123
+ # rubocop:enable ThreadSafety/ClassInstanceVariable
124
+
100
125
  def self.create_socket_with_timeout(host, port, options)
101
- # Check that TCPSocket#initialize was not overwritten by resolv-replace gem
102
- # (part of ruby standard library since 3.0.0, should be removed in 3.4.0),
103
- # as it does not handle keyword arguments correctly.
104
- # To check this we are using the fact that resolv-replace
105
- # aliases TCPSocket#initialize method to #original_resolv_initialize.
106
- # https://github.com/ruby/resolv-replace/blob/v0.1.1/lib/resolv-replace.rb#L21
107
- if RUBY_VERSION >= '3.0' &&
108
- !::TCPSocket.private_instance_methods.include?(:original_resolv_initialize)
126
+ if supports_connect_timeout?
109
127
  sock = new(host, port, connect_timeout: options[:socket_timeout])
110
128
  yield(sock)
111
129
  else
@@ -117,19 +135,59 @@ module Dalli
117
135
  end
118
136
 
119
137
  def self.init_socket_options(sock, options)
138
+ configure_tcp_options(sock, options)
139
+ configure_socket_buffers(sock, options)
140
+ configure_timeout(sock, options)
141
+ end
142
+
143
+ def self.configure_tcp_options(sock, options)
120
144
  sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
121
145
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_KEEPALIVE, true) if options[:keepalive]
146
+ end
147
+
148
+ def self.configure_socket_buffers(sock, options)
122
149
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, options[:rcvbuf]) if options[:rcvbuf]
123
150
  sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options[:sndbuf]) if options[:sndbuf]
151
+ end
124
152
 
153
+ def self.configure_timeout(sock, options)
125
154
  return unless options[:socket_timeout]
126
155
 
127
- seconds, fractional = options[:socket_timeout].divmod(1)
128
- microseconds = fractional * 1_000_000
129
- timeval = [seconds, microseconds].pack('l_2')
156
+ if sock.respond_to?(:timeout=)
157
+ # Ruby 3.2+ has IO#timeout for reliable cross-platform timeout handling
158
+ sock.timeout = options[:socket_timeout]
159
+ else
160
+ # Ruby 3.1 fallback using socket options
161
+ # struct timeval has architecture-dependent sizes (time_t, suseconds_t)
162
+ seconds, fractional = options[:socket_timeout].divmod(1)
163
+ microseconds = (fractional * 1_000_000).to_i
164
+ timeval = pack_timeval(sock, seconds, microseconds)
165
+
166
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeval)
167
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeval)
168
+ end
169
+ end
130
170
 
131
- sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeval)
132
- sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeval)
171
+ # Pack formats for struct timeval across architectures.
172
+ # Uses fixed-size formats for JRuby compatibility (JRuby doesn't support _ modifier on q).
173
+ # - ll: 8 bytes (32-bit time_t, 32-bit suseconds_t)
174
+ # - qq: 16 bytes (64-bit time_t, 64-bit suseconds_t or padded 32-bit)
175
+ TIMEVAL_PACK_FORMATS = %w[ll qq].freeze
176
+ TIMEVAL_TEST_VALUES = [0, 0].freeze
177
+
178
+ # Detect and cache the correct pack format for struct timeval on this platform.
179
+ # Different architectures have different sizes for time_t and suseconds_t.
180
+ # rubocop:disable ThreadSafety/ClassInstanceVariable
181
+ def self.timeval_pack_format(sock)
182
+ @timeval_pack_format ||= begin
183
+ expected_size = sock.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO).data.bytesize
184
+ TIMEVAL_PACK_FORMATS.find { |fmt| TIMEVAL_TEST_VALUES.pack(fmt).bytesize == expected_size } || 'll'
185
+ end
186
+ end
187
+ # rubocop:enable ThreadSafety/ClassInstanceVariable
188
+
189
+ def self.pack_timeval(sock, seconds, microseconds)
190
+ [seconds, microseconds].pack(timeval_pack_format(sock))
133
191
  end
134
192
 
135
193
  def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)
@@ -168,9 +226,16 @@ module Dalli
168
226
  Timeout.timeout(options[:socket_timeout]) do
169
227
  sock = new(path)
170
228
  sock.options = { path: path }.merge(options)
229
+ init_socket_options(sock, options)
171
230
  sock
172
231
  end
173
232
  end
233
+
234
+ def self.init_socket_options(sock, options)
235
+ # https://man7.org/linux/man-pages/man7/unix.7.html
236
+ sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options[:sndbuf]) if options[:sndbuf]
237
+ sock.timeout = options[:socket_timeout] if options[:socket_timeout] && sock.respond_to?(:timeout=)
238
+ end
174
239
  end
175
240
  end
176
241
  end
data/lib/dalli/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dalli
4
- VERSION = '3.2.8'
4
+ VERSION = '5.0.5'
5
5
 
6
- MIN_SUPPORTED_MEMCACHED_VERSION = '1.4'
6
+ MIN_SUPPORTED_MEMCACHED_VERSION = '1.6'
7
7
  end
data/lib/dalli.rb CHANGED
@@ -4,8 +4,6 @@
4
4
  # Namespace for all Dalli code.
5
5
  ##
6
6
  module Dalli
7
- autoload :Server, 'dalli/server'
8
-
9
7
  # generic error
10
8
  class DalliError < RuntimeError; end
11
9
 
@@ -27,6 +25,12 @@ module Dalli
27
25
  # operation is not permitted in a multi block
28
26
  class NotPermittedMultiOpError < DalliError; end
29
27
 
28
+ # raised when Memcached response with a SERVER_ERROR
29
+ class ServerError < DalliError; end
30
+
31
+ # socket/server communication error that can be retried
32
+ class RetryableNetworkError < NetworkError; end
33
+
30
34
  # Implements the NullObject pattern to store an application-defined value for 'Key not found' responses.
31
35
  class NilObject; end # rubocop:disable Lint/EmptyClass
32
36
  NOT_FOUND = NilObject.new
@@ -34,7 +38,7 @@ module Dalli
34
38
  QUIET = :dalli_multi
35
39
 
36
40
  def self.logger
37
- @logger ||= rails_logger || default_logger
41
+ @logger ||= rails_logger || default_logger # rubocop:disable ThreadSafety/ClassInstanceVariable
38
42
  end
39
43
 
40
44
  def self.rails_logger
@@ -50,20 +54,23 @@ module Dalli
50
54
  end
51
55
 
52
56
  def self.logger=(logger)
53
- @logger = logger
57
+ @logger = logger # rubocop:disable ThreadSafety/ClassInstanceVariable
54
58
  end
55
59
  end
56
60
 
57
61
  require_relative 'dalli/version'
62
+ require_relative 'dalli/instrumentation'
58
63
 
64
+ require_relative 'dalli/flags'
59
65
  require_relative 'dalli/compressor'
60
66
  require_relative 'dalli/client'
61
67
  require_relative 'dalli/key_manager'
62
68
  require_relative 'dalli/pipelined_getter'
69
+ require_relative 'dalli/pipelined_setter'
70
+ require_relative 'dalli/pipelined_deleter'
63
71
  require_relative 'dalli/ring'
64
72
  require_relative 'dalli/protocol'
65
73
  require_relative 'dalli/protocol/base'
66
- require_relative 'dalli/protocol/binary'
67
74
  require_relative 'dalli/protocol/connection_manager'
68
75
  require_relative 'dalli/protocol/meta'
69
76
  require_relative 'dalli/protocol/response_buffer'
@@ -71,6 +78,7 @@ require_relative 'dalli/protocol/server_config_parser'
71
78
  require_relative 'dalli/protocol/ttl_sanitizer'
72
79
  require_relative 'dalli/protocol/value_compressor'
73
80
  require_relative 'dalli/protocol/value_marshaller'
81
+ require_relative 'dalli/protocol/string_marshaller'
74
82
  require_relative 'dalli/protocol/value_serializer'
75
83
  require_relative 'dalli/servers_arg_normalizer'
76
84
  require_relative 'dalli/socket'
@@ -9,6 +9,10 @@ module Rack
9
9
  module Session
10
10
  # Rack::Session::Dalli provides memcached based session management.
11
11
  class Dalli < Abstract::PersistedSecure
12
+ class MissingSessionError < StandardError; end
13
+
14
+ RACK_SESSION_PERSISTED = 'rack.session.persisted'
15
+
12
16
  attr_reader :data
13
17
 
14
18
  # Don't freeze this until we fix the specs/implementation
@@ -70,23 +74,37 @@ module Rack
70
74
  @data = build_data_source(options)
71
75
  end
72
76
 
73
- def find_session(_req, sid)
77
+ def call(*_args)
78
+ super
79
+ rescue MissingSessionError
80
+ [401, {}, ['Wrong session ID']]
81
+ end
82
+
83
+ def find_session(req, sid)
74
84
  with_dalli_client([nil, {}]) do |dc|
75
85
  existing_session = existing_session_for_sid(dc, sid)
76
- return [sid, existing_session] unless existing_session.nil?
86
+ if existing_session.nil?
87
+ sid = create_sid_with_empty_session(dc)
88
+ existing_session = {}
89
+ end
77
90
 
78
- [create_sid_with_empty_session(dc), {}]
91
+ update_session_persisted_data(req, { id: sid })
92
+ return [sid, existing_session]
79
93
  end
80
94
  end
81
95
 
82
- def write_session(_req, sid, session, options)
96
+ def write_session(req, sid, session, options)
83
97
  return false unless sid
84
98
 
85
99
  key = memcached_key_from_sid(sid)
86
100
  return false unless key
87
101
 
88
102
  with_dalli_client(false) do |dc|
89
- dc.set(memcached_key_from_sid(sid), session, ttl(options[:expire_after]))
103
+ write_session_safely!(
104
+ dc, sid, session_persisted_data(req),
105
+ write_args: [memcached_key_from_sid(sid), session, ttl(options[:expire_after])]
106
+ )
107
+
90
108
  sid
91
109
  end
92
110
  end
@@ -139,12 +157,21 @@ module Rack
139
157
  ::Dalli::Client.new(server_configurations, client_options)
140
158
  else
141
159
  ensure_connection_pool_added!
142
- ConnectionPool.new(pool_options) do
160
+ ConnectionPool.new(**pool_options) do
143
161
  ::Dalli::Client.new(server_configurations, client_options.merge(threadsafe: false))
144
162
  end
145
163
  end
146
164
  end
147
165
 
166
+ def write_session_safely!(dalli_client, sid, persisted_data, write_args:)
167
+ if persisted_data && persisted_data[:id] == sid # That means that we update the existing session
168
+ # Override the session only if it still exists in the store!
169
+ raise MissingSessionError unless dalli_client.replace(*write_args)
170
+ else
171
+ dalli_client.set(*write_args)
172
+ end
173
+ end
174
+
148
175
  def extract_dalli_options(options)
149
176
  raise 'Rack::Session::Dalli no longer supports the :cache option.' if options[:cache]
150
177
 
@@ -175,8 +202,8 @@ module Rack
175
202
  raise e
176
203
  end
177
204
 
178
- def with_dalli_client(result_on_error = nil, &block)
179
- @data.with(&block)
205
+ def with_dalli_client(result_on_error = nil, &)
206
+ @data.with(&)
180
207
  rescue ::Dalli::DalliError, Errno::ECONNREFUSED
181
208
  raise if $ERROR_INFO.message.include?('undefined class')
182
209
 
@@ -190,6 +217,14 @@ module Rack
190
217
  def ttl(expire_after)
191
218
  expire_after.nil? ? 0 : expire_after + 1
192
219
  end
220
+
221
+ def session_persisted_data(req)
222
+ req.get_header RACK_SESSION_PERSISTED
223
+ end
224
+
225
+ def update_session_persisted_data(req, data)
226
+ req.set_header RACK_SESSION_PERSISTED, data
227
+ end
193
228
  end
194
229
  end
195
230
  end
metadata CHANGED
@@ -1,16 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalli
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.8
4
+ version: 5.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter M. Goldstein
8
8
  - Mike Perham
9
- autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2024-02-12 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 1980-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
14
27
  description: High performance memcached client for Ruby
15
28
  email:
16
29
  - peter.m.goldstein@gmail.com
@@ -27,30 +40,29 @@ files:
27
40
  - lib/dalli/cas/client.rb
28
41
  - lib/dalli/client.rb
29
42
  - lib/dalli/compressor.rb
43
+ - lib/dalli/flags.rb
44
+ - lib/dalli/instrumentation.rb
30
45
  - lib/dalli/key_manager.rb
31
46
  - lib/dalli/options.rb
32
47
  - lib/dalli/pid_cache.rb
48
+ - lib/dalli/pipelined_deleter.rb
33
49
  - lib/dalli/pipelined_getter.rb
50
+ - lib/dalli/pipelined_setter.rb
34
51
  - lib/dalli/protocol.rb
35
52
  - lib/dalli/protocol/base.rb
36
- - lib/dalli/protocol/binary.rb
37
- - lib/dalli/protocol/binary/request_formatter.rb
38
- - lib/dalli/protocol/binary/response_header.rb
39
- - lib/dalli/protocol/binary/response_processor.rb
40
- - lib/dalli/protocol/binary/sasl_authentication.rb
41
53
  - lib/dalli/protocol/connection_manager.rb
54
+ - lib/dalli/protocol/key_regularizer.rb
42
55
  - lib/dalli/protocol/meta.rb
43
- - lib/dalli/protocol/meta/key_regularizer.rb
44
- - lib/dalli/protocol/meta/request_formatter.rb
45
- - lib/dalli/protocol/meta/response_processor.rb
56
+ - lib/dalli/protocol/request_formatter.rb
46
57
  - lib/dalli/protocol/response_buffer.rb
58
+ - lib/dalli/protocol/response_processor.rb
47
59
  - lib/dalli/protocol/server_config_parser.rb
60
+ - lib/dalli/protocol/string_marshaller.rb
48
61
  - lib/dalli/protocol/ttl_sanitizer.rb
49
62
  - lib/dalli/protocol/value_compressor.rb
50
63
  - lib/dalli/protocol/value_marshaller.rb
51
64
  - lib/dalli/protocol/value_serializer.rb
52
65
  - lib/dalli/ring.rb
53
- - lib/dalli/server.rb
54
66
  - lib/dalli/servers_arg_normalizer.rb
55
67
  - lib/dalli/socket.rb
56
68
  - lib/dalli/version.rb
@@ -62,7 +74,6 @@ metadata:
62
74
  bug_tracker_uri: https://github.com/petergoldstein/dalli/issues
63
75
  changelog_uri: https://github.com/petergoldstein/dalli/blob/main/CHANGELOG.md
64
76
  rubygems_mfa_required: 'true'
65
- post_install_message:
66
77
  rdoc_options: []
67
78
  require_paths:
68
79
  - lib
@@ -70,15 +81,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
81
  requirements:
71
82
  - - ">="
72
83
  - !ruby/object:Gem::Version
73
- version: '2.6'
84
+ version: '3.3'
74
85
  required_rubygems_version: !ruby/object:Gem::Requirement
75
86
  requirements:
76
87
  - - ">="
77
88
  - !ruby/object:Gem::Version
78
89
  version: '0'
79
90
  requirements: []
80
- rubygems_version: 3.5.6
81
- signing_key:
91
+ rubygems_version: 4.0.12
82
92
  specification_version: 4
83
93
  summary: High performance memcached client for Ruby
84
94
  test_files: []
@@ -1,117 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Dalli
4
- module Protocol
5
- class Binary
6
- ##
7
- # Class that encapsulates logic for formatting binary protocol requests
8
- # to memcached.
9
- ##
10
- class RequestFormatter
11
- REQUEST = 0x80
12
-
13
- OPCODES = {
14
- get: 0x00,
15
- set: 0x01,
16
- add: 0x02,
17
- replace: 0x03,
18
- delete: 0x04,
19
- incr: 0x05,
20
- decr: 0x06,
21
- flush: 0x08,
22
- noop: 0x0A,
23
- version: 0x0B,
24
- getkq: 0x0D,
25
- append: 0x0E,
26
- prepend: 0x0F,
27
- stat: 0x10,
28
- setq: 0x11,
29
- addq: 0x12,
30
- replaceq: 0x13,
31
- deleteq: 0x14,
32
- incrq: 0x15,
33
- decrq: 0x16,
34
- flushq: 0x18,
35
- appendq: 0x19,
36
- prependq: 0x1A,
37
- touch: 0x1C,
38
- gat: 0x1D,
39
- auth_negotiation: 0x20,
40
- auth_request: 0x21,
41
- auth_continue: 0x22
42
- }.freeze
43
-
44
- REQ_HEADER_FORMAT = 'CCnCCnNNQ'
45
-
46
- KEY_ONLY = 'a*'
47
- TTL_AND_KEY = 'Na*'
48
- KEY_AND_VALUE = 'a*a*'
49
- INCR_DECR = 'NNNNNa*'
50
- TTL_ONLY = 'N'
51
- NO_BODY = ''
52
-
53
- BODY_FORMATS = {
54
- get: KEY_ONLY,
55
- getkq: KEY_ONLY,
56
- delete: KEY_ONLY,
57
- deleteq: KEY_ONLY,
58
- stat: KEY_ONLY,
59
-
60
- append: KEY_AND_VALUE,
61
- prepend: KEY_AND_VALUE,
62
- appendq: KEY_AND_VALUE,
63
- prependq: KEY_AND_VALUE,
64
- auth_request: KEY_AND_VALUE,
65
- auth_continue: KEY_AND_VALUE,
66
-
67
- set: 'NNa*a*',
68
- setq: 'NNa*a*',
69
- add: 'NNa*a*',
70
- addq: 'NNa*a*',
71
- replace: 'NNa*a*',
72
- replaceq: 'NNa*a*',
73
-
74
- incr: INCR_DECR,
75
- decr: INCR_DECR,
76
- incrq: INCR_DECR,
77
- decrq: INCR_DECR,
78
-
79
- flush: TTL_ONLY,
80
- flushq: TTL_ONLY,
81
-
82
- noop: NO_BODY,
83
- auth_negotiation: NO_BODY,
84
- version: NO_BODY,
85
-
86
- touch: TTL_AND_KEY,
87
- gat: TTL_AND_KEY
88
- }.freeze
89
- FORMAT = BODY_FORMATS.transform_values { |v| REQ_HEADER_FORMAT + v }
90
-
91
- # rubocop:disable Metrics/ParameterLists
92
- def self.standard_request(opkey:, key: nil, value: nil, opaque: 0, cas: 0, bitflags: nil, ttl: nil)
93
- extra_len = (bitflags.nil? ? 0 : 4) + (ttl.nil? ? 0 : 4)
94
- key_len = key.nil? ? 0 : key.bytesize
95
- value_len = value.nil? ? 0 : value.bytesize
96
- header = [REQUEST, OPCODES[opkey], key_len, extra_len, 0, 0, extra_len + key_len + value_len, opaque, cas]
97
- body = [bitflags, ttl, key, value].compact
98
- (header + body).pack(FORMAT[opkey])
99
- end
100
- # rubocop:enable Metrics/ParameterLists
101
-
102
- def self.decr_incr_request(opkey:, key: nil, count: nil, initial: nil, expiry: nil)
103
- extra_len = 20
104
- (h, l) = as_8byte_uint(count)
105
- (dh, dl) = as_8byte_uint(initial)
106
- header = [REQUEST, OPCODES[opkey], key.bytesize, extra_len, 0, 0, key.bytesize + extra_len, 0, 0]
107
- body = [h, l, dh, dl, expiry, key]
108
- (header + body).pack(FORMAT[opkey])
109
- end
110
-
111
- def self.as_8byte_uint(val)
112
- [val >> 32, val & 0xFFFFFFFF]
113
- end
114
- end
115
- end
116
- end
117
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Dalli
4
- module Protocol
5
- class Binary
6
- ##
7
- # Class that encapsulates data parsed from a memcached response header.
8
- ##
9
- class ResponseHeader
10
- SIZE = 24
11
- FMT = '@2nCCnNNQ'
12
-
13
- attr_reader :key_len, :extra_len, :data_type, :status, :body_len, :opaque, :cas
14
-
15
- def initialize(buf)
16
- raise ArgumentError, "Response buffer must be at least #{SIZE} bytes" unless buf.bytesize >= SIZE
17
-
18
- @key_len, @extra_len, @data_type, @status, @body_len, @opaque, @cas = buf.unpack(FMT)
19
- end
20
-
21
- def ok?
22
- status.zero?
23
- end
24
-
25
- def not_found?
26
- status == 1
27
- end
28
-
29
- NOT_STORED_STATUSES = [2, 5].freeze
30
- def not_stored?
31
- NOT_STORED_STATUSES.include?(status)
32
- end
33
- end
34
- end
35
- end
36
- end