esod-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/EXAMPLES +140 -0
- data/GEM_RELEASE +19 -0
- data/README +10 -0
- data/Rakefile +14 -0
- data/VERSION +1 -0
- data/esod-client.gemspec +118 -0
- data/esod-client.rb +35 -0
- data/lib/activesupport-2.2.2/CHANGELOG +1257 -0
- data/lib/activesupport-2.2.2/README +43 -0
- data/lib/activesupport-2.2.2/README.CFT +2 -0
- data/lib/activesupport-2.2.2/lib/active_support.rb +26 -0
- data/lib/activesupport-2.2.2/lib/active_support/base64.rb +33 -0
- data/lib/activesupport-2.2.2/lib/active_support/basic_object.rb +24 -0
- data/lib/activesupport-2.2.2/lib/active_support/buffered_logger.rb +122 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache.rb +223 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/compressed_mem_cache_store.rb +20 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/drb_store.rb +15 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/file_store.rb +72 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/mem_cache_store.rb +127 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/memory_store.rb +52 -0
- data/lib/activesupport-2.2.2/lib/active_support/cache/synchronized_memory_store.rb +47 -0
- data/lib/activesupport-2.2.2/lib/active_support/callbacks.rb +280 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext.rb +6 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash.rb +14 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/conversions.rb +259 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/deep_merge.rb +23 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/diff.rb +19 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/except.rb +25 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/indifferent_access.rb +137 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/keys.rb +52 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/reverse_merge.rb +35 -0
- data/lib/activesupport-2.2.2/lib/active_support/core_ext/hash/slice.rb +33 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor.rb +14 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/blankslate.rb +113 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder.rb +13 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/blankslate.rb +20 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/css.rb +250 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/xchar.rb +115 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/xmlbase.rb +139 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/xmlevents.rb +63 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/builder/xmlmarkup.rb +328 -0
- data/lib/activesupport-2.2.2/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb +1021 -0
- data/lib/activesupport-2.2.2/lib/activesupport.rb +1 -0
- data/lib/esodclient/esodclient.rb +22 -0
- data/lib/hash.rb +22 -0
- data/lib/mime-types-1.16/History.txt +107 -0
- data/lib/mime-types-1.16/Install.txt +17 -0
- data/lib/mime-types-1.16/Licence.txt +15 -0
- data/lib/mime-types-1.16/Manifest.txt +12 -0
- data/lib/mime-types-1.16/README.txt +28 -0
- data/lib/mime-types-1.16/Rakefile +316 -0
- data/lib/mime-types-1.16/lib/mime/types.rb +751 -0
- data/lib/mime-types-1.16/lib/mime/types.rb.data +1324 -0
- data/lib/mime-types-1.16/mime-types.gemspec +43 -0
- data/lib/mime-types-1.16/setup.rb +1585 -0
- data/lib/mime-types-1.16/test/test_mime_type.rb +356 -0
- data/lib/mime-types-1.16/test/test_mime_types.rb +122 -0
- data/lib/rest-client-1.2.0/README.rdoc +102 -0
- data/lib/rest-client-1.2.0/Rakefile +57 -0
- data/lib/rest-client-1.2.0/VERSION +1 -0
- data/lib/rest-client-1.2.0/bin/restclient +87 -0
- data/lib/rest-client-1.2.0/lib/rest_client.rb +2 -0
- data/lib/rest-client-1.2.0/lib/restclient.rb +108 -0
- data/lib/rest-client-1.2.0/lib/restclient/exceptions.rb +89 -0
- data/lib/rest-client-1.2.0/lib/restclient/mixin/response.rb +48 -0
- data/lib/rest-client-1.2.0/lib/restclient/net_http_ext.rb +21 -0
- data/lib/rest-client-1.2.0/lib/restclient/payload.rb +178 -0
- data/lib/rest-client-1.2.0/lib/restclient/raw_response.rb +30 -0
- data/lib/rest-client-1.2.0/lib/restclient/request.rb +287 -0
- data/lib/rest-client-1.2.0/lib/restclient/resource.rb +146 -0
- data/lib/rest-client-1.2.0/lib/restclient/response.rb +20 -0
- data/lib/rest-client-1.2.0/spec/base.rb +10 -0
- data/lib/rest-client-1.2.0/spec/exceptions_spec.rb +65 -0
- data/lib/rest-client-1.2.0/spec/master_shake.jpg +0 -0
- data/lib/rest-client-1.2.0/spec/mixin/response_spec.rb +46 -0
- data/lib/rest-client-1.2.0/spec/payload_spec.rb +131 -0
- data/lib/rest-client-1.2.0/spec/raw_response_spec.rb +17 -0
- data/lib/rest-client-1.2.0/spec/request_spec.rb +521 -0
- data/lib/rest-client-1.2.0/spec/resource_spec.rb +75 -0
- data/lib/rest-client-1.2.0/spec/response_spec.rb +21 -0
- data/lib/rest-client-1.2.0/spec/restclient_spec.rb +53 -0
- data/lib/trollop/trollop.rb +735 -0
- metadata +137 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
= Active Support -- Utility classes and standard library extensions from Rails
|
2
|
+
|
3
|
+
Active Support is a collection of various utility classes and standard library extensions that were found useful
|
4
|
+
for Rails. All these additions have hence been collected in this bundle as way to gather all that sugar that makes
|
5
|
+
Ruby sweeter.
|
6
|
+
|
7
|
+
|
8
|
+
== Download
|
9
|
+
|
10
|
+
The latest version of Active Support can be found at
|
11
|
+
|
12
|
+
* http://rubyforge.org/project/showfiles.php?group_id=182
|
13
|
+
|
14
|
+
Documentation can be found at
|
15
|
+
|
16
|
+
* http://as.rubyonrails.com
|
17
|
+
|
18
|
+
|
19
|
+
== Installation
|
20
|
+
|
21
|
+
The preferred method of installing Active Support is through its GEM file. You'll need to have
|
22
|
+
RubyGems[http://rubygems.rubyforge.org/wiki/wiki.pl] installed for that, though. If you have it,
|
23
|
+
then use:
|
24
|
+
|
25
|
+
% [sudo] gem install activesupport-1.0.0.gem
|
26
|
+
|
27
|
+
|
28
|
+
== License
|
29
|
+
|
30
|
+
Active Support is released under the MIT license.
|
31
|
+
|
32
|
+
|
33
|
+
== Support
|
34
|
+
|
35
|
+
The Active Support homepage is http://www.rubyonrails.com. You can find the Active Support
|
36
|
+
RubyForge page at http://rubyforge.org/projects/activesupport. And as Jim from Rake says:
|
37
|
+
|
38
|
+
Feel free to submit commits or feature requests. If you send a patch,
|
39
|
+
remember to update the corresponding unit tests. If fact, I prefer
|
40
|
+
new feature to be submitted in the form of new unit tests.
|
41
|
+
|
42
|
+
For other information, feel free to ask on the ruby-talk mailing list
|
43
|
+
(which is mirrored to comp.lang.ruby) or contact mailto:david@loudthinking.com.
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2005 David Heinemeier Hansson
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
require 'active_support/vendor'
|
25
|
+
require 'active_support/basic_object'
|
26
|
+
require 'active_support/core_ext'
|
@@ -0,0 +1,33 @@
|
|
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
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# A base class with no predefined methods that tries to behave like Builder's
|
2
|
+
# BlankSlate in Ruby 1.9. In Ruby pre-1.9, this is actually the
|
3
|
+
# Builder::BlankSlate class.
|
4
|
+
#
|
5
|
+
# Ruby 1.9 introduces BasicObject which differs slightly from Builder's
|
6
|
+
# BlankSlate that has been used so far. ActiveSupport::BasicObject provides a
|
7
|
+
# barebones base class that emulates Builder::BlankSlate while still relying on
|
8
|
+
# Ruby 1.9's BasicObject in Ruby 1.9.
|
9
|
+
module ActiveSupport
|
10
|
+
if defined? ::BasicObject
|
11
|
+
class BasicObject < ::BasicObject
|
12
|
+
undef_method :==
|
13
|
+
undef_method :equal?
|
14
|
+
|
15
|
+
# Let ActiveSupport::BasicObject at least raise exceptions.
|
16
|
+
def raise(*args)
|
17
|
+
::Object.send(:raise, *args)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
else
|
21
|
+
require 'blankslate'
|
22
|
+
BasicObject = BlankSlate
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
# Inspired by the buffered logger idea by Ezra
|
3
|
+
class BufferedLogger
|
4
|
+
module Severity
|
5
|
+
DEBUG = 0
|
6
|
+
INFO = 1
|
7
|
+
WARN = 2
|
8
|
+
ERROR = 3
|
9
|
+
FATAL = 4
|
10
|
+
UNKNOWN = 5
|
11
|
+
end
|
12
|
+
include Severity
|
13
|
+
|
14
|
+
MAX_BUFFER_SIZE = 1000
|
15
|
+
|
16
|
+
# Set to false to disable the silencer
|
17
|
+
cattr_accessor :silencer
|
18
|
+
self.silencer = true
|
19
|
+
|
20
|
+
# Silences the logger for the duration of the block.
|
21
|
+
def silence(temporary_level = ERROR)
|
22
|
+
if silencer
|
23
|
+
begin
|
24
|
+
old_logger_level, self.level = level, temporary_level
|
25
|
+
yield self
|
26
|
+
ensure
|
27
|
+
self.level = old_logger_level
|
28
|
+
end
|
29
|
+
else
|
30
|
+
yield self
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
attr_accessor :level
|
35
|
+
attr_reader :auto_flushing
|
36
|
+
|
37
|
+
def initialize(log, level = DEBUG)
|
38
|
+
@level = level
|
39
|
+
@buffer = {}
|
40
|
+
@auto_flushing = 1
|
41
|
+
@guard = Mutex.new
|
42
|
+
|
43
|
+
if log.respond_to?(:write)
|
44
|
+
@log = log
|
45
|
+
elsif File.exist?(log)
|
46
|
+
@log = open(log, (File::WRONLY | File::APPEND))
|
47
|
+
@log.sync = true
|
48
|
+
else
|
49
|
+
FileUtils.mkdir_p(File.dirname(log))
|
50
|
+
@log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
|
51
|
+
@log.sync = true
|
52
|
+
@log.write("# Logfile created on %s" % [Time.now.to_s])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def add(severity, message = nil, progname = nil, &block)
|
57
|
+
return if @level > severity
|
58
|
+
message = (message || (block && block.call) || progname).to_s
|
59
|
+
# If a newline is necessary then create a new message ending with a newline.
|
60
|
+
# Ensures that the original message is not mutated.
|
61
|
+
message = "#{message}\n" unless message[-1] == ?\n
|
62
|
+
buffer << message
|
63
|
+
auto_flush
|
64
|
+
message
|
65
|
+
end
|
66
|
+
|
67
|
+
for severity in Severity.constants
|
68
|
+
class_eval <<-EOT, __FILE__, __LINE__
|
69
|
+
def #{severity.downcase}(message = nil, progname = nil, &block)
|
70
|
+
add(#{severity}, message, progname, &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
def #{severity.downcase}?
|
74
|
+
#{severity} >= @level
|
75
|
+
end
|
76
|
+
EOT
|
77
|
+
end
|
78
|
+
|
79
|
+
# Set the auto-flush period. Set to true to flush after every log message,
|
80
|
+
# to an integer to flush every N messages, or to false, nil, or zero to
|
81
|
+
# never auto-flush. If you turn auto-flushing off, be sure to regularly
|
82
|
+
# flush the log yourself -- it will eat up memory until you do.
|
83
|
+
def auto_flushing=(period)
|
84
|
+
@auto_flushing =
|
85
|
+
case period
|
86
|
+
when true; 1
|
87
|
+
when false, nil, 0; MAX_BUFFER_SIZE
|
88
|
+
when Integer; period
|
89
|
+
else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def flush
|
94
|
+
@guard.synchronize do
|
95
|
+
unless buffer.empty?
|
96
|
+
old_buffer = buffer
|
97
|
+
clear_buffer
|
98
|
+
@log.write(old_buffer.join)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def close
|
104
|
+
flush
|
105
|
+
@log.close if @log.respond_to?(:close)
|
106
|
+
@log = nil
|
107
|
+
end
|
108
|
+
|
109
|
+
protected
|
110
|
+
def auto_flush
|
111
|
+
flush if buffer.size >= @auto_flushing
|
112
|
+
end
|
113
|
+
|
114
|
+
def buffer
|
115
|
+
@buffer[Thread.current] ||= []
|
116
|
+
end
|
117
|
+
|
118
|
+
def clear_buffer
|
119
|
+
@buffer.delete(Thread.current)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
# See ActiveSupport::Cache::Store for documentation.
|
5
|
+
module Cache
|
6
|
+
# Creates a new CacheStore object according to the given options.
|
7
|
+
#
|
8
|
+
# If no arguments are passed to this method, then a new
|
9
|
+
# ActiveSupport::Cache::MemoryStore object will be returned.
|
10
|
+
#
|
11
|
+
# If you pass a Symbol as the first argument, then a corresponding cache
|
12
|
+
# store class under the ActiveSupport::Cache namespace will be created.
|
13
|
+
# For example:
|
14
|
+
#
|
15
|
+
# ActiveSupport::Cache.lookup_store(:memory_store)
|
16
|
+
# # => returns a new ActiveSupport::Cache::MemoryStore object
|
17
|
+
#
|
18
|
+
# ActiveSupport::Cache.lookup_store(:drb_store)
|
19
|
+
# # => returns a new ActiveSupport::Cache::DRbStore object
|
20
|
+
#
|
21
|
+
# Any additional arguments will be passed to the corresponding cache store
|
22
|
+
# class's constructor:
|
23
|
+
#
|
24
|
+
# ActiveSupport::Cache.lookup_store(:file_store, "/tmp/cache")
|
25
|
+
# # => same as: ActiveSupport::Cache::FileStore.new("/tmp/cache")
|
26
|
+
#
|
27
|
+
# If the first argument is not a Symbol, then it will simply be returned:
|
28
|
+
#
|
29
|
+
# ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new)
|
30
|
+
# # => returns MyOwnCacheStore.new
|
31
|
+
def self.lookup_store(*store_option)
|
32
|
+
store, *parameters = *([ store_option ].flatten)
|
33
|
+
|
34
|
+
case store
|
35
|
+
when Symbol
|
36
|
+
store_class_name = (store == :drb_store ? "DRbStore" : store.to_s.camelize)
|
37
|
+
store_class = ActiveSupport::Cache.const_get(store_class_name)
|
38
|
+
store_class.new(*parameters)
|
39
|
+
when nil
|
40
|
+
ActiveSupport::Cache::MemoryStore.new
|
41
|
+
else
|
42
|
+
store
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.expand_cache_key(key, namespace = nil)
|
47
|
+
expanded_cache_key = namespace ? "#{namespace}/" : ""
|
48
|
+
|
49
|
+
if ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
|
50
|
+
expanded_cache_key << "#{ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]}/"
|
51
|
+
end
|
52
|
+
|
53
|
+
expanded_cache_key << case
|
54
|
+
when key.respond_to?(:cache_key)
|
55
|
+
key.cache_key
|
56
|
+
when key.is_a?(Array)
|
57
|
+
key.collect { |element| expand_cache_key(element) }.to_param
|
58
|
+
when key
|
59
|
+
key.to_param
|
60
|
+
end.to_s
|
61
|
+
|
62
|
+
expanded_cache_key
|
63
|
+
end
|
64
|
+
|
65
|
+
# An abstract cache store class. There are multiple cache store
|
66
|
+
# implementations, each having its own additional features. See the classes
|
67
|
+
# under the ActiveSupport::Cache module, e.g.
|
68
|
+
# ActiveSupport::Cache::MemCacheStore. MemCacheStore is currently the most
|
69
|
+
# popular cache store for large production websites.
|
70
|
+
#
|
71
|
+
# ActiveSupport::Cache::Store is meant for caching strings. Some cache
|
72
|
+
# store implementations, like MemoryStore, are able to cache arbitrary
|
73
|
+
# Ruby objects, but don't count on every cache store to be able to do that.
|
74
|
+
#
|
75
|
+
# cache = ActiveSupport::Cache::MemoryStore.new
|
76
|
+
#
|
77
|
+
# cache.read("city") # => nil
|
78
|
+
# cache.write("city", "Duckburgh")
|
79
|
+
# cache.read("city") # => "Duckburgh"
|
80
|
+
class Store
|
81
|
+
cattr_accessor :logger
|
82
|
+
|
83
|
+
def silence!
|
84
|
+
@silence = true
|
85
|
+
self
|
86
|
+
end
|
87
|
+
|
88
|
+
# Fetches data from the cache, using the given key. If there is data in
|
89
|
+
# the cache with the given key, then that data is returned.
|
90
|
+
#
|
91
|
+
# If there is no such data in the cache (a cache miss occurred), then
|
92
|
+
# then nil will be returned. However, if a block has been passed, then
|
93
|
+
# that block will be run in the event of a cache miss. The return value
|
94
|
+
# of the block will be written to the cache under the given cache key,
|
95
|
+
# and that return value will be returned.
|
96
|
+
#
|
97
|
+
# cache.write("today", "Monday")
|
98
|
+
# cache.fetch("today") # => "Monday"
|
99
|
+
#
|
100
|
+
# cache.fetch("city") # => nil
|
101
|
+
# cache.fetch("city") do
|
102
|
+
# "Duckburgh"
|
103
|
+
# end
|
104
|
+
# cache.fetch("city") # => "Duckburgh"
|
105
|
+
#
|
106
|
+
# You may also specify additional options via the +options+ argument.
|
107
|
+
# Setting <tt>:force => true</tt> will force a cache miss:
|
108
|
+
#
|
109
|
+
# cache.write("today", "Monday")
|
110
|
+
# cache.fetch("today", :force => true) # => nil
|
111
|
+
#
|
112
|
+
# Other options will be handled by the specific cache store implementation.
|
113
|
+
# Internally, #fetch calls #read, and calls #write on a cache miss.
|
114
|
+
# +options+ will be passed to the #read and #write calls.
|
115
|
+
#
|
116
|
+
# For example, MemCacheStore's #write method supports the +:expires_in+
|
117
|
+
# option, which tells the memcached server to automatically expire the
|
118
|
+
# cache item after a certain period. We can use this option with #fetch
|
119
|
+
# too:
|
120
|
+
#
|
121
|
+
# cache = ActiveSupport::Cache::MemCacheStore.new
|
122
|
+
# cache.fetch("foo", :force => true, :expires_in => 5.seconds) do
|
123
|
+
# "bar"
|
124
|
+
# end
|
125
|
+
# cache.fetch("foo") # => "bar"
|
126
|
+
# sleep(6)
|
127
|
+
# cache.fetch("foo") # => nil
|
128
|
+
def fetch(key, options = {})
|
129
|
+
@logger_off = true
|
130
|
+
if !options[:force] && value = read(key, options)
|
131
|
+
@logger_off = false
|
132
|
+
log("hit", key, options)
|
133
|
+
value
|
134
|
+
elsif block_given?
|
135
|
+
@logger_off = false
|
136
|
+
log("miss", key, options)
|
137
|
+
|
138
|
+
value = nil
|
139
|
+
seconds = Benchmark.realtime { value = yield }
|
140
|
+
|
141
|
+
@logger_off = true
|
142
|
+
write(key, value, options)
|
143
|
+
@logger_off = false
|
144
|
+
|
145
|
+
log("write (will save #{'%.2f' % (seconds * 1000)}ms)", key, nil)
|
146
|
+
|
147
|
+
value
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# Fetches data from the cache, using the given key. If there is data in
|
152
|
+
# the cache with the given key, then that data is returned. Otherwise,
|
153
|
+
# nil is returned.
|
154
|
+
#
|
155
|
+
# You may also specify additional options via the +options+ argument.
|
156
|
+
# The specific cache store implementation will decide what to do with
|
157
|
+
# +options+.
|
158
|
+
def read(key, options = nil)
|
159
|
+
log("read", key, options)
|
160
|
+
end
|
161
|
+
|
162
|
+
# Writes the given value to the cache, with the given key.
|
163
|
+
#
|
164
|
+
# You may also specify additional options via the +options+ argument.
|
165
|
+
# The specific cache store implementation will decide what to do with
|
166
|
+
# +options+.
|
167
|
+
#
|
168
|
+
# For example, MemCacheStore supports the +:expires_in+ option, which
|
169
|
+
# tells the memcached server to automatically expire the cache item after
|
170
|
+
# a certain period:
|
171
|
+
#
|
172
|
+
# cache = ActiveSupport::Cache::MemCacheStore.new
|
173
|
+
# cache.write("foo", "bar", :expires_in => 5.seconds)
|
174
|
+
# cache.read("foo") # => "bar"
|
175
|
+
# sleep(6)
|
176
|
+
# cache.read("foo") # => nil
|
177
|
+
def write(key, value, options = nil)
|
178
|
+
log("write", key, options)
|
179
|
+
end
|
180
|
+
|
181
|
+
def delete(key, options = nil)
|
182
|
+
log("delete", key, options)
|
183
|
+
end
|
184
|
+
|
185
|
+
def delete_matched(matcher, options = nil)
|
186
|
+
log("delete matched", matcher.inspect, options)
|
187
|
+
end
|
188
|
+
|
189
|
+
def exist?(key, options = nil)
|
190
|
+
log("exist?", key, options)
|
191
|
+
end
|
192
|
+
|
193
|
+
def increment(key, amount = 1)
|
194
|
+
log("incrementing", key, amount)
|
195
|
+
if num = read(key)
|
196
|
+
write(key, num + amount)
|
197
|
+
else
|
198
|
+
nil
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def decrement(key, amount = 1)
|
203
|
+
log("decrementing", key, amount)
|
204
|
+
if num = read(key)
|
205
|
+
write(key, num - amount)
|
206
|
+
else
|
207
|
+
nil
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
private
|
212
|
+
def log(operation, key, options)
|
213
|
+
logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@silence && !@logger_off
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
require 'active_support/cache/file_store'
|
220
|
+
require 'active_support/cache/memory_store'
|
221
|
+
require 'active_support/cache/drb_store'
|
222
|
+
require 'active_support/cache/mem_cache_store'
|
223
|
+
require 'active_support/cache/compressed_mem_cache_store'
|