Skip to content
This repository
Browse code

Merge pull request #8081 from JoeyButler/refactor_cache_store

Refactor cache store
  • Loading branch information...
commit 782ed888dcc5154c1222028d5e9d49f7ae115728 2 parents 3d76e7f + 7f6017c
Rafael Mendonça França authored January 19, 2013

Showing 1 changed file with 41 additions and 25 deletions. Show diff stats Hide diff stats

  1. 66  activesupport/lib/active_support/cache.rb
66  activesupport/lib/active_support/cache.rb
@@ -275,34 +275,14 @@ def fetch(name, options = nil)
275 275
         if block_given?
276 276
           options = merged_options(options)
277 277
           key = namespaced_key(name, options)
278  
-          unless options[:force]
279  
-            entry = instrument(:read, name, options) do |payload|
280  
-              payload[:super_operation] = :fetch if payload
281  
-              read_entry(key, options)
282  
-            end
283  
-          end
284  
-          if entry && entry.expired?
285  
-            race_ttl = options[:race_condition_ttl].to_i
286  
-            if race_ttl && (Time.now - entry.expires_at <= race_ttl)
287  
-              # When an entry has :race_condition_ttl defined, put the stale entry back into the cache
288  
-              # for a brief period while the entry is begin recalculated.
289  
-              entry.expires_at = Time.now + race_ttl
290  
-              write_entry(key, entry, :expires_in => race_ttl * 2)
291  
-            else
292  
-              delete_entry(key, options)
293  
-            end
294  
-            entry = nil
295  
-          end
  278
+
  279
+          cached_entry = find_cached_entry(key, name, options) unless options[:force]
  280
+          entry = handle_expired_entry(cached_entry, key, options)
296 281
 
297 282
           if entry
298  
-            instrument(:fetch_hit, name, options) { |payload| }
299  
-            entry.value
  283
+            get_entry_value(entry, name, options)
300 284
           else
301  
-            result = instrument(:generate, name, options) do |payload|
302  
-              yield(name)
303  
-            end
304  
-            write(name, result, options)
305  
-            result
  285
+            save_block_result_to_cache(name, options) { |name| yield name }
306 286
           end
307 287
         else
308 288
           read(name, options)
@@ -531,6 +511,42 @@ def log(operation, key, options = nil)
531 511
           return unless logger && logger.debug? && !silence?
532 512
           logger.debug("Cache #{operation}: #{key}#{options.blank? ? "" : " (#{options.inspect})"}")
533 513
         end
  514
+
  515
+        def find_cached_entry(key, name, options)
  516
+          instrument(:read, name, options) do |payload|
  517
+            payload[:super_operation] = :fetch if payload
  518
+            read_entry(key, options)
  519
+          end
  520
+        end
  521
+
  522
+        def handle_expired_entry(entry, key, options)
  523
+          if entry && entry.expired?
  524
+            race_ttl = options[:race_condition_ttl].to_i
  525
+            if race_ttl && (Time.now - entry.expires_at <= race_ttl)
  526
+              # When an entry has :race_condition_ttl defined, put the stale entry back into the cache
  527
+              # for a brief period while the entry is begin recalculated.
  528
+              entry.expires_at = Time.now + race_ttl
  529
+              write_entry(key, entry, :expires_in => race_ttl * 2)
  530
+            else
  531
+              delete_entry(key, options)
  532
+            end
  533
+            entry = nil
  534
+          end
  535
+          entry
  536
+        end
  537
+
  538
+        def get_entry_value(entry, name, options)
  539
+          instrument(:fetch_hit, name, options) { |payload| }
  540
+          entry.value
  541
+        end
  542
+
  543
+        def save_block_result_to_cache(name, options)
  544
+          result = instrument(:generate, name, options) do |payload|
  545
+            yield(name)
  546
+          end
  547
+          write(name, result, options)
  548
+          result
  549
+        end
534 550
     end
535 551
 
536 552
     # This class is used to represent cache entries. Cache entries have a value and an optional

0 notes on commit 782ed88

Please sign in to comment.
Something went wrong with that request. Please try again.