1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add delete_multi method

This commit is contained in:
Peter Zhu 2019-08-15 11:50:22 -04:00 committed by Jeremy Daer
parent d64010fe67
commit 59e746d4d0
4 changed files with 43 additions and 0 deletions

View file

@ -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.

View file

@ -477,6 +477,18 @@ module ActiveSupport
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.
@ -603,6 +615,18 @@ module ActiveSupport
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

View file

@ -420,6 +420,11 @@ module ActiveSupport
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?

View file

@ -375,6 +375,16 @@ module CacheStoreBehavior
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")
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)