Skip to content

Commit b5775c2

Browse files
committed
Add expiry support File cache store [#1693 state:resolved] [Roman Shterenzon, Pratik Naik]
1 parent 9f7eaea commit b5775c2

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ def silence!
129129
#
130130
# For example, MemCacheStore's #write method supports the +:expires_in+
131131
# 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:
134134
#
135135
# cache = ActiveSupport::Cache::MemCacheStore.new
136136
# cache.fetch("foo", :force => true, :expires_in => 5.seconds) do
@@ -169,6 +169,10 @@ def fetch(key, options = {})
169169
# You may also specify additional options via the +options+ argument.
170170
# The specific cache store implementation will decide what to do with
171171
# +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.
172176
def read(key, options = nil)
173177
log("read", key, options)
174178
end
@@ -223,6 +227,10 @@ def decrement(key, amount = 1)
223227
end
224228

225229
private
230+
def expires_in(options)
231+
(options && options[:expires_in]) || 0
232+
end
233+
226234
def log(operation, key, options)
227235
logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@silence && !@logger_off
228236
end

activesupport/lib/active_support/cache/file_store.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,23 @@ def initialize(cache_path)
1010
@cache_path = cache_path
1111
end
1212

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.
1318
def read(name, options = nil)
1419
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
1627
end
1728

29+
# Writes a value to the cache.
1830
def write(name, value, options = nil)
1931
super
2032
ensure_cache_path(File.dirname(real_file_path(name)))

activesupport/lib/active_support/cache/mem_cache_store.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ def stats
130130
end
131131

132132
private
133-
def expires_in(options)
134-
(options && options[:expires_in]) || 0
135-
end
136-
137133
def raw?(options)
138134
options && options[:raw]
139135
end

activesupport/test/caching_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ def teardown
146146
end
147147

148148
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
149159
end
150160

151161
class MemoryStoreTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)