From ec8cc2f4487c3cba5590383dbf02e25b0974f991 Mon Sep 17 00:00:00 2001 From: dolzenko Date: Tue, 2 Mar 2010 01:08:29 +0300 Subject: [PATCH] Make FileStore#exist? consistent with FileStore#read when :expires_in option is supplied --- .../lib/active_support/cache/file_store.rb | 8 ++++++-- activesupport/test/caching_test.rb | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 7521efe..ed7ecab 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -20,7 +20,7 @@ module ActiveSupport file_name = real_file_path(name) expires = expires_in(options) - if File.exist?(file_name) && (expires <= 0 || Time.now - File.mtime(file_name) < expires) + if exist_without_instrument?(file_name, expires) File.open(file_name, 'rb') { |f| Marshal.load(f) } end end @@ -61,7 +61,7 @@ module ActiveSupport def exist?(name, options = nil) super do - File.exist?(real_file_path(name)) + exist_without_instrument?(real_file_path(name), expires_in(options)) end end @@ -85,6 +85,10 @@ module ActiveSupport end end end + + def exist_without_instrument?(file_name, expires) + File.exist?(file_name) && (expires <= 0 || Time.now - File.mtime(file_name) < expires) + end end end end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 00e05f7..964dad2 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -157,13 +157,18 @@ class FileStoreTest < ActiveSupport::TestCase @cache.write('foo', 'bar') cache_read = lambda { @cache.read('foo', :expires_in => 60) } + cache_exists = lambda { @cache.exist?('foo', :expires_in => 60) } + assert_equal 'bar', cache_read.call + assert_equal true, cache_exists.call Time.stubs(:now).returns(time + 30) assert_equal 'bar', cache_read.call + assert_equal true, cache_exists.call Time.stubs(:now).returns(time + 120) assert_nil cache_read.call + assert_equal false, cache_exists.call end end -- 1.6.0.4