Skip to content
This repository
Browse code

Add expiry support File cache store [#1693 state:resolved] [Roman Sht…

…erenzon, Pratik Naik]
  • Loading branch information...
commit b5775c2b3efb3ae5ef9074d26f6fc3e302a4f6f0 1 parent 9f7eaea
Pratik authored
12  activesupport/lib/active_support/cache.rb
@@ -129,8 +129,8 @@ def silence!
129 129
       #
130 130
       # For example, MemCacheStore's #write method supports the +:expires_in+
131 131
       # option, which tells the memcached server to automatically expire the
132  
-      # cache item after a certain period. We can use this option with #fetch
133  
-      # too:
  132
+      # cache item after a certain period. This options is also supported by
  133
+      # FileStore's #read method. We can use this option with #fetch too:
134 134
       #
135 135
       #   cache = ActiveSupport::Cache::MemCacheStore.new
136 136
       #   cache.fetch("foo", :force => true, :expires_in => 5.seconds) do
@@ -169,6 +169,10 @@ def fetch(key, options = {})
169 169
       # You may also specify additional options via the +options+ argument.
170 170
       # The specific cache store implementation will decide what to do with
171 171
       # +options+.
  172
+      #
  173
+      # For example, FileStore supports the +:expires_in+ option, which
  174
+      # makes the method return nil for cache items older than the specified
  175
+      # period.
172 176
       def read(key, options = nil)
173 177
         log("read", key, options)
174 178
       end
@@ -223,6 +227,10 @@ def decrement(key, amount = 1)
223 227
       end
224 228
 
225 229
       private
  230
+        def expires_in(options)
  231
+          (options && options[:expires_in]) || 0
  232
+        end
  233
+
226 234
         def log(operation, key, options)
227 235
           logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@silence && !@logger_off
228 236
         end
14  activesupport/lib/active_support/cache/file_store.rb
@@ -10,11 +10,23 @@ def initialize(cache_path)
10 10
         @cache_path = cache_path
11 11
       end
12 12
 
  13
+      # Reads a value from the cache.
  14
+      #
  15
+      # Possible options:
  16
+      # - +:expires_in+ - the number of seconds that this value may stay in
  17
+      #   the cache.
13 18
       def read(name, options = nil)
14 19
         super
15  
-        File.open(real_file_path(name), 'rb') { |f| Marshal.load(f) } rescue nil
  20
+
  21
+        file_name = real_file_path(name)
  22
+        expires = expires_in(options)
  23
+
  24
+        if File.exist?(file_name) && (expires <= 0 || Time.now - File.mtime(file_name) < expires)
  25
+          File.open(file_name, 'rb') { |f| Marshal.load(f) }
  26
+        end
16 27
       end
17 28
 
  29
+      # Writes a value to the cache.
18 30
       def write(name, value, options = nil)
19 31
         super
20 32
         ensure_cache_path(File.dirname(real_file_path(name)))
4  activesupport/lib/active_support/cache/mem_cache_store.rb
@@ -130,10 +130,6 @@ def stats
130 130
       end
131 131
 
132 132
       private
133  
-        def expires_in(options)
134  
-          (options && options[:expires_in]) || 0
135  
-        end
136  
-
137 133
         def raw?(options)
138 134
           options && options[:raw]
139 135
         end
10  activesupport/test/caching_test.rb
@@ -146,6 +146,16 @@ def teardown
146 146
   end
147 147
 
148 148
   include CacheStoreBehavior
  149
+
  150
+  def test_expires_in
  151
+    @cache.write('foo', 'bar')
  152
+    cache_read = lambda { @cache.read('foo', :expires_in => 2) }
  153
+    assert_equal 'bar', cache_read.call
  154
+    sleep(1)
  155
+    assert_equal 'bar', cache_read.call
  156
+    sleep(1)
  157
+    assert_nil cache_read.call
  158
+  end
149 159
 end
150 160
 
151 161
 class MemoryStoreTest < ActiveSupport::TestCase

0 notes on commit b5775c2

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