diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2017-06-06 16:39:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-06 16:39:20 -0700 |
commit | 2b96d5822bfe407be7589e293f3265c0c7a6726c (patch) | |
tree | f50c2eef76a67707b10be6ce14e856ab95c19cfc /activesupport/test | |
parent | 0d55b5bb5dfdcf65d1e9a10b567579e3d339c2c9 (diff) | |
download | rails-2b96d5822bfe407be7589e293f3265c0c7a6726c.tar.gz rails-2b96d5822bfe407be7589e293f3265c0c7a6726c.tar.bz2 rails-2b96d5822bfe407be7589e293f3265c0c7a6726c.zip |
Cache: write_multi (#29366)
Rails.cache.write_multi foo: 'bar', baz: 'qux'
Plus faster `fetch_multi` with stores that implement `write_multi_entries`.
Keys that aren't found may be written to the cache store in one shot
instead of separate writes.
The default implementation simply calls `write_entry` for each entry.
Stores may override if they're capable of one-shot bulk writes, like
Redis `MSET`.
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/caching_test.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index f53b98c73e..f2f8c58111 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -1299,3 +1299,61 @@ class CacheEntryTest < ActiveSupport::TestCase assert_equal value.bytesize, entry.size end end + +class CacheStoreWriteMultiEntriesStoreProviderInterfaceTest < ActiveSupport::TestCase + setup do + @cache = ActiveSupport::Cache.lookup_store(:null_store) + end + + test "fetch_multi uses write_multi_entries store provider interface" do + assert_called_with(@cache, :write_multi_entries) do + @cache.fetch_multi "a", "b", "c" do |key| + key * 2 + end + end + end +end + +class CacheStoreWriteMultiInstrumentationTest < ActiveSupport::TestCase + setup do + @cache = ActiveSupport::Cache.lookup_store(:null_store) + end + + test "instrumentation" do + writes = { "a" => "aa", "b" => "bb" } + + events = with_instrumentation "write_multi" do + @cache.write_multi(writes) + end + + assert_equal %w[ cache_write_multi.active_support ], events.map(&:name) + assert_nil events[0].payload[:super_operation] + assert_equal({ "a" => "aa", "b" => "bb" }, events[0].payload[:key]) + end + + test "instrumentation with fetch_multi as super operation" do + skip "fetch_multi isn't instrumented yet" + + events = with_instrumentation "write_multi" do + @cache.fetch_multi("a", "b") { |key| key * 2 } + end + + assert_equal %w[ cache_write_multi.active_support ], events.map(&:name) + assert_nil events[0].payload[:super_operation] + assert !events[0].payload[:hit] + end + + private + def with_instrumentation(method) + event_name = "cache_#{method}.active_support" + + [].tap do |events| + ActiveSupport::Notifications.subscribe event_name do |*args| + events << ActiveSupport::Notifications::Event.new(*args) + end + yield + end + ensure + ActiveSupport::Notifications.unsubscribe event_name + end +end |