diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-01-19 10:48:44 -0800 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-01-19 10:48:44 -0800 |
commit | 782ed888dcc5154c1222028d5e9d49f7ae115728 (patch) | |
tree | abbe151a2628072a9c8e331dedf697de226c90a3 /activesupport | |
parent | 3d76e7fbd2db962776d904c96e302bee4ac6af4b (diff) | |
parent | 7f6017c4a8f167fc3221de33f995727d1ee6147a (diff) | |
download | rails-782ed888dcc5154c1222028d5e9d49f7ae115728.tar.gz rails-782ed888dcc5154c1222028d5e9d49f7ae115728.tar.bz2 rails-782ed888dcc5154c1222028d5e9d49f7ae115728.zip |
Merge pull request #8081 from JoeyButler/refactor_cache_store
Refactor cache store
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/cache.rb | 66 |
1 files changed, 41 insertions, 25 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 5a5548d567..6a1f1fefb8 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -275,34 +275,14 @@ module ActiveSupport if block_given? options = merged_options(options) key = namespaced_key(name, options) - unless options[:force] - entry = instrument(:read, name, options) do |payload| - payload[:super_operation] = :fetch if payload - read_entry(key, options) - end - end - if entry && entry.expired? - race_ttl = options[:race_condition_ttl].to_i - if race_ttl && (Time.now - entry.expires_at <= race_ttl) - # When an entry has :race_condition_ttl defined, put the stale entry back into the cache - # for a brief period while the entry is begin recalculated. - entry.expires_at = Time.now + race_ttl - write_entry(key, entry, :expires_in => race_ttl * 2) - else - delete_entry(key, options) - end - entry = nil - end + + cached_entry = find_cached_entry(key, name, options) unless options[:force] + entry = handle_expired_entry(cached_entry, key, options) if entry - instrument(:fetch_hit, name, options) { |payload| } - entry.value + get_entry_value(entry, name, options) else - result = instrument(:generate, name, options) do |payload| - yield(name) - end - write(name, result, options) - result + save_block_result_to_cache(name, options) { |name| yield name } end else read(name, options) @@ -531,6 +511,42 @@ module ActiveSupport return unless logger && logger.debug? && !silence? logger.debug("Cache #{operation}: #{key}#{options.blank? ? "" : " (#{options.inspect})"}") end + + def find_cached_entry(key, name, options) + instrument(:read, name, options) do |payload| + payload[:super_operation] = :fetch if payload + read_entry(key, options) + end + end + + def handle_expired_entry(entry, key, options) + if entry && entry.expired? + race_ttl = options[:race_condition_ttl].to_i + if race_ttl && (Time.now - entry.expires_at <= race_ttl) + # When an entry has :race_condition_ttl defined, put the stale entry back into the cache + # for a brief period while the entry is begin recalculated. + entry.expires_at = Time.now + race_ttl + write_entry(key, entry, :expires_in => race_ttl * 2) + else + delete_entry(key, options) + end + entry = nil + end + entry + end + + def get_entry_value(entry, name, options) + instrument(:fetch_hit, name, options) { |payload| } + entry.value + end + + def save_block_result_to_cache(name, options) + result = instrument(:generate, name, options) do |payload| + yield(name) + end + write(name, result, options) + result + end end # This class is used to represent cache entries. Cache entries have a value and an optional |