From c17bd7476abb0bade3c27e652b1d56548d0ee4ae Mon Sep 17 00:00:00 2001 From: grosser Date: Tue, 6 Aug 2013 19:43:28 -0700 Subject: support :unless_exist for FileCache --- activesupport/CHANGELOG.md | 4 ++++ activesupport/lib/active_support/cache/file_store.rb | 1 + activesupport/test/caching_test.rb | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index f15297f279..f5d5389bf1 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Support :unless_exist in FileStore + + *Michael Grosser* + * Ensure that autoloaded constants in all-caps nestings are marked as autoloaded. diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 472f23c1c5..d0de6e861a 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -96,6 +96,7 @@ module ActiveSupport def write_entry(key, entry, options) file_name = key_file_path(key) + return false if options[:unless_exist] && File.exist?(file_name) ensure_cache_path(File.dirname(file_name)) File.atomic_write(file_name, cache_path) {|f| Marshal.dump(entry, f)} true diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index df570d485a..7f4b24c934 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -709,6 +709,13 @@ class FileStoreTest < ActiveSupport::TestCase @cache.send(:read_entry, "winston", {}) assert @buffer.string.present? end + + def test_write_with_unless_exist + assert_equal true, @cache.write(1, "aaaaaaaaaa") + assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true) + @cache.write(1, nil) + assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true) + end end class MemoryStoreTest < ActiveSupport::TestCase -- cgit v1.2.3