csd 0.1.5 → 0.1.6
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.
- data/COPYING +1 -2
- data/README.rdoc +7 -1
- data/Rakefile +7 -2
- data/VERSION +1 -1
- data/csd.gemspec +18 -216
- data/lib/csd.rb +6 -16
- data/lib/csd/application.rb +2 -2
- data/lib/csd/application/default.rb +15 -14
- data/lib/csd/application/default/base.rb +1 -1
- data/lib/csd/application/minisip.rb +43 -11
- data/lib/csd/application/minisip/about.yml +5 -5
- data/lib/csd/application/minisip/base.rb +82 -70
- data/lib/csd/application/minisip/error.rb +1 -1
- data/lib/csd/application/minisip/options/common.rb +1 -1
- data/lib/csd/application/minisip/options/compile.rb +1 -1
- data/lib/csd/application/minisip/options/package.rb +1 -1
- data/lib/csd/application/minisip/unix.rb +40 -27
- data/lib/csd/application/minisip/unix/darwin.rb +12 -0
- data/lib/csd/application/minisip/unix/linux.rb +1 -11
- data/lib/csd/application/minisip/unix/linux/debian.rb +14 -25
- data/lib/csd/application/minisip/unix/linux/debian/ubuntu10.rb +12 -13
- data/lib/csd/applications.rb +5 -5
- data/lib/csd/commands.rb +164 -89
- data/lib/csd/container.rb +61 -0
- data/lib/csd/error.rb +10 -1
- data/lib/csd/extensions.rb +3 -2
- data/lib/csd/extensions/core/array.rb +29 -1
- data/lib/csd/extensions/core/dir.rb +15 -24
- data/lib/csd/extensions/core/object.rb +10 -2
- data/lib/csd/extensions/core/option_parser.rb +22 -3
- data/lib/csd/extensions/core/pathname.rb +30 -2
- data/lib/csd/extensions/core/string.rb +51 -4
- data/lib/csd/extensions/gem/platform.rb +18 -3
- data/lib/csd/{options.rb → options_parser.rb} +16 -20
- data/lib/csd/path_container.rb +24 -0
- data/lib/csd/user_interface.rb +2 -0
- data/lib/csd/user_interface/base.rb +26 -0
- data/lib/csd/user_interface/cli.rb +37 -0
- data/lib/csd/vendor/active_support/MIT-LICENSE +20 -0
- data/lib/csd/vendor/active_support/inflector.rb +155 -0
- data/lib/csd/vendor/term/GPL2-LICENSE +339 -0
- data/lib/csd/vendor/term/ansicolor.rb +113 -0
- data/lib/csd/version.rb +13 -4
- data/test/application/test_minisip.rb +45 -0
- data/test/functional/test_applications.rb +10 -16
- data/test/functional/test_commands.rb +260 -23
- data/test/unit/test_string.rb +1 -1
- metadata +18 -216
- data/lib/active_support.rb +0 -75
- data/lib/active_support/all.rb +0 -3
- data/lib/active_support/backtrace_cleaner.rb +0 -94
- data/lib/active_support/base64.rb +0 -42
- data/lib/active_support/basic_object.rb +0 -21
- data/lib/active_support/benchmarkable.rb +0 -60
- data/lib/active_support/buffered_logger.rb +0 -132
- data/lib/active_support/builder.rb +0 -6
- data/lib/active_support/cache.rb +0 -626
- data/lib/active_support/cache/compressed_mem_cache_store.rb +0 -13
- data/lib/active_support/cache/file_store.rb +0 -188
- data/lib/active_support/cache/mem_cache_store.rb +0 -191
- data/lib/active_support/cache/memory_store.rb +0 -159
- data/lib/active_support/cache/strategy/local_cache.rb +0 -164
- data/lib/active_support/cache/synchronized_memory_store.rb +0 -11
- data/lib/active_support/callbacks.rb +0 -600
- data/lib/active_support/concern.rb +0 -29
- data/lib/active_support/configurable.rb +0 -36
- data/lib/active_support/core_ext.rb +0 -3
- data/lib/active_support/core_ext/array.rb +0 -7
- data/lib/active_support/core_ext/array/access.rb +0 -46
- data/lib/active_support/core_ext/array/conversions.rb +0 -164
- data/lib/active_support/core_ext/array/extract_options.rb +0 -29
- data/lib/active_support/core_ext/array/grouping.rb +0 -100
- data/lib/active_support/core_ext/array/random_access.rb +0 -20
- data/lib/active_support/core_ext/array/uniq_by.rb +0 -17
- data/lib/active_support/core_ext/array/wrap.rb +0 -22
- data/lib/active_support/core_ext/benchmark.rb +0 -7
- data/lib/active_support/core_ext/big_decimal.rb +0 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +0 -27
- data/lib/active_support/core_ext/cgi.rb +0 -1
- data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +0 -19
- data/lib/active_support/core_ext/class.rb +0 -4
- data/lib/active_support/core_ext/class/attribute.rb +0 -67
- data/lib/active_support/core_ext/class/attribute_accessors.rb +0 -63
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -44
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +0 -232
- data/lib/active_support/core_ext/class/subclasses.rb +0 -55
- data/lib/active_support/core_ext/date/acts_like.rb +0 -8
- data/lib/active_support/core_ext/date/calculations.rb +0 -240
- data/lib/active_support/core_ext/date/conversions.rb +0 -99
- data/lib/active_support/core_ext/date/freeze.rb +0 -31
- data/lib/active_support/core_ext/date_time/acts_like.rb +0 -13
- data/lib/active_support/core_ext/date_time/calculations.rb +0 -113
- data/lib/active_support/core_ext/date_time/conversions.rb +0 -102
- data/lib/active_support/core_ext/date_time/zones.rb +0 -17
- data/lib/active_support/core_ext/enumerable.rb +0 -119
- data/lib/active_support/core_ext/exception.rb +0 -3
- data/lib/active_support/core_ext/file.rb +0 -2
- data/lib/active_support/core_ext/file/atomic.rb +0 -41
- data/lib/active_support/core_ext/file/path.rb +0 -5
- data/lib/active_support/core_ext/float.rb +0 -1
- data/lib/active_support/core_ext/float/rounding.rb +0 -19
- data/lib/active_support/core_ext/hash.rb +0 -8
- data/lib/active_support/core_ext/hash/conversions.rb +0 -150
- data/lib/active_support/core_ext/hash/deep_merge.rb +0 -16
- data/lib/active_support/core_ext/hash/diff.rb +0 -13
- data/lib/active_support/core_ext/hash/except.rb +0 -24
- data/lib/active_support/core_ext/hash/indifferent_access.rb +0 -14
- data/lib/active_support/core_ext/hash/keys.rb +0 -45
- data/lib/active_support/core_ext/hash/reverse_merge.rb +0 -28
- data/lib/active_support/core_ext/hash/slice.rb +0 -38
- data/lib/active_support/core_ext/integer.rb +0 -3
- data/lib/active_support/core_ext/integer/inflections.rb +0 -14
- data/lib/active_support/core_ext/integer/multiple.rb +0 -6
- data/lib/active_support/core_ext/integer/time.rb +0 -39
- data/lib/active_support/core_ext/kernel.rb +0 -5
- data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
- data/lib/active_support/core_ext/kernel/debugger.rb +0 -16
- data/lib/active_support/core_ext/kernel/reporting.rb +0 -62
- data/lib/active_support/core_ext/kernel/requires.rb +0 -26
- data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -13
- data/lib/active_support/core_ext/load_error.rb +0 -23
- data/lib/active_support/core_ext/logger.rb +0 -146
- data/lib/active_support/core_ext/module.rb +0 -12
- data/lib/active_support/core_ext/module/aliasing.rb +0 -70
- data/lib/active_support/core_ext/module/anonymous.rb +0 -24
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +0 -31
- data/lib/active_support/core_ext/module/attr_internal.rb +0 -32
- data/lib/active_support/core_ext/module/attribute_accessors.rb +0 -66
- data/lib/active_support/core_ext/module/delegation.rb +0 -146
- data/lib/active_support/core_ext/module/deprecation.rb +0 -9
- data/lib/active_support/core_ext/module/introspection.rb +0 -88
- data/lib/active_support/core_ext/module/method_names.rb +0 -14
- data/lib/active_support/core_ext/module/reachable.rb +0 -10
- data/lib/active_support/core_ext/module/remove_method.rb +0 -6
- data/lib/active_support/core_ext/module/synchronization.rb +0 -42
- data/lib/active_support/core_ext/name_error.rb +0 -18
- data/lib/active_support/core_ext/numeric.rb +0 -2
- data/lib/active_support/core_ext/numeric/bytes.rb +0 -44
- data/lib/active_support/core_ext/numeric/time.rb +0 -77
- data/lib/active_support/core_ext/object.rb +0 -14
- data/lib/active_support/core_ext/object/acts_like.rb +0 -10
- data/lib/active_support/core_ext/object/blank.rb +0 -76
- data/lib/active_support/core_ext/object/conversions.rb +0 -4
- data/lib/active_support/core_ext/object/duplicable.rb +0 -65
- data/lib/active_support/core_ext/object/extending.rb +0 -11
- data/lib/active_support/core_ext/object/instance_variables.rb +0 -67
- data/lib/active_support/core_ext/object/misc.rb +0 -2
- data/lib/active_support/core_ext/object/returning.rb +0 -42
- data/lib/active_support/core_ext/object/to_param.rb +0 -49
- data/lib/active_support/core_ext/object/to_query.rb +0 -27
- data/lib/active_support/core_ext/object/try.rb +0 -36
- data/lib/active_support/core_ext/object/with_options.rb +0 -26
- data/lib/active_support/core_ext/proc.rb +0 -14
- data/lib/active_support/core_ext/process.rb +0 -1
- data/lib/active_support/core_ext/process/daemon.rb +0 -23
- data/lib/active_support/core_ext/range.rb +0 -4
- data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
- data/lib/active_support/core_ext/range/conversions.rb +0 -21
- data/lib/active_support/core_ext/range/include_range.rb +0 -21
- data/lib/active_support/core_ext/range/overlaps.rb +0 -8
- data/lib/active_support/core_ext/regexp.rb +0 -5
- data/lib/active_support/core_ext/rexml.rb +0 -46
- data/lib/active_support/core_ext/string.rb +0 -12
- data/lib/active_support/core_ext/string/access.rb +0 -99
- data/lib/active_support/core_ext/string/behavior.rb +0 -7
- data/lib/active_support/core_ext/string/conversions.rb +0 -61
- data/lib/active_support/core_ext/string/encoding.rb +0 -11
- data/lib/active_support/core_ext/string/exclude.rb +0 -6
- data/lib/active_support/core_ext/string/filters.rb +0 -49
- data/lib/active_support/core_ext/string/inflections.rb +0 -149
- data/lib/active_support/core_ext/string/interpolation.rb +0 -2
- data/lib/active_support/core_ext/string/multibyte.rb +0 -72
- data/lib/active_support/core_ext/string/output_safety.rb +0 -109
- data/lib/active_support/core_ext/string/starts_ends_with.rb +0 -4
- data/lib/active_support/core_ext/string/xchar.rb +0 -18
- data/lib/active_support/core_ext/time/acts_like.rb +0 -8
- data/lib/active_support/core_ext/time/calculations.rb +0 -282
- data/lib/active_support/core_ext/time/conversions.rb +0 -85
- data/lib/active_support/core_ext/time/marshal.rb +0 -56
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
- data/lib/active_support/core_ext/time/zones.rb +0 -78
- data/lib/active_support/core_ext/uri.rb +0 -22
- data/lib/active_support/dependencies.rb +0 -628
- data/lib/active_support/dependencies/autoload.rb +0 -50
- data/lib/active_support/deprecation.rb +0 -18
- data/lib/active_support/deprecation/behaviors.rb +0 -38
- data/lib/active_support/deprecation/method_wrappers.rb +0 -29
- data/lib/active_support/deprecation/proxy_wrappers.rb +0 -74
- data/lib/active_support/deprecation/reporting.rb +0 -56
- data/lib/active_support/duration.rb +0 -105
- data/lib/active_support/gzip.rb +0 -25
- data/lib/active_support/hash_with_indifferent_access.rb +0 -145
- data/lib/active_support/i18n.rb +0 -8
- data/lib/active_support/inflections.rb +0 -56
- data/lib/active_support/inflector.rb +0 -7
- data/lib/active_support/inflector/inflections.rb +0 -211
- data/lib/active_support/inflector/methods.rb +0 -141
- data/lib/active_support/inflector/transliterate.rb +0 -97
- data/lib/active_support/json.rb +0 -2
- data/lib/active_support/json/backends/jsongem.rb +0 -43
- data/lib/active_support/json/backends/yajl.rb +0 -40
- data/lib/active_support/json/backends/yaml.rb +0 -90
- data/lib/active_support/json/decoding.rb +0 -51
- data/lib/active_support/json/encoding.rb +0 -254
- data/lib/active_support/json/variable.rb +0 -11
- data/lib/active_support/lazy_load_hooks.rb +0 -27
- data/lib/active_support/locale/en.yml +0 -36
- data/lib/active_support/memoizable.rb +0 -103
- data/lib/active_support/message_encryptor.rb +0 -71
- data/lib/active_support/message_verifier.rb +0 -62
- data/lib/active_support/multibyte.rb +0 -44
- data/lib/active_support/multibyte/chars.rb +0 -480
- data/lib/active_support/multibyte/exceptions.rb +0 -8
- data/lib/active_support/multibyte/unicode.rb +0 -393
- data/lib/active_support/multibyte/utils.rb +0 -60
- data/lib/active_support/notifications.rb +0 -81
- data/lib/active_support/notifications/fanout.rb +0 -93
- data/lib/active_support/notifications/instrumenter.rb +0 -56
- data/lib/active_support/option_merger.rb +0 -25
- data/lib/active_support/ordered_hash.rb +0 -158
- data/lib/active_support/ordered_options.rb +0 -27
- data/lib/active_support/railtie.rb +0 -100
- data/lib/active_support/rescuable.rb +0 -114
- data/lib/active_support/ruby/shim.rb +0 -22
- data/lib/active_support/secure_random.rb +0 -199
- data/lib/active_support/string_inquirer.rb +0 -21
- data/lib/active_support/test_case.rb +0 -42
- data/lib/active_support/testing/assertions.rb +0 -82
- data/lib/active_support/testing/declarative.rb +0 -40
- data/lib/active_support/testing/default.rb +0 -9
- data/lib/active_support/testing/deprecation.rb +0 -55
- data/lib/active_support/testing/isolation.rb +0 -154
- data/lib/active_support/testing/pending.rb +0 -48
- data/lib/active_support/testing/performance.rb +0 -455
- data/lib/active_support/testing/setup_and_teardown.rb +0 -111
- data/lib/active_support/time.rb +0 -34
- data/lib/active_support/time/autoload.rb +0 -5
- data/lib/active_support/time_with_zone.rb +0 -341
- data/lib/active_support/values/time_zone.rb +0 -377
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +0 -10
- data/lib/active_support/whiny_nil.rb +0 -60
- data/lib/active_support/xml_mini.rb +0 -158
- data/lib/active_support/xml_mini/jdom.rb +0 -168
- data/lib/active_support/xml_mini/libxml.rb +0 -80
- data/lib/active_support/xml_mini/libxmlsax.rb +0 -85
- data/lib/active_support/xml_mini/nokogiri.rb +0 -78
- data/lib/active_support/xml_mini/nokogirisax.rb +0 -83
- data/lib/active_support/xml_mini/rexml.rb +0 -129
- data/lib/csd/extensions/core/file.rb +0 -14
- data/lib/csd/global_open_struct.rb +0 -19
- data/lib/csd/path.rb +0 -32
- data/lib/csd/ui.rb +0 -2
- data/lib/csd/ui/cli.rb +0 -8
- data/lib/csd/ui/ui.rb +0 -45
- data/lib/term/ansicolor.rb +0 -102
- data/lib/term/ansicolor/.keep +0 -0
- data/lib/term/ansicolor/version.rb +0 -10
@@ -1,42 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'base64'
|
3
|
-
rescue LoadError
|
4
|
-
end
|
5
|
-
|
6
|
-
module ActiveSupport
|
7
|
-
if defined? ::Base64
|
8
|
-
Base64 = ::Base64
|
9
|
-
else
|
10
|
-
# Base64 provides utility methods for encoding and de-coding binary data
|
11
|
-
# using a base 64 representation. A base 64 representation of binary data
|
12
|
-
# consists entirely of printable US-ASCII characters. The Base64 module
|
13
|
-
# is included in Ruby 1.8, but has been removed in Ruby 1.9.
|
14
|
-
module Base64
|
15
|
-
# Encodes a string to its base 64 representation. Each 60 characters of
|
16
|
-
# output is separated by a newline character.
|
17
|
-
#
|
18
|
-
# ActiveSupport::Base64.encode64("Original unencoded string")
|
19
|
-
# # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==\n"
|
20
|
-
def self.encode64(data)
|
21
|
-
[data].pack("m")
|
22
|
-
end
|
23
|
-
|
24
|
-
# Decodes a base 64 encoded string to its original representation.
|
25
|
-
#
|
26
|
-
# ActiveSupport::Base64.decode64("T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==")
|
27
|
-
# # => "Original unencoded string"
|
28
|
-
def self.decode64(data)
|
29
|
-
data.unpack("m").first
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# Encodes the value as base64 without the newline breaks. This makes the base64 encoding readily usable as URL parameters
|
35
|
-
# or memcache keys without further processing.
|
36
|
-
#
|
37
|
-
# ActiveSupport::Base64.encode64s("Original unencoded string")
|
38
|
-
# # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw=="
|
39
|
-
def Base64.encode64s(value)
|
40
|
-
encode64(value).gsub(/\n/, '')
|
41
|
-
end
|
42
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module ActiveSupport
|
2
|
-
if defined? ::BasicObject
|
3
|
-
# A class with no predefined methods that behaves similarly to Builder's
|
4
|
-
# BlankSlate. Used for proxy classes.
|
5
|
-
class BasicObject < ::BasicObject
|
6
|
-
undef_method :==
|
7
|
-
undef_method :equal?
|
8
|
-
|
9
|
-
# Let ActiveSupport::BasicObject at least raise exceptions.
|
10
|
-
def raise(*args)
|
11
|
-
::Object.send(:raise, *args)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
else
|
15
|
-
class BasicObject #:nodoc:
|
16
|
-
instance_methods.each do |m|
|
17
|
-
undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/benchmark'
|
2
|
-
require 'active_support/core_ext/hash/keys'
|
3
|
-
|
4
|
-
module ActiveSupport
|
5
|
-
module Benchmarkable
|
6
|
-
# Allows you to measure the execution time of a block
|
7
|
-
# in a template and records the result to the log. Wrap this block around
|
8
|
-
# expensive operations or possible bottlenecks to get a time reading
|
9
|
-
# for the operation. For example, let's say you thought your file
|
10
|
-
# processing method was taking too long; you could wrap it in a benchmark block.
|
11
|
-
#
|
12
|
-
# <% benchmark "Process data files" do %>
|
13
|
-
# <%= expensive_files_operation %>
|
14
|
-
# <% end %>
|
15
|
-
#
|
16
|
-
# That would add something like "Process data files (345.2ms)" to the log,
|
17
|
-
# which you can then use to compare timings when optimizing your code.
|
18
|
-
#
|
19
|
-
# You may give an optional logger level as the :level option.
|
20
|
-
# (:debug, :info, :warn, :error); the default value is :info.
|
21
|
-
#
|
22
|
-
# <% benchmark "Low-level files", :level => :debug do %>
|
23
|
-
# <%= lowlevel_files_operation %>
|
24
|
-
# <% end %>
|
25
|
-
#
|
26
|
-
# Finally, you can pass true as the third argument to silence all log activity
|
27
|
-
# inside the block. This is great for boiling down a noisy block to just a single statement:
|
28
|
-
#
|
29
|
-
# <% benchmark "Process data files", :level => :info, :silence => true do %>
|
30
|
-
# <%= expensive_and_chatty_files_operation %>
|
31
|
-
# <% end %>
|
32
|
-
def benchmark(message = "Benchmarking", options = {})
|
33
|
-
if logger
|
34
|
-
if options.is_a?(Symbol)
|
35
|
-
ActiveSupport::Deprecation.warn("use benchmark('#{message}', :level => :#{options}) instead", caller)
|
36
|
-
options = { :level => options, :silence => false }
|
37
|
-
else
|
38
|
-
options.assert_valid_keys(:level, :silence)
|
39
|
-
options[:level] ||= :info
|
40
|
-
end
|
41
|
-
|
42
|
-
result = nil
|
43
|
-
ms = Benchmark.ms { result = options[:silence] ? logger.silence { yield } : yield }
|
44
|
-
logger.send(options[:level], '%s (%.1fms)' % [ message, ms ])
|
45
|
-
result
|
46
|
-
else
|
47
|
-
yield
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
# Silence the logger during the execution of the block.
|
52
|
-
#
|
53
|
-
def silence
|
54
|
-
old_logger_level, logger.level = logger.level, ::Logger::ERROR if logger
|
55
|
-
yield
|
56
|
-
ensure
|
57
|
-
logger.level = old_logger_level if logger
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/class/attribute_accessors'
|
2
|
-
|
3
|
-
module ActiveSupport
|
4
|
-
# Inspired by the buffered logger idea by Ezra
|
5
|
-
class BufferedLogger
|
6
|
-
module Severity
|
7
|
-
DEBUG = 0
|
8
|
-
INFO = 1
|
9
|
-
WARN = 2
|
10
|
-
ERROR = 3
|
11
|
-
FATAL = 4
|
12
|
-
UNKNOWN = 5
|
13
|
-
end
|
14
|
-
include Severity
|
15
|
-
|
16
|
-
MAX_BUFFER_SIZE = 1000
|
17
|
-
|
18
|
-
##
|
19
|
-
# :singleton-method:
|
20
|
-
# Set to false to disable the silencer
|
21
|
-
cattr_accessor :silencer
|
22
|
-
self.silencer = true
|
23
|
-
|
24
|
-
# Silences the logger for the duration of the block.
|
25
|
-
def silence(temporary_level = ERROR)
|
26
|
-
if silencer
|
27
|
-
begin
|
28
|
-
old_logger_level, self.level = level, temporary_level
|
29
|
-
yield self
|
30
|
-
ensure
|
31
|
-
self.level = old_logger_level
|
32
|
-
end
|
33
|
-
else
|
34
|
-
yield self
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
attr_accessor :level
|
39
|
-
attr_reader :auto_flushing
|
40
|
-
|
41
|
-
def initialize(log, level = DEBUG)
|
42
|
-
@level = level
|
43
|
-
@buffer = {}
|
44
|
-
@auto_flushing = 1
|
45
|
-
@guard = Mutex.new
|
46
|
-
|
47
|
-
if log.respond_to?(:write)
|
48
|
-
@log = log
|
49
|
-
elsif File.exist?(log)
|
50
|
-
@log = open(log, (File::WRONLY | File::APPEND))
|
51
|
-
@log.sync = true
|
52
|
-
else
|
53
|
-
FileUtils.mkdir_p(File.dirname(log))
|
54
|
-
@log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
|
55
|
-
@log.sync = true
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def add(severity, message = nil, progname = nil, &block)
|
60
|
-
return if @level > severity
|
61
|
-
message = (message || (block && block.call) || progname).to_s
|
62
|
-
# If a newline is necessary then create a new message ending with a newline.
|
63
|
-
# Ensures that the original message is not mutated.
|
64
|
-
message = "#{message}\n" unless message[-1] == ?\n
|
65
|
-
buffer << message
|
66
|
-
auto_flush
|
67
|
-
message
|
68
|
-
end
|
69
|
-
|
70
|
-
# Dynamically add methods such as:
|
71
|
-
# def info
|
72
|
-
# def warn
|
73
|
-
# def debug
|
74
|
-
for severity in Severity.constants
|
75
|
-
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
76
|
-
def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
|
77
|
-
add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
|
78
|
-
end # end
|
79
|
-
|
80
|
-
def #{severity.downcase}? # def debug?
|
81
|
-
#{severity} >= @level # DEBUG >= @level
|
82
|
-
end # end
|
83
|
-
EOT
|
84
|
-
end
|
85
|
-
|
86
|
-
# Set the auto-flush period. Set to true to flush after every log message,
|
87
|
-
# to an integer to flush every N messages, or to false, nil, or zero to
|
88
|
-
# never auto-flush. If you turn auto-flushing off, be sure to regularly
|
89
|
-
# flush the log yourself -- it will eat up memory until you do.
|
90
|
-
def auto_flushing=(period)
|
91
|
-
@auto_flushing =
|
92
|
-
case period
|
93
|
-
when true; 1
|
94
|
-
when false, nil, 0; MAX_BUFFER_SIZE
|
95
|
-
when Integer; period
|
96
|
-
else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
def flush
|
101
|
-
@guard.synchronize do
|
102
|
-
unless buffer.empty?
|
103
|
-
old_buffer = buffer
|
104
|
-
@log.write(old_buffer.join)
|
105
|
-
end
|
106
|
-
|
107
|
-
# Important to do this even if buffer was empty or else @buffer will
|
108
|
-
# accumulate empty arrays for each request where nothing was logged.
|
109
|
-
clear_buffer
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
def close
|
114
|
-
flush
|
115
|
-
@log.close if @log.respond_to?(:close)
|
116
|
-
@log = nil
|
117
|
-
end
|
118
|
-
|
119
|
-
protected
|
120
|
-
def auto_flush
|
121
|
-
flush if buffer.size >= @auto_flushing
|
122
|
-
end
|
123
|
-
|
124
|
-
def buffer
|
125
|
-
@buffer[Thread.current] ||= []
|
126
|
-
end
|
127
|
-
|
128
|
-
def clear_buffer
|
129
|
-
@buffer.delete(Thread.current)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
data/lib/active_support/cache.rb
DELETED
@@ -1,626 +0,0 @@
|
|
1
|
-
require 'benchmark'
|
2
|
-
require 'zlib'
|
3
|
-
require 'active_support/core_ext/array/extract_options'
|
4
|
-
require 'active_support/core_ext/array/wrap'
|
5
|
-
require 'active_support/core_ext/benchmark'
|
6
|
-
require 'active_support/core_ext/exception'
|
7
|
-
require 'active_support/core_ext/class/attribute_accessors'
|
8
|
-
require 'active_support/core_ext/numeric/bytes'
|
9
|
-
require 'active_support/core_ext/numeric/time'
|
10
|
-
require 'active_support/core_ext/object/to_param'
|
11
|
-
require 'active_support/core_ext/string/inflections'
|
12
|
-
|
13
|
-
module ActiveSupport
|
14
|
-
# See ActiveSupport::Cache::Store for documentation.
|
15
|
-
module Cache
|
16
|
-
autoload :FileStore, 'active_support/cache/file_store'
|
17
|
-
autoload :MemoryStore, 'active_support/cache/memory_store'
|
18
|
-
autoload :MemCacheStore, 'active_support/cache/mem_cache_store'
|
19
|
-
autoload :SynchronizedMemoryStore, 'active_support/cache/synchronized_memory_store'
|
20
|
-
autoload :CompressedMemCacheStore, 'active_support/cache/compressed_mem_cache_store'
|
21
|
-
|
22
|
-
EMPTY_OPTIONS = {}.freeze
|
23
|
-
|
24
|
-
# These options mean something to all cache implementations. Individual cache
|
25
|
-
# implementations may support additional optons.
|
26
|
-
UNIVERSAL_OPTIONS = [:namespace, :compress, :compress_threshold, :expires_in, :race_condition_ttl]
|
27
|
-
|
28
|
-
module Strategy
|
29
|
-
autoload :LocalCache, 'active_support/cache/strategy/local_cache'
|
30
|
-
end
|
31
|
-
|
32
|
-
# Creates a new CacheStore object according to the given options.
|
33
|
-
#
|
34
|
-
# If no arguments are passed to this method, then a new
|
35
|
-
# ActiveSupport::Cache::MemoryStore object will be returned.
|
36
|
-
#
|
37
|
-
# If you pass a Symbol as the first argument, then a corresponding cache
|
38
|
-
# store class under the ActiveSupport::Cache namespace will be created.
|
39
|
-
# For example:
|
40
|
-
#
|
41
|
-
# ActiveSupport::Cache.lookup_store(:memory_store)
|
42
|
-
# # => returns a new ActiveSupport::Cache::MemoryStore object
|
43
|
-
#
|
44
|
-
# ActiveSupport::Cache.lookup_store(:mem_cache_store)
|
45
|
-
# # => returns a new ActiveSupport::Cache::MemCacheStore object
|
46
|
-
#
|
47
|
-
# Any additional arguments will be passed to the corresponding cache store
|
48
|
-
# class's constructor:
|
49
|
-
#
|
50
|
-
# ActiveSupport::Cache.lookup_store(:file_store, "/tmp/cache")
|
51
|
-
# # => same as: ActiveSupport::Cache::FileStore.new("/tmp/cache")
|
52
|
-
#
|
53
|
-
# If the first argument is not a Symbol, then it will simply be returned:
|
54
|
-
#
|
55
|
-
# ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new)
|
56
|
-
# # => returns MyOwnCacheStore.new
|
57
|
-
def self.lookup_store(*store_option)
|
58
|
-
store, *parameters = *Array.wrap(store_option).flatten
|
59
|
-
|
60
|
-
case store
|
61
|
-
when Symbol
|
62
|
-
store_class_name = store.to_s.camelize
|
63
|
-
store_class = ActiveSupport::Cache.const_get(store_class_name)
|
64
|
-
store_class.new(*parameters)
|
65
|
-
when nil
|
66
|
-
ActiveSupport::Cache::MemoryStore.new
|
67
|
-
else
|
68
|
-
store
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.expand_cache_key(key, namespace = nil)
|
73
|
-
expanded_cache_key = namespace ? "#{namespace}/" : ""
|
74
|
-
|
75
|
-
prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
|
76
|
-
if prefix
|
77
|
-
expanded_cache_key << "#{prefix}/"
|
78
|
-
end
|
79
|
-
|
80
|
-
expanded_cache_key <<
|
81
|
-
if key.respond_to?(:cache_key)
|
82
|
-
key.cache_key
|
83
|
-
elsif key.is_a?(Array)
|
84
|
-
if key.size > 1
|
85
|
-
key.collect { |element| expand_cache_key(element) }.to_param
|
86
|
-
else
|
87
|
-
key.first.to_param
|
88
|
-
end
|
89
|
-
elsif key
|
90
|
-
key.to_param
|
91
|
-
end.to_s
|
92
|
-
|
93
|
-
expanded_cache_key
|
94
|
-
end
|
95
|
-
|
96
|
-
# An abstract cache store class. There are multiple cache store
|
97
|
-
# implementations, each having its own additional features. See the classes
|
98
|
-
# under the ActiveSupport::Cache module, e.g.
|
99
|
-
# ActiveSupport::Cache::MemCacheStore. MemCacheStore is currently the most
|
100
|
-
# popular cache store for large production websites.
|
101
|
-
#
|
102
|
-
# Some implementations may not support all methods beyond the basic cache
|
103
|
-
# methods of +fetch+, +write+, +read+, +exist?+, and +delete+.
|
104
|
-
#
|
105
|
-
# ActiveSupport::Cache::Store can store any serializable Ruby object.
|
106
|
-
#
|
107
|
-
# cache = ActiveSupport::Cache::MemoryStore.new
|
108
|
-
#
|
109
|
-
# cache.read("city") # => nil
|
110
|
-
# cache.write("city", "Duckburgh")
|
111
|
-
# cache.read("city") # => "Duckburgh"
|
112
|
-
#
|
113
|
-
# Keys are always translated into Strings and are case sensitive. When an
|
114
|
-
# object is specified as a key, its +cache_key+ method will be called if it
|
115
|
-
# is defined. Otherwise, the +to_param+ method will be called. Hashes and
|
116
|
-
# Arrays can be used as keys. The elements will be delimited by slashes
|
117
|
-
# and Hashes elements will be sorted by key so they are consistent.
|
118
|
-
#
|
119
|
-
# cache.read("city") == cache.read(:city) # => true
|
120
|
-
#
|
121
|
-
# Nil values can be cached.
|
122
|
-
#
|
123
|
-
# If your cache is on a shared infrastructure, you can define a namespace for
|
124
|
-
# your cache entries. If a namespace is defined, it will be prefixed on to every
|
125
|
-
# key. The namespace can be either a static value or a Proc. If it is a Proc, it
|
126
|
-
# will be invoked when each key is evaluated so that you can use application logic
|
127
|
-
# to invalidate keys.
|
128
|
-
#
|
129
|
-
# cache.namespace = lambda { @last_mod_time } # Set the namespace to a variable
|
130
|
-
# @last_mod_time = Time.now # Invalidate the entire cache by changing namespace
|
131
|
-
#
|
132
|
-
# All caches support auto expiring content after a specified number of seconds.
|
133
|
-
# To set the cache entry time to live, you can either specify +:expires_in+ as
|
134
|
-
# an option to the constructor to have it affect all entries or to the +fetch+
|
135
|
-
# or +write+ methods for just one entry.
|
136
|
-
#
|
137
|
-
# cache = ActiveSupport::Cache::MemoryStore.new(:expire_in => 5.minutes)
|
138
|
-
# cache.write(key, value, :expire_in => 1.minute) # Set a lower value for one entry
|
139
|
-
#
|
140
|
-
# Caches can also store values in a compressed format to save space and reduce
|
141
|
-
# time spent sending data. Since there is some overhead, values must be large
|
142
|
-
# enough to warrant compression. To turn on compression either pass
|
143
|
-
# <tt>:compress => true</tt> in the initializer or to +fetch+ or +write+.
|
144
|
-
# To specify the threshold at which to compress values, set
|
145
|
-
# <tt>:compress_threshold</tt>. The default threshold is 32K.
|
146
|
-
class Store
|
147
|
-
|
148
|
-
cattr_accessor :logger, :instance_writer => true
|
149
|
-
|
150
|
-
attr_reader :silence
|
151
|
-
alias :silence? :silence
|
152
|
-
|
153
|
-
# Create a new cache. The options will be passed to any write method calls except
|
154
|
-
# for :namespace which can be used to set the global namespace for the cache.
|
155
|
-
def initialize (options = nil)
|
156
|
-
@options = options ? options.dup : {}
|
157
|
-
end
|
158
|
-
|
159
|
-
# Get the default options set when the cache was created.
|
160
|
-
def options
|
161
|
-
@options ||= {}
|
162
|
-
end
|
163
|
-
|
164
|
-
# Silence the logger.
|
165
|
-
def silence!
|
166
|
-
@silence = true
|
167
|
-
self
|
168
|
-
end
|
169
|
-
|
170
|
-
# Silence the logger within a block.
|
171
|
-
def mute
|
172
|
-
previous_silence, @silence = defined?(@silence) && @silence, true
|
173
|
-
yield
|
174
|
-
ensure
|
175
|
-
@silence = previous_silence
|
176
|
-
end
|
177
|
-
|
178
|
-
# Set to true if cache stores should be instrumented. By default is false.
|
179
|
-
def self.instrument=(boolean)
|
180
|
-
Thread.current[:instrument_cache_store] = boolean
|
181
|
-
end
|
182
|
-
|
183
|
-
def self.instrument
|
184
|
-
Thread.current[:instrument_cache_store] || false
|
185
|
-
end
|
186
|
-
|
187
|
-
# Fetches data from the cache, using the given key. If there is data in
|
188
|
-
# the cache with the given key, then that data is returned.
|
189
|
-
#
|
190
|
-
# If there is no such data in the cache (a cache miss occurred), then
|
191
|
-
# then nil will be returned. However, if a block has been passed, then
|
192
|
-
# that block will be run in the event of a cache miss. The return value
|
193
|
-
# of the block will be written to the cache under the given cache key,
|
194
|
-
# and that return value will be returned.
|
195
|
-
#
|
196
|
-
# cache.write("today", "Monday")
|
197
|
-
# cache.fetch("today") # => "Monday"
|
198
|
-
#
|
199
|
-
# cache.fetch("city") # => nil
|
200
|
-
# cache.fetch("city") do
|
201
|
-
# "Duckburgh"
|
202
|
-
# end
|
203
|
-
# cache.fetch("city") # => "Duckburgh"
|
204
|
-
#
|
205
|
-
# You may also specify additional options via the +options+ argument.
|
206
|
-
# Setting <tt>:force => true</tt> will force a cache miss:
|
207
|
-
#
|
208
|
-
# cache.write("today", "Monday")
|
209
|
-
# cache.fetch("today", :force => true) # => nil
|
210
|
-
#
|
211
|
-
# Setting <tt>:compress</tt> will store a large cache entry set by the call
|
212
|
-
# in a compressed format.
|
213
|
-
#
|
214
|
-
# Setting <tt>:expires_in</tt> will set an expiration time on the cache
|
215
|
-
# entry if it is set by call.
|
216
|
-
#
|
217
|
-
# Setting <tt>:race_condition_ttl</tt> will invoke logic on entries set with
|
218
|
-
# an <tt>:expires_in</tt> option. If an entry is found in the cache that is
|
219
|
-
# expired and it has been expired for less than the number of seconds specified
|
220
|
-
# by this option and a block was passed to the method call, then the expiration
|
221
|
-
# future time of the entry in the cache will be updated to that many seconds
|
222
|
-
# in the and the block will be evaluated and written to the cache.
|
223
|
-
#
|
224
|
-
# This is very useful in situations where a cache entry is used very frequently
|
225
|
-
# under heavy load. The first process to find an expired cache entry will then
|
226
|
-
# become responsible for regenerating that entry while other processes continue
|
227
|
-
# to use the slightly out of date entry. This can prevent race conditions where
|
228
|
-
# too many processes are trying to regenerate the entry all at once. If the
|
229
|
-
# process regenerating the entry errors out, the entry will be regenerated
|
230
|
-
# after the specified number of seconds.
|
231
|
-
#
|
232
|
-
# # Set all values to expire after one minute.
|
233
|
-
# cache = ActiveSupport::Cache::MemoryCache.new(:expires_in => 1.minute)
|
234
|
-
#
|
235
|
-
# cache.write("foo", "original value")
|
236
|
-
# val_1 = nil
|
237
|
-
# val_2 = nil
|
238
|
-
# sleep 60
|
239
|
-
#
|
240
|
-
# Thread.new do
|
241
|
-
# val_1 = cache.fetch("foo", :race_condition_ttl => 10) do
|
242
|
-
# sleep 1
|
243
|
-
# "new value 1"
|
244
|
-
# end
|
245
|
-
# end
|
246
|
-
#
|
247
|
-
# Thread.new do
|
248
|
-
# val_2 = cache.fetch("foo", :race_condition_ttl => 10) do
|
249
|
-
# "new value 2"
|
250
|
-
# end
|
251
|
-
# end
|
252
|
-
#
|
253
|
-
# # val_1 => "new value 1"
|
254
|
-
# # val_2 => "original value"
|
255
|
-
# # cache.fetch("foo") => "new value 1"
|
256
|
-
#
|
257
|
-
# Other options will be handled by the specific cache store implementation.
|
258
|
-
# Internally, #fetch calls #read_entry, and calls #write_entry on a cache miss.
|
259
|
-
# +options+ will be passed to the #read and #write calls.
|
260
|
-
#
|
261
|
-
# For example, MemCacheStore's #write method supports the +:raw+
|
262
|
-
# option, which tells the memcached server to store all values as strings.
|
263
|
-
# We can use this option with #fetch too:
|
264
|
-
#
|
265
|
-
# cache = ActiveSupport::Cache::MemCacheStore.new
|
266
|
-
# cache.fetch("foo", :force => true, :raw => true) do
|
267
|
-
# :bar
|
268
|
-
# end
|
269
|
-
# cache.fetch("foo") # => "bar"
|
270
|
-
def fetch(name, options = nil, &block)
|
271
|
-
options = merged_options(options)
|
272
|
-
key = namespaced_key(name, options)
|
273
|
-
entry = instrument(:read, name, options) { read_entry(key, options) } unless options[:force]
|
274
|
-
if entry && entry.expired?
|
275
|
-
race_ttl = options[:race_condition_ttl].to_f
|
276
|
-
if race_ttl and Time.now.to_f - entry.expires_at <= race_ttl
|
277
|
-
entry.expires_at = Time.now + race_ttl
|
278
|
-
write_entry(key, entry, :expires_in => race_ttl * 2)
|
279
|
-
else
|
280
|
-
delete_entry(key, options)
|
281
|
-
end
|
282
|
-
entry = nil
|
283
|
-
end
|
284
|
-
|
285
|
-
if entry
|
286
|
-
entry.value
|
287
|
-
elsif block_given?
|
288
|
-
result = instrument(:generate, name, options, &block)
|
289
|
-
write(name, result, options)
|
290
|
-
result
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
# Fetches data from the cache, using the given key. If there is data in
|
295
|
-
# the cache with the given key, then that data is returned. Otherwise,
|
296
|
-
# nil is returned.
|
297
|
-
#
|
298
|
-
# Options are passed to the underlying cache implementation.
|
299
|
-
def read(name, options = nil)
|
300
|
-
options = merged_options(options)
|
301
|
-
key = namespaced_key(name, options)
|
302
|
-
instrument(:read, name, options) do
|
303
|
-
entry = read_entry(key, options)
|
304
|
-
if entry
|
305
|
-
if entry.expired?
|
306
|
-
delete_entry(key, options)
|
307
|
-
nil
|
308
|
-
else
|
309
|
-
entry.value
|
310
|
-
end
|
311
|
-
else
|
312
|
-
nil
|
313
|
-
end
|
314
|
-
end
|
315
|
-
end
|
316
|
-
|
317
|
-
# Read multiple values at once from the cache. Options can be passed
|
318
|
-
# in the last argument.
|
319
|
-
#
|
320
|
-
# Some cache implementation may optimize this method.
|
321
|
-
#
|
322
|
-
# Returns a hash mapping the names provided to the values found.
|
323
|
-
def read_multi(*names)
|
324
|
-
options = names.extract_options!
|
325
|
-
options = merged_options(options)
|
326
|
-
results = {}
|
327
|
-
names.each do |name|
|
328
|
-
key = namespaced_key(name, options)
|
329
|
-
entry = read_entry(key, options)
|
330
|
-
if entry
|
331
|
-
if entry.expired?
|
332
|
-
delete_entry(key)
|
333
|
-
else
|
334
|
-
results[name] = entry.value
|
335
|
-
end
|
336
|
-
end
|
337
|
-
end
|
338
|
-
results
|
339
|
-
end
|
340
|
-
|
341
|
-
# Writes the given value to the cache, with the given key.
|
342
|
-
#
|
343
|
-
# You may also specify additional options via the +options+ argument.
|
344
|
-
# The specific cache store implementation will decide what to do with
|
345
|
-
# +options+.
|
346
|
-
def write(name, value, options = nil)
|
347
|
-
options = merged_options(options)
|
348
|
-
instrument(:write, name, options) do
|
349
|
-
entry = Entry.new(value, options)
|
350
|
-
write_entry(namespaced_key(name, options), entry, options)
|
351
|
-
end
|
352
|
-
end
|
353
|
-
|
354
|
-
# Delete an entry in the cache. Returns +true+ if there was an entry to delete.
|
355
|
-
#
|
356
|
-
# Options are passed to the underlying cache implementation.
|
357
|
-
def delete(name, options = nil)
|
358
|
-
options = merged_options(options)
|
359
|
-
instrument(:delete, name) do
|
360
|
-
delete_entry(namespaced_key(name, options), options)
|
361
|
-
end
|
362
|
-
end
|
363
|
-
|
364
|
-
# Return true if the cache contains an entry with this name.
|
365
|
-
#
|
366
|
-
# Options are passed to the underlying cache implementation.
|
367
|
-
def exist?(name, options = nil)
|
368
|
-
options = merged_options(options)
|
369
|
-
instrument(:exist?, name) do
|
370
|
-
entry = read_entry(namespaced_key(name, options), options)
|
371
|
-
if entry && !entry.expired?
|
372
|
-
true
|
373
|
-
else
|
374
|
-
false
|
375
|
-
end
|
376
|
-
end
|
377
|
-
end
|
378
|
-
|
379
|
-
# Delete all entries whose keys match a pattern.
|
380
|
-
#
|
381
|
-
# Options are passed to the underlying cache implementation.
|
382
|
-
#
|
383
|
-
# Not all implementations may support +delete_matched+.
|
384
|
-
def delete_matched(matcher, options = nil)
|
385
|
-
raise NotImplementedError.new("#{self.class.name} does not support delete_matched")
|
386
|
-
end
|
387
|
-
|
388
|
-
# Increment an integer value in the cache.
|
389
|
-
#
|
390
|
-
# Options are passed to the underlying cache implementation.
|
391
|
-
#
|
392
|
-
# Not all implementations may support +delete_matched+.
|
393
|
-
def increment(name, amount = 1, options = nil)
|
394
|
-
raise NotImplementedError.new("#{self.class.name} does not support increment")
|
395
|
-
end
|
396
|
-
|
397
|
-
# Increment an integer value in the cache.
|
398
|
-
#
|
399
|
-
# Options are passed to the underlying cache implementation.
|
400
|
-
#
|
401
|
-
# Not all implementations may support +delete_matched+.
|
402
|
-
def decrement(name, amount = 1, options = nil)
|
403
|
-
raise NotImplementedError.new("#{self.class.name} does not support decrement")
|
404
|
-
end
|
405
|
-
|
406
|
-
# Cleanup the cache by removing expired entries. Not all cache implementations may
|
407
|
-
# support this method.
|
408
|
-
#
|
409
|
-
# Options are passed to the underlying cache implementation.
|
410
|
-
#
|
411
|
-
# Not all implementations may support +delete_matched+.
|
412
|
-
def cleanup(options = nil)
|
413
|
-
raise NotImplementedError.new("#{self.class.name} does not support cleanup")
|
414
|
-
end
|
415
|
-
|
416
|
-
# Clear the entire cache. Not all cache implementations may support this method.
|
417
|
-
# You should be careful with this method since it could affect other processes
|
418
|
-
# if you are using a shared cache.
|
419
|
-
#
|
420
|
-
# Options are passed to the underlying cache implementation.
|
421
|
-
#
|
422
|
-
# Not all implementations may support +delete_matched+.
|
423
|
-
def clear(options = nil)
|
424
|
-
raise NotImplementedError.new("#{self.class.name} does not support clear")
|
425
|
-
end
|
426
|
-
|
427
|
-
protected
|
428
|
-
# Add the namespace defined in the options to a pattern designed to match keys.
|
429
|
-
# Implementations that support delete_matched should call this method to translate
|
430
|
-
# a pattern that matches names into one that matches namespaced keys.
|
431
|
-
def key_matcher(pattern, options)
|
432
|
-
prefix = options[:namespace].is_a?(Proc) ? options[:namespace].call : options[:namespace]
|
433
|
-
if prefix
|
434
|
-
source = pattern.source
|
435
|
-
if source.start_with?('^')
|
436
|
-
source = source[1, source.length]
|
437
|
-
else
|
438
|
-
source = ".*#{source[0, source.length]}"
|
439
|
-
end
|
440
|
-
Regexp.new("^#{Regexp.escape(prefix)}:#{source}", pattern.options)
|
441
|
-
else
|
442
|
-
pattern
|
443
|
-
end
|
444
|
-
end
|
445
|
-
|
446
|
-
# Read an entry from the cache implementation. Subclasses must implement this method.
|
447
|
-
def read_entry(key, options) # :nodoc:
|
448
|
-
raise NotImplementedError.new
|
449
|
-
end
|
450
|
-
|
451
|
-
# Write an entry to the cache implementation. Subclasses must implement this method.
|
452
|
-
def write_entry(key, entry, options) # :nodoc:
|
453
|
-
raise NotImplementedError.new
|
454
|
-
end
|
455
|
-
|
456
|
-
# Delete an entry from the cache implementation. Subclasses must implement this method.
|
457
|
-
def delete_entry(key, options) # :nodoc:
|
458
|
-
raise NotImplementedError.new
|
459
|
-
end
|
460
|
-
|
461
|
-
private
|
462
|
-
# Merge the default options with ones specific to a method call.
|
463
|
-
def merged_options(call_options) # :nodoc:
|
464
|
-
if call_options
|
465
|
-
options.merge(call_options)
|
466
|
-
else
|
467
|
-
options.dup
|
468
|
-
end
|
469
|
-
end
|
470
|
-
|
471
|
-
# Expand a key to be a consistent string value. If the object responds to +cache_key+,
|
472
|
-
# it will be called. Otherwise, the to_param method will be called. If the key is a
|
473
|
-
# Hash, the keys will be sorted alphabetically.
|
474
|
-
def expanded_key(key) # :nodoc:
|
475
|
-
if key.respond_to?(:cache_key)
|
476
|
-
key = key.cache_key.to_s
|
477
|
-
elsif key.is_a?(Array)
|
478
|
-
if key.size > 1
|
479
|
-
key.collect{|element| expanded_key(element)}.to_param
|
480
|
-
else
|
481
|
-
key.first.to_param
|
482
|
-
end
|
483
|
-
elsif key.is_a?(Hash)
|
484
|
-
key = key.to_a.sort{|a,b| a.first.to_s <=> b.first.to_s}.collect{|k,v| "#{k}=#{v}"}.to_param
|
485
|
-
else
|
486
|
-
key = key.to_param
|
487
|
-
end
|
488
|
-
end
|
489
|
-
|
490
|
-
# Prefix a key with the namespace. The two values will be delimited with a colon.
|
491
|
-
def namespaced_key(key, options)
|
492
|
-
key = expanded_key(key)
|
493
|
-
namespace = options[:namespace] if options
|
494
|
-
prefix = namespace.is_a?(Proc) ? namespace.call : namespace
|
495
|
-
key = "#{prefix}:#{key}" if prefix
|
496
|
-
key
|
497
|
-
end
|
498
|
-
|
499
|
-
def instrument(operation, key, options = nil)
|
500
|
-
log(operation, key, options)
|
501
|
-
|
502
|
-
if self.class.instrument
|
503
|
-
payload = { :key => key }
|
504
|
-
payload.merge!(options) if options.is_a?(Hash)
|
505
|
-
ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload){ yield }
|
506
|
-
else
|
507
|
-
yield
|
508
|
-
end
|
509
|
-
end
|
510
|
-
|
511
|
-
def log(operation, key, options = nil)
|
512
|
-
return unless logger && logger.debug? && !silence?
|
513
|
-
logger.debug("Cache #{operation}: #{key}#{options.blank? ? "" : " (#{options.inspect})"}")
|
514
|
-
end
|
515
|
-
end
|
516
|
-
|
517
|
-
# Entry that is put into caches. It supports expiration time on entries and can compress values
|
518
|
-
# to save space in the cache.
|
519
|
-
class Entry
|
520
|
-
attr_reader :created_at, :expires_in
|
521
|
-
|
522
|
-
DEFAULT_COMPRESS_LIMIT = 16.kilobytes
|
523
|
-
|
524
|
-
class << self
|
525
|
-
# Create an entry with internal attributes set. This method is intended to be
|
526
|
-
# used by implementations that store cache entries in a native format instead
|
527
|
-
# of as serialized Ruby objects.
|
528
|
-
def create (raw_value, created_at, options = {})
|
529
|
-
entry = new(nil)
|
530
|
-
entry.instance_variable_set(:@value, raw_value)
|
531
|
-
entry.instance_variable_set(:@created_at, created_at.to_f)
|
532
|
-
entry.instance_variable_set(:@compressed, !!options[:compressed])
|
533
|
-
entry.instance_variable_set(:@expires_in, options[:expires_in])
|
534
|
-
entry
|
535
|
-
end
|
536
|
-
end
|
537
|
-
|
538
|
-
# Create a new cache entry for the specified value. Options supported are
|
539
|
-
# +:compress+, +:compress_threshold+, and +:expires_in+.
|
540
|
-
def initialize(value, options = {})
|
541
|
-
@compressed = false
|
542
|
-
@expires_in = options[:expires_in]
|
543
|
-
@expires_in = @expires_in.to_f if @expires_in
|
544
|
-
@created_at = Time.now.to_f
|
545
|
-
if value
|
546
|
-
if should_compress?(value, options)
|
547
|
-
@value = Zlib::Deflate.deflate(Marshal.dump(value))
|
548
|
-
@compressed = true
|
549
|
-
else
|
550
|
-
@value = value
|
551
|
-
end
|
552
|
-
else
|
553
|
-
@value = nil
|
554
|
-
end
|
555
|
-
end
|
556
|
-
|
557
|
-
# Get the raw value. This value may be serialized and compressed.
|
558
|
-
def raw_value
|
559
|
-
@value
|
560
|
-
end
|
561
|
-
|
562
|
-
# Get the value stored in the cache.
|
563
|
-
def value
|
564
|
-
if @value
|
565
|
-
val = compressed? ? Marshal.load(Zlib::Inflate.inflate(@value)) : @value
|
566
|
-
unless val.frozen?
|
567
|
-
val.freeze rescue nil
|
568
|
-
end
|
569
|
-
val
|
570
|
-
end
|
571
|
-
end
|
572
|
-
|
573
|
-
def compressed?
|
574
|
-
@compressed
|
575
|
-
end
|
576
|
-
|
577
|
-
# Check if the entry is expired. The +expires_in+ parameter can override the
|
578
|
-
# value set when the entry was created.
|
579
|
-
def expired?
|
580
|
-
if @expires_in && @created_at + @expires_in <= Time.now.to_f
|
581
|
-
true
|
582
|
-
else
|
583
|
-
false
|
584
|
-
end
|
585
|
-
end
|
586
|
-
|
587
|
-
# Set a new time to live on the entry so it expires at the given time.
|
588
|
-
def expires_at=(time)
|
589
|
-
if time
|
590
|
-
@expires_in = time.to_f - @created_at
|
591
|
-
else
|
592
|
-
@expires_in = nil
|
593
|
-
end
|
594
|
-
end
|
595
|
-
|
596
|
-
# Seconds since the epoch when the cache entry will expire.
|
597
|
-
def expires_at
|
598
|
-
@expires_in ? @created_at + @expires_in : nil
|
599
|
-
end
|
600
|
-
|
601
|
-
# Get the size of the cached value. This could be less than value.size
|
602
|
-
# if the data is compressed.
|
603
|
-
def size
|
604
|
-
if @value.nil?
|
605
|
-
0
|
606
|
-
elsif @value.respond_to?(:bytesize)
|
607
|
-
@value.bytesize
|
608
|
-
else
|
609
|
-
Marshal.dump(@value).bytesize
|
610
|
-
end
|
611
|
-
end
|
612
|
-
|
613
|
-
private
|
614
|
-
def should_compress?(value, options)
|
615
|
-
if options[:compress] && value
|
616
|
-
unless value.is_a?(Numeric)
|
617
|
-
compress_threshold = options[:compress_threshold] || DEFAULT_COMPRESS_LIMIT
|
618
|
-
serialized_value = value.is_a?(String) ? value : Marshal.dump(value)
|
619
|
-
return true if serialized_value.size >= compress_threshold
|
620
|
-
end
|
621
|
-
end
|
622
|
-
false
|
623
|
-
end
|
624
|
-
end
|
625
|
-
end
|
626
|
-
end
|