Skip to content

Commit

Permalink
add unless_exist option to memory store
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed May 10, 2012
1 parent dd9ea8d commit b5005b6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions activesupport/lib/active_support/cache/memory_store.rb
Expand Up @@ -137,6 +137,7 @@ def read_entry(key, options) # :nodoc:
def write_entry(key, entry, options) # :nodoc: def write_entry(key, entry, options) # :nodoc:
synchronize do synchronize do
old_entry = @data[key] old_entry = @data[key]
return false if @data.key?(key) && options[:unless_exist]
@cache_size -= old_entry.size if old_entry @cache_size -= old_entry.size if old_entry
@cache_size += entry.size @cache_size += entry.size
@key_access[key] = Time.now.to_f @key_access[key] = Time.now.to_f
Expand Down
7 changes: 7 additions & 0 deletions activesupport/test/caching_test.rb
Expand Up @@ -684,6 +684,13 @@ def @cache.delete_entry (*args)
assert @cache.exist?(2) assert @cache.exist?(2)
assert !@cache.exist?(1) assert !@cache.exist?(1)
end 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 end


uses_memcached 'memcached backed store' do uses_memcached 'memcached backed store' do
Expand Down

0 comments on commit b5005b6

Please sign in to comment.