Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete_multi method to cache #36927

Merged
merged 1 commit into from Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,7 @@
* Add `ActiveSupport::Cache::Store#delete_multi` method to delete multiple keys from the cache store.

*Peter Zhu*

* Support multiple arguments in `HashWithIndifferentAccess` for `merge` and `update` methods, to
follow Ruby 2.6 addition.

Expand Down
24 changes: 24 additions & 0 deletions activesupport/lib/active_support/cache.rb
Expand Up @@ -477,6 +477,18 @@ def delete(name, options = nil)
end
end

# Deletes multiple entries in the cache.
#
# Options are passed to the underlying cache implementation.
def delete_multi(names, options = nil)
options = merged_options(options)
names.map! { |key| normalize_key(key, options) }

instrument :delete_multi, names do
delete_multi_entries(names, options)
end
end

# Returns +true+ if the cache contains an entry for the given key.
#
# Options are passed to the underlying cache implementation.
Expand Down Expand Up @@ -603,6 +615,18 @@ def delete_entry(key, options)
raise NotImplementedError.new
end

# Deletes multiples entries in the cache implementation. Subclasses MAY
# implement this method.
def delete_multi_entries(entries, options)
entries.inject(0) do |sum, key|
if delete_entry(key, options)
sum + 1
else
sum
end
end
end

# Merges the default options with ones specific to a method call.
def merged_options(call_options)
if call_options
Expand Down
5 changes: 5 additions & 0 deletions activesupport/lib/active_support/cache/redis_cache_store.rb
Expand Up @@ -420,6 +420,11 @@ def delete_entry(key, options)
end
end

# Deletes multiple entries in the cache. Returns the number of entries deleted.
def delete_multi_entries(entries, options)
redis.with { |c| c.del(entries) }
end

# Nonstandard store provider API to write multiple values at once.
def write_multi_entries(entries, expires_in: nil, **options)
if entries.any?
Expand Down
10 changes: 10 additions & 0 deletions activesupport/test/cache/behaviors/cache_store_behavior.rb
Expand Up @@ -375,6 +375,16 @@ def test_delete
assert_not @cache.exist?("foo")
end

def test_delete_multi
@cache.write("foo", "bar")
assert @cache.exist?("foo")
@cache.write("hello", "world")
assert @cache.exist?("foo")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it it the good assertion here?

assert_equal 2, @cache.delete_multi(["foo", "does_not_exist", "hello"])
assert_not @cache.exist?("foo")
assert_not @cache.exist?("hello")
end

def test_original_store_objects_should_not_be_immutable
bar = +"bar"
@cache.write("foo", bar)
Expand Down