activesupport 4.2.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +630 -220
- data/MIT-LICENSE +2 -2
- data/README.rdoc +2 -3
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +1 -1
- data/lib/active_support/benchmarkable.rb +1 -1
- data/lib/active_support/cache/file_store.rb +36 -22
- data/lib/active_support/cache/mem_cache_store.rb +63 -54
- data/lib/active_support/cache/memory_store.rb +16 -21
- data/lib/active_support/cache/null_store.rb +1 -4
- data/lib/active_support/cache/strategy/local_cache.rb +31 -20
- data/lib/active_support/cache.rb +73 -89
- data/lib/active_support/callbacks.rb +195 -155
- data/lib/active_support/concern.rb +2 -2
- data/lib/active_support/concurrency/latch.rb +7 -15
- data/lib/active_support/concurrency/share_lock.rb +186 -0
- data/lib/active_support/configurable.rb +1 -0
- data/lib/active_support/core_ext/array/access.rb +27 -1
- data/lib/active_support/core_ext/array/conversions.rb +6 -4
- data/lib/active_support/core_ext/array/grouping.rb +9 -18
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/wrap.rb +5 -4
- data/lib/active_support/core_ext/array.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -10
- data/lib/active_support/core_ext/class/attribute.rb +10 -9
- data/lib/active_support/core_ext/class/subclasses.rb +3 -4
- data/lib/active_support/core_ext/class.rb +0 -1
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +1 -1
- data/lib/active_support/core_ext/date/conversions.rb +13 -6
- data/lib/active_support/core_ext/date.rb +1 -1
- data/lib/active_support/core_ext/date_and_time/calculations.rb +109 -25
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +3 -4
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +36 -10
- data/lib/active_support/core_ext/date_time/compatibility.rb +5 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +2 -0
- data/lib/active_support/core_ext/date_time.rb +2 -1
- data/lib/active_support/core_ext/enumerable.rb +49 -5
- data/lib/active_support/core_ext/file/atomic.rb +30 -25
- data/lib/active_support/core_ext/hash/conversions.rb +23 -4
- data/lib/active_support/core_ext/hash/deep_merge.rb +1 -1
- data/lib/active_support/core_ext/hash/except.rb +9 -8
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -1
- data/lib/active_support/core_ext/hash/keys.rb +23 -19
- data/lib/active_support/core_ext/hash/slice.rb +1 -1
- data/lib/active_support/core_ext/hash/transform_values.rb +11 -5
- data/lib/active_support/core_ext/integer/time.rb +1 -16
- data/lib/active_support/core_ext/kernel/concern.rb +2 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -10
- data/lib/active_support/core_ext/kernel/reporting.rb +2 -83
- data/lib/active_support/core_ext/kernel.rb +0 -1
- data/lib/active_support/core_ext/load_error.rb +4 -2
- data/lib/active_support/core_ext/marshal.rb +12 -11
- data/lib/active_support/core_ext/module/aliasing.rb +6 -1
- data/lib/active_support/core_ext/module/anonymous.rb +10 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
- data/lib/active_support/core_ext/module/attribute_accessors.rb +15 -15
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +4 -4
- data/lib/active_support/core_ext/module/delegation.rb +35 -25
- data/lib/active_support/core_ext/module/deprecation.rb +2 -2
- data/lib/active_support/core_ext/module/introspection.rb +4 -0
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -11
- data/lib/active_support/core_ext/module/qualified_const.rb +30 -12
- data/lib/active_support/core_ext/module/remove_method.rb +23 -0
- data/lib/active_support/core_ext/module.rb +1 -0
- data/lib/active_support/core_ext/name_error.rb +15 -2
- data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +74 -64
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +24 -19
- data/lib/active_support/core_ext/numeric.rb +1 -0
- data/lib/active_support/core_ext/object/blank.rb +17 -5
- data/lib/active_support/core_ext/object/deep_dup.rb +10 -3
- data/lib/active_support/core_ext/object/duplicable.rb +8 -13
- data/lib/active_support/core_ext/object/inclusion.rb +2 -2
- data/lib/active_support/core_ext/object/instance_variables.rb +1 -1
- data/lib/active_support/core_ext/object/json.rb +15 -7
- data/lib/active_support/core_ext/object/to_query.rb +1 -1
- data/lib/active_support/core_ext/object/try.rb +68 -22
- data/lib/active_support/core_ext/object/with_options.rb +1 -1
- data/lib/active_support/core_ext/object.rb +0 -1
- data/lib/active_support/core_ext/range/conversions.rb +18 -6
- data/lib/active_support/core_ext/range/each.rb +16 -18
- data/lib/active_support/core_ext/range/include_range.rb +20 -20
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string/access.rb +1 -1
- data/lib/active_support/core_ext/string/behavior.rb +1 -1
- data/lib/active_support/core_ext/string/conversions.rb +4 -3
- data/lib/active_support/core_ext/string/filters.rb +5 -5
- data/lib/active_support/core_ext/string/inflections.rb +32 -5
- data/lib/active_support/core_ext/string/multibyte.rb +11 -7
- data/lib/active_support/core_ext/string/output_safety.rb +18 -16
- data/lib/active_support/core_ext/string/strip.rb +3 -6
- data/lib/active_support/core_ext/struct.rb +3 -6
- data/lib/active_support/core_ext/time/calculations.rb +36 -11
- data/lib/active_support/core_ext/time/compatibility.rb +5 -0
- data/lib/active_support/core_ext/time/conversions.rb +4 -2
- data/lib/active_support/core_ext/time/marshal.rb +2 -29
- data/lib/active_support/core_ext/time/zones.rb +36 -4
- data/lib/active_support/core_ext/time.rb +1 -1
- data/lib/active_support/core_ext/uri.rb +1 -3
- data/lib/active_support/core_ext.rb +2 -1
- data/lib/active_support/dependencies/interlock.rb +51 -0
- data/lib/active_support/dependencies.rb +87 -95
- data/lib/active_support/deprecation/behaviors.rb +16 -2
- data/lib/active_support/deprecation/method_wrappers.rb +42 -16
- data/lib/active_support/deprecation/proxy_wrappers.rb +47 -24
- data/lib/active_support/deprecation/reporting.rb +23 -5
- data/lib/active_support/deprecation.rb +1 -1
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/duration.rb +55 -10
- data/lib/active_support/evented_file_update_checker.rb +194 -0
- data/lib/active_support/execution_wrapper.rb +117 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +23 -3
- data/lib/active_support/gem_version.rb +3 -3
- data/lib/active_support/hash_with_indifferent_access.rb +46 -13
- data/lib/active_support/i18n_railtie.rb +25 -4
- data/lib/active_support/inflector/inflections.rb +36 -5
- data/lib/active_support/inflector/methods.rb +97 -90
- data/lib/active_support/inflector/transliterate.rb +36 -21
- data/lib/active_support/json/decoding.rb +11 -10
- data/lib/active_support/json/encoding.rb +4 -49
- data/lib/active_support/key_generator.rb +7 -9
- data/lib/active_support/locale/en.yml +2 -0
- data/lib/active_support/log_subscriber/test_helper.rb +3 -3
- data/lib/active_support/log_subscriber.rb +1 -1
- data/lib/active_support/logger.rb +50 -1
- data/lib/active_support/logger_silence.rb +8 -4
- data/lib/active_support/logger_thread_safe_level.rb +31 -0
- data/lib/active_support/message_encryptor.rb +4 -4
- data/lib/active_support/message_verifier.rb +70 -8
- data/lib/active_support/multibyte/chars.rb +13 -4
- data/lib/active_support/multibyte/unicode.rb +44 -21
- data/lib/active_support/notifications/fanout.rb +6 -6
- data/lib/active_support/notifications/instrumenter.rb +20 -2
- data/lib/active_support/notifications.rb +2 -2
- data/lib/active_support/number_helper/number_to_currency_converter.rb +7 -9
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +8 -3
- data/lib/active_support/number_helper/number_to_human_converter.rb +6 -4
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +6 -2
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +1 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +11 -2
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +30 -25
- data/lib/active_support/number_helper.rb +90 -67
- data/lib/active_support/ordered_hash.rb +1 -1
- data/lib/active_support/ordered_options.rb +15 -1
- data/lib/active_support/per_thread_registry.rb +8 -3
- data/lib/active_support/rails.rb +2 -2
- data/lib/active_support/railtie.rb +6 -1
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +93 -47
- data/lib/active_support/security_utils.rb +7 -0
- data/lib/active_support/string_inquirer.rb +1 -1
- data/lib/active_support/subscriber.rb +5 -10
- data/lib/active_support/tagged_logging.rb +3 -1
- data/lib/active_support/test_case.rb +15 -29
- data/lib/active_support/testing/assertions.rb +15 -13
- data/lib/active_support/testing/autorun.rb +8 -1
- data/lib/active_support/testing/deprecation.rb +9 -8
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +22 -8
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/time_helpers.rb +13 -10
- data/lib/active_support/time_with_zone.rb +135 -46
- data/lib/active_support/values/time_zone.rb +95 -47
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/xml_mini/jdom.rb +7 -6
- data/lib/active_support/xml_mini/libxml.rb +2 -2
- data/lib/active_support/xml_mini/nokogiri.rb +2 -2
- data/lib/active_support/xml_mini/rexml.rb +7 -8
- data/lib/active_support/xml_mini.rb +22 -14
- data/lib/active_support.rb +20 -6
- metadata +32 -35
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -14
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
- data/lib/active_support/core_ext/date_time/zones.rb +0 -6
- data/lib/active_support/core_ext/object/itself.rb +0 -15
- data/lib/active_support/core_ext/thread.rb +0 -86
data/lib/active_support/cache.rb
CHANGED
@@ -8,7 +8,7 @@ require 'active_support/core_ext/numeric/bytes'
|
|
8
8
|
require 'active_support/core_ext/numeric/time'
|
9
9
|
require 'active_support/core_ext/object/to_param'
|
10
10
|
require 'active_support/core_ext/string/inflections'
|
11
|
-
require 'active_support/
|
11
|
+
require 'active_support/core_ext/string/strip'
|
12
12
|
|
13
13
|
module ActiveSupport
|
14
14
|
# See ActiveSupport::Cache::Store for documentation.
|
@@ -27,7 +27,7 @@ module ActiveSupport
|
|
27
27
|
end
|
28
28
|
|
29
29
|
class << self
|
30
|
-
# Creates a new
|
30
|
+
# Creates a new Store object according to the given options.
|
31
31
|
#
|
32
32
|
# If no arguments are passed to this method, then a new
|
33
33
|
# ActiveSupport::Cache::MemoryStore object will be returned.
|
@@ -158,20 +158,20 @@ module ActiveSupport
|
|
158
158
|
attr_reader :silence, :options
|
159
159
|
alias :silence? :silence
|
160
160
|
|
161
|
-
#
|
161
|
+
# Creates a new cache. The options will be passed to any write method calls
|
162
162
|
# except for <tt>:namespace</tt> which can be used to set the global
|
163
163
|
# namespace for the cache.
|
164
164
|
def initialize(options = nil)
|
165
165
|
@options = options ? options.dup : {}
|
166
166
|
end
|
167
167
|
|
168
|
-
#
|
168
|
+
# Silences the logger.
|
169
169
|
def silence!
|
170
170
|
@silence = true
|
171
171
|
self
|
172
172
|
end
|
173
173
|
|
174
|
-
#
|
174
|
+
# Silences the logger within a block.
|
175
175
|
def mute
|
176
176
|
previous_silence, @silence = defined?(@silence) && @silence, true
|
177
177
|
yield
|
@@ -179,18 +179,6 @@ module ActiveSupport
|
|
179
179
|
@silence = previous_silence
|
180
180
|
end
|
181
181
|
|
182
|
-
# :deprecated:
|
183
|
-
def self.instrument=(boolean)
|
184
|
-
ActiveSupport::Deprecation.warn "ActiveSupport::Cache.instrument= is deprecated and will be removed in Rails 5. Instrumentation is now always on so you can safely stop using it."
|
185
|
-
true
|
186
|
-
end
|
187
|
-
|
188
|
-
# :deprecated:
|
189
|
-
def self.instrument
|
190
|
-
ActiveSupport::Deprecation.warn "ActiveSupport::Cache.instrument is deprecated and will be removed in Rails 5. Instrumentation is now always on so you can safely stop using it."
|
191
|
-
true
|
192
|
-
end
|
193
|
-
|
194
182
|
# Fetches data from the cache, using the given key. If there is data in
|
195
183
|
# the cache with the given key, then that data is returned.
|
196
184
|
#
|
@@ -210,10 +198,17 @@ module ActiveSupport
|
|
210
198
|
# cache.fetch('city') # => "Duckburgh"
|
211
199
|
#
|
212
200
|
# You may also specify additional options via the +options+ argument.
|
213
|
-
# Setting <tt>force: true</tt>
|
201
|
+
# Setting <tt>force: true</tt> forces a cache "miss," meaning we treat
|
202
|
+
# the cache value as missing even if it's present. Passing a block is
|
203
|
+
# required when `force` is true so this always results in a cache write.
|
214
204
|
#
|
215
205
|
# cache.write('today', 'Monday')
|
216
|
-
# cache.fetch('today', force: true)
|
206
|
+
# cache.fetch('today', force: true) { 'Tuesday' } # => 'Tuesday'
|
207
|
+
# cache.fetch('today', force: true) # => ArgumentError
|
208
|
+
#
|
209
|
+
# The `:force` option is useful when you're calling some other method to
|
210
|
+
# ask whether you should force a cache write. Otherwise, it's clearer to
|
211
|
+
# just call `Cache#write`.
|
217
212
|
#
|
218
213
|
# Setting <tt>:compress</tt> will store a large cache entry set by the call
|
219
214
|
# in a compressed format.
|
@@ -267,10 +262,11 @@ module ActiveSupport
|
|
267
262
|
# end
|
268
263
|
# end
|
269
264
|
#
|
270
|
-
# #
|
271
|
-
# #
|
272
|
-
#
|
273
|
-
# #
|
265
|
+
# cache.fetch('foo') # => "original value"
|
266
|
+
# sleep 10 # First thread extended the life of cache by another 10 seconds
|
267
|
+
# cache.fetch('foo') # => "new value 1"
|
268
|
+
# val_1 # => "new value 1"
|
269
|
+
# val_2 # => "original value"
|
274
270
|
#
|
275
271
|
# Other options will be handled by the specific cache store implementation.
|
276
272
|
# Internally, #fetch calls #read_entry, and calls #write_entry on a cache
|
@@ -288,16 +284,23 @@ module ActiveSupport
|
|
288
284
|
def fetch(name, options = nil)
|
289
285
|
if block_given?
|
290
286
|
options = merged_options(options)
|
291
|
-
key =
|
287
|
+
key = normalize_key(name, options)
|
292
288
|
|
293
|
-
|
294
|
-
|
289
|
+
entry = nil
|
290
|
+
instrument(:read, name, options) do |payload|
|
291
|
+
cached_entry = read_entry(key, options) unless options[:force]
|
292
|
+
entry = handle_expired_entry(cached_entry, key, options)
|
293
|
+
payload[:super_operation] = :fetch if payload
|
294
|
+
payload[:hit] = !!entry if payload
|
295
|
+
end
|
295
296
|
|
296
297
|
if entry
|
297
298
|
get_entry_value(entry, name, options)
|
298
299
|
else
|
299
300
|
save_block_result_to_cache(name, options) { |_name| yield _name }
|
300
301
|
end
|
302
|
+
elsif options && options[:force]
|
303
|
+
raise ArgumentError, 'Missing block: Calling `Cache#fetch` with `force: true` requires a block.'
|
301
304
|
else
|
302
305
|
read(name, options)
|
303
306
|
end
|
@@ -310,7 +313,7 @@ module ActiveSupport
|
|
310
313
|
# Options are passed to the underlying cache implementation.
|
311
314
|
def read(name, options = nil)
|
312
315
|
options = merged_options(options)
|
313
|
-
key =
|
316
|
+
key = normalize_key(name, options)
|
314
317
|
instrument(:read, name, options) do |payload|
|
315
318
|
entry = read_entry(key, options)
|
316
319
|
if entry
|
@@ -329,7 +332,7 @@ module ActiveSupport
|
|
329
332
|
end
|
330
333
|
end
|
331
334
|
|
332
|
-
#
|
335
|
+
# Reads multiple values at once from the cache. Options can be passed
|
333
336
|
# in the last argument.
|
334
337
|
#
|
335
338
|
# Some cache implementation may optimize this method.
|
@@ -338,9 +341,10 @@ module ActiveSupport
|
|
338
341
|
def read_multi(*names)
|
339
342
|
options = names.extract_options!
|
340
343
|
options = merged_options(options)
|
344
|
+
|
341
345
|
results = {}
|
342
346
|
names.each do |name|
|
343
|
-
key =
|
347
|
+
key = normalize_key(name, options)
|
344
348
|
entry = read_entry(key, options)
|
345
349
|
if entry
|
346
350
|
if entry.expired?
|
@@ -363,8 +367,11 @@ module ActiveSupport
|
|
363
367
|
# Returns a hash with the data for each of the names. For example:
|
364
368
|
#
|
365
369
|
# cache.write("bim", "bam")
|
366
|
-
# cache.fetch_multi("bim", "
|
367
|
-
#
|
370
|
+
# cache.fetch_multi("bim", "unknown_key") do |key|
|
371
|
+
# "Fallback value for key: #{key}"
|
372
|
+
# end
|
373
|
+
# # => { "bim" => "bam",
|
374
|
+
# # "unknown_key" => "Fallback value for key: unknown_key" }
|
368
375
|
#
|
369
376
|
def fetch_multi(*names)
|
370
377
|
options = names.extract_options!
|
@@ -388,7 +395,7 @@ module ActiveSupport
|
|
388
395
|
|
389
396
|
instrument(:write, name, options) do
|
390
397
|
entry = Entry.new(value, options)
|
391
|
-
write_entry(
|
398
|
+
write_entry(normalize_key(name, options), entry, options)
|
392
399
|
end
|
393
400
|
end
|
394
401
|
|
@@ -399,7 +406,7 @@ module ActiveSupport
|
|
399
406
|
options = merged_options(options)
|
400
407
|
|
401
408
|
instrument(:delete, name) do
|
402
|
-
delete_entry(
|
409
|
+
delete_entry(normalize_key(name, options), options)
|
403
410
|
end
|
404
411
|
end
|
405
412
|
|
@@ -410,12 +417,12 @@ module ActiveSupport
|
|
410
417
|
options = merged_options(options)
|
411
418
|
|
412
419
|
instrument(:exist?, name) do
|
413
|
-
entry = read_entry(
|
420
|
+
entry = read_entry(normalize_key(name, options), options)
|
414
421
|
(entry && !entry.expired?) || false
|
415
422
|
end
|
416
423
|
end
|
417
424
|
|
418
|
-
#
|
425
|
+
# Deletes all entries with keys matching the pattern.
|
419
426
|
#
|
420
427
|
# Options are passed to the underlying cache implementation.
|
421
428
|
#
|
@@ -424,7 +431,7 @@ module ActiveSupport
|
|
424
431
|
raise NotImplementedError.new("#{self.class.name} does not support delete_matched")
|
425
432
|
end
|
426
433
|
|
427
|
-
#
|
434
|
+
# Increments an integer value in the cache.
|
428
435
|
#
|
429
436
|
# Options are passed to the underlying cache implementation.
|
430
437
|
#
|
@@ -433,7 +440,7 @@ module ActiveSupport
|
|
433
440
|
raise NotImplementedError.new("#{self.class.name} does not support increment")
|
434
441
|
end
|
435
442
|
|
436
|
-
#
|
443
|
+
# Decrements an integer value in the cache.
|
437
444
|
#
|
438
445
|
# Options are passed to the underlying cache implementation.
|
439
446
|
#
|
@@ -442,7 +449,7 @@ module ActiveSupport
|
|
442
449
|
raise NotImplementedError.new("#{self.class.name} does not support decrement")
|
443
450
|
end
|
444
451
|
|
445
|
-
#
|
452
|
+
# Cleanups the cache by removing expired entries.
|
446
453
|
#
|
447
454
|
# Options are passed to the underlying cache implementation.
|
448
455
|
#
|
@@ -451,7 +458,7 @@ module ActiveSupport
|
|
451
458
|
raise NotImplementedError.new("#{self.class.name} does not support cleanup")
|
452
459
|
end
|
453
460
|
|
454
|
-
#
|
461
|
+
# Clears the entire cache. Be careful with this method since it could
|
455
462
|
# affect other processes if shared cache is being used.
|
456
463
|
#
|
457
464
|
# The options hash is passed to the underlying cache implementation.
|
@@ -462,7 +469,7 @@ module ActiveSupport
|
|
462
469
|
end
|
463
470
|
|
464
471
|
protected
|
465
|
-
#
|
472
|
+
# Adds the namespace defined in the options to a pattern designed to
|
466
473
|
# match keys. Implementations that support delete_matched should call
|
467
474
|
# this method to translate a pattern that matches names into one that
|
468
475
|
# matches namespaced keys.
|
@@ -481,26 +488,26 @@ module ActiveSupport
|
|
481
488
|
end
|
482
489
|
end
|
483
490
|
|
484
|
-
#
|
491
|
+
# Reads an entry from the cache implementation. Subclasses must implement
|
485
492
|
# this method.
|
486
493
|
def read_entry(key, options) # :nodoc:
|
487
494
|
raise NotImplementedError.new
|
488
495
|
end
|
489
496
|
|
490
|
-
#
|
497
|
+
# Writes an entry to the cache implementation. Subclasses must implement
|
491
498
|
# this method.
|
492
499
|
def write_entry(key, entry, options) # :nodoc:
|
493
500
|
raise NotImplementedError.new
|
494
501
|
end
|
495
502
|
|
496
|
-
#
|
503
|
+
# Deletes an entry from the cache implementation. Subclasses must
|
497
504
|
# implement this method.
|
498
505
|
def delete_entry(key, options) # :nodoc:
|
499
506
|
raise NotImplementedError.new
|
500
507
|
end
|
501
508
|
|
502
509
|
private
|
503
|
-
#
|
510
|
+
# Merges the default options with ones specific to a method call.
|
504
511
|
def merged_options(call_options) # :nodoc:
|
505
512
|
if call_options
|
506
513
|
options.merge(call_options)
|
@@ -509,7 +516,7 @@ module ActiveSupport
|
|
509
516
|
end
|
510
517
|
end
|
511
518
|
|
512
|
-
#
|
519
|
+
# Expands key to be a consistent string value. Invokes +cache_key+ if
|
513
520
|
# object responds to +cache_key+. Otherwise, +to_param+ method will be
|
514
521
|
# called. If the key is a Hash, then keys will be sorted alphabetically.
|
515
522
|
def expanded_key(key) # :nodoc:
|
@@ -529,9 +536,9 @@ module ActiveSupport
|
|
529
536
|
key.to_param
|
530
537
|
end
|
531
538
|
|
532
|
-
#
|
539
|
+
# Prefixes a key with the namespace. Namespace and key will be delimited
|
533
540
|
# with a colon.
|
534
|
-
def
|
541
|
+
def normalize_key(key, options)
|
535
542
|
key = expanded_key(key)
|
536
543
|
namespace = options[:namespace] if options
|
537
544
|
prefix = namespace.is_a?(Proc) ? namespace.call : namespace
|
@@ -539,32 +546,33 @@ module ActiveSupport
|
|
539
546
|
key
|
540
547
|
end
|
541
548
|
|
549
|
+
def namespaced_key(*args)
|
550
|
+
ActiveSupport::Deprecation.warn(<<-MESSAGE.strip_heredoc)
|
551
|
+
`namespaced_key` is deprecated and will be removed from Rails 5.1.
|
552
|
+
Please use `normalize_key` which will return a fully resolved key.
|
553
|
+
MESSAGE
|
554
|
+
normalize_key(*args)
|
555
|
+
end
|
556
|
+
|
542
557
|
def instrument(operation, key, options = nil)
|
543
|
-
log
|
558
|
+
log { "Cache #{operation}: #{normalize_key(key, options)}#{options.blank? ? "" : " (#{options.inspect})"}" }
|
544
559
|
|
545
560
|
payload = { :key => key }
|
546
561
|
payload.merge!(options) if options.is_a?(Hash)
|
547
562
|
ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload){ yield(payload) }
|
548
563
|
end
|
549
564
|
|
550
|
-
def log
|
565
|
+
def log
|
551
566
|
return unless logger && logger.debug? && !silence?
|
552
|
-
logger.debug(
|
553
|
-
end
|
554
|
-
|
555
|
-
def find_cached_entry(key, name, options)
|
556
|
-
instrument(:read, name, options) do |payload|
|
557
|
-
payload[:super_operation] = :fetch if payload
|
558
|
-
read_entry(key, options)
|
559
|
-
end
|
567
|
+
logger.debug(yield)
|
560
568
|
end
|
561
569
|
|
562
570
|
def handle_expired_entry(entry, key, options)
|
563
571
|
if entry && entry.expired?
|
564
572
|
race_ttl = options[:race_condition_ttl].to_i
|
565
|
-
if race_ttl && (Time.now.to_f - entry.expires_at <= race_ttl)
|
566
|
-
# When an entry has :race_condition_ttl defined, put the stale entry back into the cache
|
567
|
-
# for a brief period while the entry is
|
573
|
+
if (race_ttl > 0) && (Time.now.to_f - entry.expires_at <= race_ttl)
|
574
|
+
# When an entry has a positive :race_condition_ttl defined, put the stale entry back into the cache
|
575
|
+
# for a brief period while the entry is being recalculated.
|
568
576
|
entry.expires_at = Time.now + race_ttl
|
569
577
|
write_entry(key, entry, :expires_in => race_ttl * 2)
|
570
578
|
else
|
@@ -576,12 +584,12 @@ module ActiveSupport
|
|
576
584
|
end
|
577
585
|
|
578
586
|
def get_entry_value(entry, name, options)
|
579
|
-
instrument(:fetch_hit, name, options) {
|
587
|
+
instrument(:fetch_hit, name, options) { }
|
580
588
|
entry.value
|
581
589
|
end
|
582
590
|
|
583
591
|
def save_block_result_to_cache(name, options)
|
584
|
-
result = instrument(:generate, name, options) do
|
592
|
+
result = instrument(:generate, name, options) do
|
585
593
|
yield(name)
|
586
594
|
end
|
587
595
|
|
@@ -599,7 +607,7 @@ module ActiveSupport
|
|
599
607
|
class Entry # :nodoc:
|
600
608
|
DEFAULT_COMPRESS_LIMIT = 16.kilobytes
|
601
609
|
|
602
|
-
#
|
610
|
+
# Creates a new cache entry for the specified value. Options supported are
|
603
611
|
# +:compress+, +:compress_threshold+, and +:expires_in+.
|
604
612
|
def initialize(value, options = {})
|
605
613
|
if should_compress?(value, options)
|
@@ -615,14 +623,12 @@ module ActiveSupport
|
|
615
623
|
end
|
616
624
|
|
617
625
|
def value
|
618
|
-
convert_version_4beta1_entry! if defined?(@v)
|
619
626
|
compressed? ? uncompress(@value) : @value
|
620
627
|
end
|
621
628
|
|
622
|
-
#
|
629
|
+
# Checks if the entry is expired. The +expires_in+ parameter can override
|
623
630
|
# the value set when the entry was created.
|
624
631
|
def expired?
|
625
|
-
convert_version_4beta1_entry! if defined?(@value)
|
626
632
|
@expires_in && @created_at + @expires_in <= Time.now.to_f
|
627
633
|
end
|
628
634
|
|
@@ -655,11 +661,9 @@ module ActiveSupport
|
|
655
661
|
end
|
656
662
|
end
|
657
663
|
|
658
|
-
#
|
664
|
+
# Duplicates the value in a class. This is used by cache implementations that don't natively
|
659
665
|
# serialize entries to protect against accidental cache modifications.
|
660
666
|
def dup_value!
|
661
|
-
convert_version_4beta1_entry! if defined?(@v)
|
662
|
-
|
663
667
|
if @value && !compressed? && !(@value.is_a?(Numeric) || @value == true || @value == false)
|
664
668
|
if @value.is_a?(String)
|
665
669
|
@value = @value.dup
|
@@ -692,26 +696,6 @@ module ActiveSupport
|
|
692
696
|
def uncompress(value)
|
693
697
|
Marshal.load(Zlib::Inflate.inflate(value))
|
694
698
|
end
|
695
|
-
|
696
|
-
# The internals of this method changed between Rails 3.x and 4.0. This method provides the glue
|
697
|
-
# to ensure that cache entries created under the old version still work with the new class definition.
|
698
|
-
def convert_version_4beta1_entry!
|
699
|
-
if defined?(@v)
|
700
|
-
@value = @v
|
701
|
-
remove_instance_variable(:@v)
|
702
|
-
end
|
703
|
-
|
704
|
-
if defined?(@c)
|
705
|
-
@compressed = @c
|
706
|
-
remove_instance_variable(:@c)
|
707
|
-
end
|
708
|
-
|
709
|
-
if defined?(@x) && @x
|
710
|
-
@created_at ||= Time.now.to_f
|
711
|
-
@expires_in = @x - @created_at
|
712
|
-
remove_instance_variable(:@x)
|
713
|
-
end
|
714
|
-
end
|
715
699
|
end
|
716
700
|
end
|
717
701
|
end
|