memcached_store 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa72b41fd6b6cb58a042294012e8f736c3b5ef62
4
- data.tar.gz: e053cc731f76cb2bc909e36f7f004002b2fd7e38
3
+ metadata.gz: 604df981c587ff19ef1bd833c79f95d0716051bd
4
+ data.tar.gz: cd75d95d121d1969b8ffe73d858b9699a51a3000
5
5
  SHA512:
6
- metadata.gz: 2045826e00f5d2c2d988eed051036713348bafcd7867bf06b1f6760decea9719e62a0cfc332ee82f3e3ef84f2eecb65ab7eba373f1e969f5592cfa53eb12a034
7
- data.tar.gz: c36fde971c9abc498626b8662cb87977369e85c3818efbbb5bf23bf0ec0bf51e9a17e338dce98661c41b7fdb2abbf7fc6a1a271bea0cb0a8fd177e8f9e5dccf7
6
+ metadata.gz: 82bec692dca6c4e55e809ab7a413681e8d3737cc3a59a9cd20c8b505a1506eb6f7a1e3d0e6bb0c91a6da5b0e1c3e9f330692f04e729a6213fcec9a5375ff8adf
7
+ data.tar.gz: ba4c196fb7bbdc0cbae3e03568ef0d2dfea8577a5f66a7ecaa1c4afc52116f4f63ef3d0d2bc848e7bffd1eb735ad031ca00cbf504d94dc3d232caa669f632c37
@@ -12,6 +12,16 @@ module ActiveSupport
12
12
  class MemcachedSnappyStore < MemcachedStore
13
13
  class UnsupportedOperation < StandardError; end
14
14
 
15
+ module SnappyCompressor
16
+ def self.compress(source)
17
+ Snappy.deflate(source)
18
+ end
19
+
20
+ def self.decompress(source)
21
+ Snappy.inflate(source)
22
+ end
23
+ end
24
+
15
25
  def increment(*)
16
26
  raise UnsupportedOperation, "increment is not supported by: #{self.class.name}"
17
27
  end
@@ -25,21 +35,10 @@ module ActiveSupport
25
35
  false
26
36
  end
27
37
 
28
- private
29
-
30
- def serialize_entry(entry, options)
31
- value = options[:raw] ? entry.value.to_s : Marshal.dump(entry)
32
- [Snappy.deflate(value), true]
33
- end
34
-
35
- def deserialize_entry(compressed_value)
36
- if compressed_value
37
- super(Snappy.inflate(compressed_value))
38
- end
39
- end
40
-
41
- def cas_raw?(_options)
42
- true
38
+ def initialize(*addresses, **options)
39
+ options[:codec] ||= ActiveSupport::Cache::MemcachedStore::Codec.new(compressor: SnappyCompressor)
40
+ options[:compress] = false
41
+ super(*addresses, **options)
43
42
  end
44
43
  end
45
44
  end
@@ -13,11 +13,54 @@ module ActiveSupport
13
13
  class MemcachedStore < Store
14
14
  ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/n
15
15
 
16
+ class Codec
17
+ # use dalli compatible flags
18
+ SERIALIZED_FLAG = 0x1
19
+ COMPRESSED_FLAG = 0x2
20
+
21
+ # Older versions of this gem would use 0 for the flags whether or not
22
+ # the value was marshal dumped. By setting this flag, we can tell if
23
+ # it were set with an older version for backwards compatible decoding.
24
+ RAW_FLAG = 0x10
25
+
26
+ def initialize(serializer: Marshal, compressor: nil)
27
+ @serializer = serializer
28
+ @compressor = compressor
29
+ end
30
+
31
+ def encode(_key, value, flags)
32
+ unless value.is_a?(String)
33
+ flags |= SERIALIZED_FLAG
34
+ value = @serializer.dump(value)
35
+ end
36
+ if @compressor
37
+ flags |= COMPRESSED_FLAG
38
+ value = @compressor.compress(value)
39
+ end
40
+ flags |= RAW_FLAG if flags == 0
41
+ [value, flags]
42
+ end
43
+
44
+ def decode(_key, value, flags)
45
+ if (flags & COMPRESSED_FLAG) != 0
46
+ value = @compressor.decompress(value)
47
+ end
48
+
49
+ if (flags & SERIALIZED_FLAG) != 0
50
+ @serializer.load(value)
51
+ elsif flags == 0 # legacy cache value
52
+ @serializer.load(value) rescue value
53
+ else
54
+ value
55
+ end
56
+ end
57
+ end
58
+
16
59
  attr_accessor :read_only, :swallow_exceptions
17
60
 
18
- def initialize(*addresses)
61
+ def initialize(*addresses, **options)
19
62
  addresses = addresses.flatten
