Skip to content

Commit f18cf8e

Browse files
committed
Merge pull request rails#11785 from grosser/grosser/file-unless-exist
support :unless_exist for FileCache Conflicts: activesupport/CHANGELOG.md activesupport/test/caching_test.rb
2 parents f78c5fb + c17bd74 commit f18cf8e

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Support :unless_exist in FileStore
2+
3+
*Michael Grosser*
4+
15
* Fix `slice!` deleting the default value of the hash.
26

37
*Antonio Santos*

activesupport/lib/active_support/cache/file_store.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def read_entry(key, options)
9797

9898
def write_entry(key, entry, options)
9999
file_name = key_file_path(key)
100+
return false if options[:unless_exist] && File.exist?(file_name)
100101
ensure_cache_path(File.dirname(file_name))
101102
File.atomic_write(file_name, cache_path) {|f| Marshal.dump(entry, f)}
102103
true

activesupport/test/caching_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,13 @@ def test_cleanup_removes_all_expired_entries
721721
assert @cache.exist?('baz')
722722
assert @cache.exist?('quux')
723723
end
724+
725+
def test_write_with_unless_exist
726+
assert_equal true, @cache.write(1, "aaaaaaaaaa")
727+
assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true)
728+
@cache.write(1, nil)
729+
assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true)
730+
end
724731
end
725732

726733
class MemoryStoreTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)