memcached_store 1.0.0 → 1.1.0

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: 0915f7c2305d9b4962115d99c52997673e711a58
4
- data.tar.gz: 0cc5bbca84c1aee1bcf70f782735d90cd562252d
3
+ metadata.gz: a346167d61e3e802e7ce5d1c45b9ba225e6100e2
4
+ data.tar.gz: 33f531223bd3e02d5a760078975243777a83a6fc
5
5
  SHA512:
6
- metadata.gz: b8a0873627bf43775afd6ef3a7aeb9788a3b5d96d82eaac63f3a49879942e43daf9ace73d8c90c697fc0ca4a4f8a6ee777b3314339634c18734953c84e065d94
7
- data.tar.gz: 5326f2375fa65be54b8c0f5a9c163e0a606c8f6a474ee21f7476c8175fa39252813392e206ad599494b3b72251b6054ad8c824024e963dd9addc3ea16316513c
6
+ metadata.gz: 02c614d50425a3287076e9947a951ba836dde85eb43214f42c42a94e07fac1b8b3bf902f57dafdf247b6d14f08cdc11fa379b847ad96fd56d70d91866fb2c0f3
7
+ data.tar.gz: 7f08c3cd9c8ed9f0d12af91d4a935eeaab25e88a6c78b3a5450a708f768eccdadbe29e8e32aba46b6e974f184708d5320b887745c106fde98cc4acb5c8bb5a18
@@ -11,37 +11,25 @@ module ActiveSupport
11
11
  # MemcachedStore implements the Strategy::LocalCache strategy which implements
12
12
  # an in-memory cache inside of a block.
13
13
  class MemcachedStore < Store
14
- FATAL_EXCEPTIONS = [Memcached::ABadKeyWasProvidedOrCharactersOutOfRange,
15
- Memcached::AKeyLengthOfZeroWasProvided,
16
- Memcached::ConnectionBindFailure,
17
- Memcached::ConnectionDataDoesNotExist,
18
- Memcached::ConnectionFailure,
19
- Memcached::ConnectionSocketCreateFailure,
20
- Memcached::CouldNotOpenUnixSocket,
21
- Memcached::NoServersDefined,
22
- Memcached::TheHostTransportProtocolDoesNotMatchThatOfTheClient]
23
-
24
- NONFATAL_EXCEPTIONS = if defined?(::Rails) && ::Rails.env.test?
25
- []
26
- else
27
- Memcached::EXCEPTIONS - FATAL_EXCEPTIONS
28
- end
29
-
30
14
  ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/n
31
15
 
32
- attr_accessor :read_only
16
+ attr_accessor :read_only, :swallow_exceptions
33
17
 
34
18
  def initialize(*addresses)
35
19
  addresses = addresses.flatten
36
20
  options = addresses.extract_options!
21
+ @swallow_exceptions = true
22
+ @swallow_exceptions = options.delete(:swallow_exceptions) if options.key?(:swallow_exceptions)
23
+
37
24
  super(options)
38
25
 
39
26
  if addresses.first.respond_to?(:get)
40
27
  @data = addresses.first
41
28
  else
42
29
  mem_cache_options = options.dup
30
+ servers = mem_cache_options.delete(:servers)
43
31
  UNIVERSAL_OPTIONS.each { |name| mem_cache_options.delete(name) }
44
- @data = Memcached::Rails.new(*(addresses + [mem_cache_options]))
32
+ @data = Memcached.new([*addresses, *servers], mem_cache_options)
45
33
  end
46
34
 
47
35
  extend Strategy::LocalCache
@@ -64,39 +52,40 @@ module ActiveSupport
64
52
 
65
53
  def read_multi(*names)
66
54
  options = names.extract_options!
55
+ return {} if names.empty?
56
+
67
57
  options = merged_options(options)
68
58
  keys_to_names = Hash[names.map { |name| [normalize_key(name, options), name] }]
69
59
  values = {}
70
60
 
71
- instrument(:read_multi, names, options) do
72
- if raw_values = @data.get_multi(keys_to_names.keys, raw: true)
73
- raw_values.each do |key, value|
74
- entry = deserialize_entry(value)
75
- values[keys_to_names[key]] = entry.value unless entry.expired?
61
+ handle_exceptions(return_value_on_error: {}) do
62
+ instrument(:read_multi, names, options) do
63
+ if raw_values = @data.get(keys_to_names.keys, false)
64
+ raw_values.each do |key, value|
65
+ entry = deserialize_entry(value)
66
+ values[keys_to_names[key]] = entry.value unless entry.expired?
67
+ end
76
68
  end
77
69
  end
70
+ values
78
71
  end
79
- values
80
- rescue *NONFATAL_EXCEPTIONS => e
81
- @data.log_exception(e)
82
- {}
83
72
  end
84
73
 
85
74
  def cas(name, options = nil)
86
75
  options = merged_options(options)
87
76
  key = normalize_key(name, options)
88
77
 
89
- instrument(:cas, name, options) do
90
- @data.cas(key, expiration(options), cas_raw?(options)) do |raw_value|
91
- entry = deserialize_entry(raw_value)
92
- value = yield entry.value
93
- break true if read_only
94
- serialize_entry(Entry.new(value, options), options).first
78
+ handle_exceptions(return_value_on_error: false) do
79
+ instrument(:cas, name, options) do
80
+ @data.cas(key, expiration(options), !cas_raw?(options)) do |raw_value|
81
+ entry = deserialize_entry(raw_value)
82
+ value = yield entry.value
83
+ break true if read_only
84
+ serialize_entry(Entry.new(value, options), options).first
85
+ end
95
86
  end
87
+ true
96
88
  end
97
- rescue *NONFATAL_EXCEPTIONS => e
98
- @data.log_exception(e)
99
- false
100
89
  end
101
90
 
102
91
  def cas_multi(*names)
@@ -106,54 +95,52 @@ module ActiveSupport
106
95
  options = merged_options(options)
107
96
  keys_to_names = Hash[names.map { |name| [normalize_key(name, options), name] }]
108
97
 
109
- instrument(:cas_multi, names, options) do
110
- @data.cas(keys_to_names.keys, expiration(options), cas_raw?(options)) do |raw_values|
111
- values = {}
98
+ handle_exceptions(return_value_on_error: false) do
99
+ instrument(:cas_multi, names, options) do
100
+ @data.cas(keys_to_names.keys, expiration(options), !cas_raw?(options)) do |raw_values|
101
+ values = {}
112
102
 
113
- raw_values.each do |key, raw_value|
114
- entry = deserialize_entry(raw_value)
115
- values[keys_to_names[key]] = entry.value unless entry.expired?
116
- end
103
+ raw_values.each do |key, raw_value|
104
+ entry = deserialize_entry(raw_value)
105
+ values[keys_to_names[key]] = entry.value unless entry.expired?
106
+ end
117
107
 
118
- values = yield values
108
+ values = yield values
119
109
 
120
- break true if read_only
110
+ break true if read_only
121
111
 
122
- serialized_values = values.map do |name, value|
123
- [normalize_key(name, options), serialize_entry(Entry.new(value, options), options).first]
124
- end
112
+ serialized_values = values.map do |name, value|
113
+ [normalize_key(name, options), serialize_entry(Entry.new(value, options), options).first]
114
+ end
125
115
 
126
- Hash[serialized_values]
116
+ Hash[serialized_values]
117
+ end
118
+ true
127
119
  end
128
120
  end
129
- rescue *NONFATAL_EXCEPTIONS => e
130
- @data.log_exception(e)
131
- false
132
121
  end
133
122
 
134
123
  def increment(name, amount = 1, options = nil) # :nodoc:
135
124
  options = merged_options(options)
136
- instrument(:increment, name, amount: amount) do
137
- @data.incr(normalize_key(name, options), amount)
125
+ handle_exceptions(return_value_on_error: nil) do
126
+ instrument(:increment, name, amount: amount) do
127
+ @data.incr(normalize_key(name, options), amount)
128
+ end
138
129
  end
139
- rescue *NONFATAL_EXCEPTIONS => e
140
- @data.log_exception(e)
141
- nil
142
130
  end
143
131
 
144
132
  def decrement(name, amount = 1, options = nil) # :nodoc:
145
133
  options = merged_options(options)
146
- instrument(:decrement, name, amount: amount) do
147
- @data.decr(normalize_key(name, options), amount)
134
+ handle_exceptions(return_value_on_error: nil) do
135
+ instrument(:decrement, name, amount: amount) do
136
+ @data.decr(normalize_key(name, options), amount)
137
+ end
148
138
  end
149
- rescue *NONFATAL_EXCEPTIONS => e
150
- @data.log_exception(e)
151
- nil
152
139
  end
153
140
 
154
141
  def clear(options = nil)
155
142
  ActiveSupport::Notifications.instrument("cache_clear.active_support", options || {}) do
156
- @data.flush_all
143
+ @data.flush
157
144
  end
158
145
  end
159
146
 
@@ -168,19 +155,17 @@ module ActiveSupport
168
155
  end
169
156
 
170
157
  def reset #:nodoc:
171
- @data.reset
172
- rescue *NONFATAL_EXCEPTIONS => e
173
- @data.log_exception(e)
174
- false
158
+ handle_exceptions(return_value_on_error: false) do
159
+ @data.reset
160
+ end
175
161
  end
176
162
 
177
163
  protected
178
164
 
179
165
  def read_entry(key, _options) # :nodoc:
180
- deserialize_entry(@data.get(escape_key(key), true))
181
- rescue *NONFATAL_EXCEPTIONS => e
182
- @data.log_exception(e)
183
- nil
166
+ handle_exceptions(return_value_on_error: nil) do
167
+ deserialize_entry(@data.get(escape_key(key), false))
168
+ end
184
169
  end
185
170
 
186
171
  def write_entry(key, entry, options) # :nodoc:
@@ -188,19 +173,18 @@ module ActiveSupport
188
173
  method = options && options[:unless_exist] ? :add : :set
189
174
  expires_in = expiration(options)
190
175
  value, raw = serialize_entry(entry, options)
191
- @data.send(method, escape_key(key), value, expires_in, raw)
192
- rescue *NONFATAL_EXCEPTIONS => e
193
- @data.log_exception(e)
194
- false
176
+ handle_exceptions(return_value_on_error: false) do
177
+ @data.send(method, escape_key(key), value, expires_in, !raw)
178
+ true
179
+ end
195
180
  end
196
181
 
197
182
  def delete_entry(key, _options) # :nodoc:
198
183
  return true if read_only
199
- @data.delete(escape_key(key))
200
- true
201
- rescue *NONFATAL_EXCEPTIONS => e
202
- @data.log_exception(e)
203
- false
184
+ handle_exceptions(return_value_on_error: false, on_miss: true) do
185
+ @data.delete(escape_key(key))
186
+ true
187
+ end
204
188
  end
205
189
 
206
190
  private
@@ -259,6 +243,16 @@ module ActiveSupport
259
243
  end
260
244
  expires_in
261
245
  end
246
+
247
+ def handle_exceptions(return_value_on_error:, on_miss: return_value_on_error)
248
+ yield
249
+ rescue Memcached::NotFound, Memcached::ConnectionDataExists
250
+ on_miss
251
+ rescue Memcached::Error => e
252
+ raise unless @swallow_exceptions
253
+ logger.warn("memcached error: #{e.class}: #{e.message}") if logger
254
+ return_value_on_error
255
+ end
262
256
  end
263
257
  end
264
258
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module MemcachedStore
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
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: 1.0.0
4
+ version: 1.1.0
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: 2016-12-01 00:00:00.000000000 Z
14
+ date: 2017-05-08 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  requirements: []
93
93
  rubyforge_project:
94
- rubygems_version: 2.5.1
94
+ rubygems_version: 2.5.2
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: Plugin-able Memcached adapters to add features (compression, safety)