20
- options = addresses.extract_options!
63
+ options[:codec] ||= Codec.new
21
64
  @swallow_exceptions = true
22
65
  @swallow_exceptions = options.delete(:swallow_exceptions) if options.key?(:swallow_exceptions)
23
66
 
@@ -57,7 +100,7 @@ module ActiveSupport
57
100
 
58
101
  handle_exceptions(return_value_on_error: {}) do
59
102
  instrument(:read_multi, names, options) do
60
- if raw_values = @data.get(keys_to_names.keys, false)
103
+ if raw_values = @data.get(keys_to_names.keys)
61
104
  raw_values.each do |key, value|
62
105
  entry = deserialize_entry(value)
63
106
  values[keys_to_names[key]] = entry.value unless entry.expired?
@@ -74,19 +117,18 @@ module ActiveSupport
74
117
 
75
118
  handle_exceptions(return_value_on_error: false) do
76
119
  instrument(:cas, name, options) do
77
- @data.cas(key, expiration(options), !cas_raw?(options)) do |raw_value|
120
+ @data.cas(key, expiration(options)) do |raw_value|
78
121
  entry = deserialize_entry(raw_value)
79
122
  value = yield entry.value
80
123
  break true if read_only
81
- serialize_entry(Entry.new(value, options), options).first
124
+ serialize_entry(Entry.new(value, options), options)
82
125
  end
83
126
  end
84
127
  true
85
128
  end
86
129
  end
87
130
 
88
- def cas_multi(*names)
89
- options = names.extract_options!
131
+ def cas_multi(*names, **options)
90
132
  return if names.empty?
91
133
 
92
134
  options = merged_options(options)
@@ -94,7 +136,7 @@ module ActiveSupport
94
136
 
95
137
  handle_exceptions(return_value_on_error: false) do
96
138
  instrument(:cas_multi, names, options) do
97
- @data.cas(keys_to_names.keys, expiration(options), !cas_raw?(options)) do |raw_values|
139
+ @data.cas(keys_to_names.keys, expiration(options)) do |raw_values|
98
140
  values = {}
99
141
 
100
142
  raw_values.each do |key, raw_value|
@@ -107,7 +149,7 @@ module ActiveSupport
107
149
  break true if read_only
108
150
 
109
151
  serialized_values = values.map do |name, value|
110
- [normalize_key(name, options), serialize_entry(Entry.new(value, options), options).first]
152
+ [normalize_key(name, options), serialize_entry(Entry.new(value, options), options)]
111
153
  end
112
154
 
113
155
  Hash[serialized_values]
@@ -161,7 +203,7 @@ module ActiveSupport
161
203
 
162
204
  def read_entry(key, _options) # :nodoc:
163
205
  handle_exceptions(return_value_on_error: nil) do
164
- deserialize_entry(@data.get(escape_key(key), false))
206
+ deserialize_entry(@data.get(escape_key(key)))
165
207
  end
166
208
  end
167
209
 
@@ -169,9 +211,9 @@ module ActiveSupport
169
211
  return true if read_only
170
212
  method = options && options[:unless_exist] ? :add : :set
171
213
  expires_in = expiration(options)
172
- value, raw = serialize_entry(entry, options)
214
+ value = serialize_entry(entry, options)
173
215
  handle_exceptions(return_value_on_error: false) do
174
- @data.send(method, escape_key(key), value, expires_in, !raw)
216
+ @data.send(method, escape_key(key), value, expires_in)
175
217
  true
176
218
  end
177
219
  end
@@ -213,24 +255,18 @@ module ActiveSupport
213
255
  end
214
256
  end
215
257
 
216
- def deserialize_entry(raw_value)
217
- if raw_value
218
- entry = begin
219
- Marshal.load(raw_value)
220
- rescue
221
- raw_value
222
- end
223
- entry.is_a?(Entry) ? entry : Entry.new(entry)
258
+ def deserialize_entry(value)
259
+ if value
260
+ value.is_a?(Entry) ? value : Entry.new(value, compresss: false)
224
261
  end
225
262
  end
226
263
 
227
264
  def serialize_entry(entry, options)
228
- entry = entry.value.to_s if options[:raw]
229
- [entry, options[:raw]]
230
- end
231
-
232
- def cas_raw?(options)
233
- options[:raw]
265
+ if options[:raw]
266
+ entry.value.to_s
267
+ else
268
+ entry
269
+ end
234
270
  end
235
271
 
236
272
  def expiration(options)
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module MemcachedStore
3
- VERSION = "2.0.1"
3
+ VERSION = "2.0.2"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memcached_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Camilo Lopez
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2018-08-23 00:00:00.000000000 Z
14
+ date: 2018-09-25 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport